IOS--Extract local < contacts > Data <1>

Source: Internet
Author: User

#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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.