iOS reads Address Book data

Source: Internet
Author: User

#import<Foundation/Foundation.h>@interfaceLoadingcontactdata:nsobject//reading Contacts+ (Loadingcontactdata *) shareintance;- (void) Getcontactdata;@end#import "LoadingContactData.h"StaticLoadingcontactdata *_contactdata;@implementationLoadingcontactdata+ (Loadingcontactdata *) shareintance{if(_contactdata = =Nil) {_contactdata=[[Loadingcontactdata alloc]init]; }    return_contactdata;}- (void) getcontactdata{cferrorref error=NULL; Abaddressbookref AddressBook= Abaddressbookcreatewithoptions (NULL, &error); if(addressbook) {Nsarray*allcontacts = (__bridge_transfer nsarray*) abaddressbookcopyarrayofallpeople (addressbook); Nsmutablearray*contactsarray = [NsmutablearrayNew];  for(Nsuinteger i =0; I<[allcontacts Count]; i++) {abrecordref ContactPerson=(__bridge abrecordref) allcontacts[i]; //Get Mobile numberAbmultivalueref Phonesref =Abrecordcopyvalue (ContactPerson, Kabpersonphoneproperty); Nsmutablearray*telephonearray =[self getmobilephoneproperty:phonesref]; [Telephonearray Enumerateobjectsusingblock:^(IDobj, Nsuinteger idx, BOOL *stop) {Thcontact*contact =[[Thcontact alloc] init]; //Get First and last names and FullNameNSString *firstname = (__bridge_transfer nsstring*) Abrecordcopyvalue (ContactPerson, Kabpersonfirstnameproperty); NSString*lastname = (__bridge_transfer nsstring*) Abrecordcopyvalue (ContactPerson, Kabpersonlastnameproperty); NSString*fullname = (__bridge_transfer NSString *) Abrecordcopycompositename (ContactPerson); //Set Contact PropertiesContact.firstname =FirstName; Contact.lastname=LastName; if(FullName! =Nil) {Contact.FullName=FullName; }                Else if(FirstName! = Nil && LastName! =Nil) {Contact.FullName= [NSString stringWithFormat:@"%@%@", Lastname,firstname]; } Else if(FirstName! =Nil) {Contact.FullName=FirstName; } Else if(LastName! =Nil) {Contact.FullName=LastName; } Else{contact.fullname=@""; }                                //Get Image if it existsNSData * Imgdata = (__bridge_transfer NSData *) Abpersoncopyimagedatawithformat (ContactPerson, Kabpersonimageformatthumbnail);//contact Avatar (cropped)//NSData *imgdata = (__bridge_transfer NSData *) abpersoncopyimagedata (ContactPerson); //Contact Avatar ArtworkUIImage *userimg =[UIImage Imagewithdata:imgdata]; Contact.image=userimg; if(!contact.image) {contact.image= Mf_pngwithskin (Defaults_skin,@"Icon-avatar.png"); } Contact.phone=[obj Telephonewithreformat]; if([Unity ValidateMobile:contact.phone]) {Contact.recordid= Abrecordgetrecordid (ContactPerson) + [[Contact.phone Substringwithrange:nsmakerange (2,8)] Integervalue]+idx;                [Contactsarray Addobject:contact];                }            }]; if(phonesref) {cfrelease (phonesref);                }} [[Contactdataarray sharecontactdata] setcontactdataarray:contactsarray]; if(addressbook) {cfrelease (addressbook); }    }    Else{NSLog (@"Error"); }}-(Nsmutablearray *) Getmobilephoneproperty: (abmultivalueref) phonesref{Nsmutablearray*dataarray =[[Nsmutablearray alloc]init];  for(intI=0; I < Abmultivaluegetcount (phonesref); i++) {cfstringref Currentphonevalue=Abmultivaluecopyvalueatindex (Phonesref, i); NSString*phone = (__bridge NSString *) Currentphonevalue;    [DataArray Addobject:phone]; }        returnDataArray;}@end

iOS reads Address Book data

Related Article

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.