CocoaPods trunk is moving to be read-only. Read more on the blog, there are 18 months to go.

ContactsKit 2.1.2

ContactsKit 2.1.2

TestsTested
LangLanguage Obj-CObjective C
License MIT
ReleasedLast Release Apr 2017

Maintained by Sergey Popov.



  • By
  • Sergey Popov

ContactsKit is a library for easy contact management supports iOS and Mac OS X.

Features

  • Unifying linked contacts
  • Support Mac OS X
  • Contact management add, update, delete
  • Support NSCoding
  • Observing changes (adding, updating, deleting) for iOS

Podfile

To integrate ContactsKit into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '7.0'

pod 'ContactsKit'

Then, run the following command:

$ pod install

Get started

Import ContactsKit into your porject

	
	#import <ContactsKit/ContactsKit.h>
	

Firstly you have to create an instance of the CKAddressBook, and request an access for getting contacts. The user will only be prompted the first time access is requested.

	
	CKAddressBook *addressBook = [[CKAddressBook alloc] init];
    
    [addressBook requestAccessWithCompletion:^(NSError *error) {
        
        if (! error)
        {
			// Everything fine you can get contacts
        }
        else
        {
			// The app doesn't have a permission for getting contacts
			// You have to go to the settings and turn on contacts
        }
    }];

Then if the access is granted you can get contacts

	// Get fields from the mask
    CKContactField mask = CKContactFieldFirstName | CKContactFieldLastName | CKContactFieldBirthday;
    
    // Final sort of the contacts array
    NSArray *sortDescriptors = @[ [NSSortDescriptor sortDescriptorWithKey:@"firstName" ascending:YES] ];
    
    [addressBook contactsWithMask:mask uinify:NO sortDescriptors:sortDescriptors
                           filter:nil completion:^(NSArray *contacts, NSError *error) {
       
        if (! error)
        {
            // Do someting with contacts
        }
        
    }];

Or do the same by using the CKAddressBookDelegate protocol

	
	addressBook.fieldsMask = CKContactFieldFirstName | CKContactFieldLastName | CKContactFieldBirthday;
    addressBook.sortDescriptors = @[ [NSSortDescriptor sortDescriptorWithKey:@"firstName" ascending:YES] ];
    addressBook.delegate = self;
    
    [addressBook fetchContacts];

Then the protocol method will called

	#pragma mark - CKAddressBookDelegate

	- (void)addressBook:(CKAddressBook *)addressBook didFetchContacts:(NSArray<CKContact *> *)contacts
	{
    	// Do something with contacts
	}