#pragmaMark-Get phone all contacts-(void) Obtainphoneallcontact {//This variable is used to record whether the authorization is successful, that is, whether the user allows us to access int__block tip=0; //declaring a reference to an address bookAbaddressbookref Addbook =Nil; //because there is a difference in the way in which permissions are applied after IOS6.0 and before, make a judgment here if([[Uidevice currentdevice].systemversion floatvalue]>=6.0) { //Create a reference to the Address bookaddbook=abaddressbookcreatewithoptions (null, NULL); //create a signal with an incident semaphore of 0dispatch_semaphore_t Sema=dispatch_semaphore_create (0); //Request access RightsAbaddressbookrequestaccesswithcompletion (Addbook, ^ (BOOLgreanted, cferrorref error) { //greanted for yes means user allowed, otherwise not allowed if(!greanted) {Tip=1; } //send a signal oncedispatch_semaphore_signal (SEMA); }); //wait for the signal to triggerdispatch_semaphore_wait (SEMA, dispatch_time_forever); } Else { //before IOS6Addbook =abaddressbookcreate (); } if(tip) {//make a friendly tipUialertview * Alart = [[Uialertview alloc]initwithtitle:@"Warm Tips"message:@"please set allow app to access your contacts \nsettings>general>privacy" Delegate: Self cancelbuttontitle:@"Determine"Otherbuttontitles:nil, nil Nil]; [Alart show]; return ; } //get an array of all contactsCfarrayref alllinkpeople =abaddressbookcopyarrayofallpeople (Addbook); //get total number of contactsCfindex number =Abaddressbookgetpersoncount (Addbook); //to traverse for(Nsinteger i=0; i<number; i++) { //get a reference to a contact objectAbrecordref people =Cfarraygetvalueatindex (alllinkpeople, i); //get the current contact nameNsstring*firstname= (__bridge NSString *) (Abrecordcopyvalue (People, kabpersonfirstnameproperty)); //get current Contact last nameNsstring*lastname= (__bridge NSString *) (Abrecordcopyvalue (People, kabpersonlastnameproperty)); //get the company of the current contactnsstring*organization= (__bridge nsstring*) (Abrecordcopyvalue (People, kabpersonorganizationproperty)); //get the current contact's positionnsstring*job= (__bridge nsstring*) (Abrecordcopyvalue (People, kabpersonjobtitleproperty)); //get the Department of the current contactNsstring*department= (__bridge nsstring*) (Abrecordcopyvalue (People, kabpersondepartmentproperty)); //get an array of phone numbers for the current contactNsmutablearray * Phonearr =[[Nsmutablearray alloc]init]; Abmultivalueref Phones=Abrecordcopyvalue (people, kabpersonphoneproperty); for(Nsinteger j=0; J<abmultivaluegetcount (phones); J + +) {[Phonearr addobject: (__bridge nsstring*) (Abmultivaluecopyvalueatindex (phones, j))]; } //get the current contact avatar pictureNsdata*userimage= (__bridge nsdata*) (Abpersoncopyimagedata (people)); if(LastName = =NULL) {NSLog (@"Name:%@", FirstName); } Else if(FirstName = =NULL) {NSLog (@"Name:%@", LastName); } Else if(FirstName! = NULL && LastName! =NULL) {NSLog (@"Name:%@%@", Lastname,firstname); } nsstring*str =Phonearr; NSLog (@"Contact Tel:%@", Phonearr); if(Userimage = =NULL) {NSLog (@"picture is empty"); } } }
IOS--Extract local < contacts > Data <1>