Apple's official document, using Swift https://developer.apple.com/library/ios/documentation/Contacts/Reference/Contacts_Framework/ index.html#//apple_ref/doc/uid/tp40015328
Here is a record of your own use, only records, and a lot of unclear areas.
Cncontactstore My understanding is that a class that stores all contacts has an object method that sends an authorization request to the user
Requestaccessforentitytype:cnentitytypecontacts completionhandler:^ (BOOL Granted, Nserror * _Nullable error)
Request Authorization Succeeded granted = 1, otherwise error is returned.
Get all contacts:
1. Object Store call Enumeratecontactswithfetchrequest: request Error:&error usingblock:^ (cncontact * _nonnull Contact, BOOL * _nonnull stop) can get all the contacts ( here are the questions, see below ), here the request is Cncontactfetchrequest object, can be Initwithkeystofetch object method to create, the following array is the key you need, can be found at the bottom of the Cncontact class.
2. The cncontact returned in block is basically the contact that we need, the familyname,givenname in it is not much said, the key is phonenumbers this attribute, is an array, Inside the element is Cnlabeledvalue type of object, the pit father came, this class has a valuetype attribute, through the class method to know is actually cnphonenumber type, We can get the phone number strings we need via stringvalue. and Cnlabeledvalue's Label property is the type of contact phone number that I'm using (the printed string is a _$!<mobile>!$_ _$!
Note : I started the bottom of the method to get all the contacts in the authorization method, the result is a thread blocking problem, and then I deleted the authorization method (that is no longer authorized, the simulator test app deleted the rerun), but also can read the contact person, I do not know what to do here, I hope to have the understanding of the Great God Guide .
[Store Requestaccessforentitytype:cnentitytypecontacts completionhandler:^ (BOOL Granted, Nserror * _Nullable error) {
NSLog (@ "%d", granted);
}];
Here's the code for your test, just get the name and phone number of the contact.
==================================== The following is the actual code =======================================
Cncontactstore *store = [[Cncontactstore alloc]init];
Nserror *error = nil;
Cncontactfetchrequest *request = [[Cncontactfetchrequest Alloc]initwithkeystofetch:@[cncontactgivennamekey, Cncontactfamilynamekey,cncontactphonenumberskey]];
Nsmutablearray *datasource = [Nsmutablearray array];
[Store Enumeratecontactswithfetchrequest:request error:&error usingblock:^ (cncontact * _Nonnull Contact, BOOL * _ Nonnull stop) {
NSString *name = [NSString stringwithformat:@ "%@%@", Contact.givenname,contact.familyname]; //Name
NSString *phone = [NSString string];
For (Cnlabeledvalue *value in contact.phonenumbers) {
if ([Value.label isequaltostring:@ "_$!<mobile>!$_"]) {
Cnphonenumber *phonenum = Value.value;
Phone = Phonenum.stringvalue; //Telephone
}
}
}];
Add, delete, modify contacts etc. waiting to be replenished.
AC qq:758214335
Contacts.framework iOS9 new class, used instead of AddressBook