TestsTested | ✗ |
LangLanguage | Obj-CObjective C |
License | MIT |
ReleasedLast Release | Sep 2017 |
Maintained by Alexey Belkevich.
APAddressBook is a wrapper on AddressBook.framework that gives easy access to native address book without pain in a head.
Installation
Add APAddressBook
pod to Podfile
pod 'APAddressBook'
Warning for iOS 10.0 and after
To protect user privacy, an iOS app linked on or after iOS 10.0, and which accesses the user’s contacts, must statically declare the intent to do so. Include the NSContactsUsageDescription
key in your app’s Info.plist
file and provide a purpose string for this key. If your app attempts to access the user’s contacts without a corresponding purpose string, your app exits.
From here.
Load contacts
APAddressBook *addressBook = [[APAddressBook alloc] init];
// don't forget to show some activity
[addressBook loadContacts:^(NSArray <APContact *> *contacts, NSError *error)
{
// hide activity
if (!error)
{
// do something with contacts array
}
else
{
// show error
}
}];
Callback block will be run on main queue! If you need to run callback block on custom queue use
loadContactsOnQueue:completion:
method
Select contact fields bit-mask
Available fields:
Contact
recordID
property is always available
Example of field mask with name and thumbnail:
APAddressBook *addressBook = [[APAddressBook alloc] init];
addressBook.fieldsMask = APContactFieldFirstName | APContactFieldThumbnail;
Filter contacts
The most common use of this option is to filter contacts without phone number. Example:
addressBook.filterBlock = ^BOOL(APContact *contact)
{
return contact.phones.count > 0;
};
Sort contacts
APAddressBook returns unsorted contacts. So, most of users would like to sort contacts by first name and last name.
addressBook.sortDescriptors = @[
[NSSortDescriptor sortDescriptorWithKey:@"name.firstName" ascending:YES],
[NSSortDescriptor sortDescriptorWithKey:@"name.lastName" ascending:YES]
];
Load contact by address book record ID
[addressBook loadContactByRecordID:recordID completion:^(APContact *contact)
{
self.contact = contact;
}];
APContact
instance will contain fields that set inaddressBook.fieldsMask
Callback block will be run on main queue! If you need to run callback block on custom queue use
loadContactByRecordID:onQueue:completion:
method
Load contact photo by address book record ID
[addressBook loadPhotoByRecordID:recordID completion:^(UIImage *image)
{
self.imageView.image = image;
}];
Callback block will be run on main queue! If you need to run callback block on custom queue use
loadPhotoByRecordID:onQueue:completion:
method
Observe address book external changes
// start observing
[addressBook startObserveChangesWithCallback:^
{
// reload contacts
}];
// stop observing
[addressBook stopObserveChanges];
Request address book access
[addressBook requestAccess:^(BOOL granted, NSError *error)
{
// check `granted`
}];
Check address book access
switch([APAddressBook access])
{
case APAddressBookAccessUnknown:
// Application didn't request address book access yet
break;
case APAddressBookAccessGranted:
// Access granted
break;
case APAddressBookAccessDenied:
// Access denied or restricted by privacy settings
break;
}
Installation
pod 'APAddressBook/Swift'
Import APAddressBook-Bridging.h
to application's objective-c bridging file.
#import <APAddressBook/APAddressBook-Bridging.h>
Example
See example application in Example/Swift
directory.
self.addressBook.loadContacts(
{ (contacts: [APContact]?, error: Error?) in
if let uwrappedContacts = contacts {
// do something with contacts
}
else if let unwrappedError = error {
// show error
}
})
Use APContact-EasyMapping by Jean Lebrument
If you have improvements or concerns, feel free to post an issue and write details.
Check out all Alterplay's GitHub projects.
Email us with other ideas and projects.