import AddressBook and Addressbookui frameworks
before IOS 6, you can get contacts in the following ways
abaddressbookref addressbook = Abaddressbookcreate ();
However, after iOS 6, this method is deprecated and can be used to obtain the Address Book in the following way.
ab_extern abaddressbookref abaddressbookcreatewithoptions (cfdictionaryref options, cferrorref* error) __OSX_ Available_starting (__MAC_NA,__IPHONE_6_0);
and after IOS6, each app should be authorized by the user to access the Address Book:
//system-defined block
typedef void (^Abaddressbookrequestaccesscompletionhandler) (bool granted, cferrorref error);
//Get the authorization method, where the second parameter is the block defined above
ab_extern void Abaddressbookrequestaccesswithcompletion (Abaddressbookref addressbook,Abaddressbookrequestaccesscompletionhandlercompletion) __osx_available_starting (__MAC_NA,__IPHONE_6_0);
The code is as follows:
-(void) getaddress{//typedef cftyperef abaddressbookref; typedef const VOID * CFTYPEREF; Pointer to constant abaddressbookref addressbook = nil; Determine the current version of the system if ([[[Uidevice currentdevice].systemversion Floatvalue] >= 6.0) {//If not less than 6.0, use the corresponding API to get the Address book, note that The user's consent must be requested first, if no consent is obtained or the user is not operational, the contents of this address Book are empty addressbook = abaddressbookcreatewithoptions (null, NULL);//wait for consent to execute down//to ensure user consent After the operation, at this time using a multi-threaded semaphore mechanism, create a semaphore, the number of resources of the semaphore 0 means no resources, call dispatch_semaphore_wait will immediately wait. If you do not understand this, please refer to the GCD semaphore synchronization related content. dispatch_semaphore_t sema = dispatch_semaphore_create (0);//Make a request to access the Address Book abaddressbookrequestaccesswithcompletion (add Ressbook, ^ (bool granted, cferrorref error) {//If the user agrees, the method inside this block will be executed//This method sends a signal to increase the number of resources Dispatch_semaphore_signal (SEMA);}); If the previous block is not executed, then the number of SEMA resources is zero, the program will be blocked//when the user chooses to agree, the block method is executed, the number of SEMA resources is 1; dispatch_semaphore_wait (Sema, Dispatch _time_forever); Dispatch_release (SEMA); }//if the system is a system prior to 6.0, you do not need to obtain consent to access else{directly AddreSsbook = Abaddressbookcreate (); }//Address book information has been obtained, start to remove cfarrayref results = abaddressbookcopyarrayofallpeople (addressbook); Number of contact entries (using long instead of int is for compatibility with 64-bit) long Peoplecount = Cfarraygetcount (results); for (int i=0; i<peoplecount; i++) {abrecordref record = Cfarraygetvalueatindex (results, i); NSString *firstname,*lastname; FirstName = nil; Lastname= Nil; Cftyperef tmp = NULL; FirstName tmp = Abrecordcopyvalue (record, kabpersonfirstnameproperty); if (tmp) {firstName = [nsstring stringwithstring:tmp]; Cfrelease (TMP); tmp = NULL; }//lastname tmp = Abrecordcopyvalue (record, kabpersonlastnameproperty); if (TMP) {lastname= [nsstring stringwithstring:tmp]; Cfrelease (TMP); TMP = NULL; }//Get full name (not related to FirstName, LastName above) Cfstringref Fullname=abrecordcopycompositename (record); NSLog (@ "%@", (nsstring*) fullName); Read the phone, more than oneAbmultivalueref phones = Abrecordcopyvalue (record, kabpersonphoneproperty); Long Phonecount = abmultivaluegetcount (phones); for (int j=0; j<phonecount; J + +) {//label cfstringref lable = Abmultivaluecopylabelatindex (phones, j); Phone number Cfstringref number = Abmultivaluecopyvalueatindex (phones, j); Localize label Cfstringref local = Abaddressbookcopylocalizedlabel (lable); Here you can use a custom model class to store name and phone information. I'm going to go straight out here. NSLog (@ "number%@", (NSString *) number); if (local) cfrelease (local); if (lable) cfrelease (lable); if (number) cfrelease (number); } if (phones) cfrelease (phones); record = NULL; } if (results) cfrelease (results); results = Nil;if (addressbook) cfrelease (addressbook); addressbook = NULL;}
Request access to the Address book, only the first pop-up alert box asks the user. You can use the following methods to determine the status to alert the user
typedef cf_enum (Cfindex, abauthorizationstatus) {kabauthorizationstatusnotdetermined = 0, kabauthorizations tatusrestricted, kabauthorizationstatusdenied, kabauthorizationstatusauthorized}; Ab_extern abauthorizationstatus abaddressbookgetauthorizationstatus (void) __osx_available_starting (__MAC_NA,__ IPHONE_6_0);
Get state, judge by status
Abauthorizationstatus Statu=abaddressbookgetauthorizationstatus ();
[iOS] IOS6 iOS7 access and use system contacts