1. Get a list of contacts
+ (nsmutabledictionary*) getaddresspeoplearray{BOOL granted=[Addressengine getaccessgranted];//Nsmutablearray*addarray=[nsmutablearray arraywithcapacity:0];Nsmutabledictionary * Addrdic = [Nsmutabledictionary dictionarywithcapacity:0]; if(granted) {Abaddressbookref addressbook=abaddressbookcreatewithoptions (null, NULL); Abaddressbookrequestaccesswithcompletion (AddressBook,^(BOOLgranted, cferrorref error) { }); Nsarray*array = (__bridge Nsarray *) abaddressbookcopyarrayofallpeople (addressbook); //NSLog (@ "Kaishi"); for(inti =0; i<array.count;i++) {Abrecordref person=(__bridge abrecordref) ([array objectatindex:i]); Abmutablemultivaluerefref= Abrecordcopyvalue (person, kabpersonphoneproperty);//read phone format 111-2222-2222Addresspeople * people=[[Addresspeople alloc]init]; Nsinteger ncount= Abmultivaluegetcount (ref);//number of single-person phones if(ncount>5|| ncount<1 ) { Continue; } Addresspeokey*akey=[[Addresspeokey alloc]init]; Akey.count=ncount; Akey.phoneset=[nsmutableset setwithcapacity:0]; People.phnum=[nsmutablearray arraywithcapacity:0]; for(inti =0; I < ncount;i++) {NSString*phoneno=[addressengine trimilegalchar:[self Trimilegalchar: (__bridge NSString *) (Abmultivaluecopyvalueatindex (ref, i)]]; if(phoneno!=nil&&! [Phoneno isequaltostring:@""]) { if([Phoneno Hasprefix:@"+86"]) {Phoneno=[phoneno Substringfromindex:3]; } [People.phnum Addobject:phoneno]; [Akey.phoneset Addobject:phoneno]; }} people.name= (__bridge NSString *) abrecordcopycompositename (person);//name if(people.name==Nil) {People.name=@"name unknown"; } if(people.name.length>0) {NSString*firstchar = [People.name substringtoindex:1]; Const Char* CString =[Firstchar utf8string]; if(cstring!=NULL) { if(strlen (cString) = =3) { //the name is the beginning of Chinese charactersNsmutablestring *ms =[[Nsmutablestring alloc] initWithString:people.name]; Cfstringtransform (__bridge cfmutablestringref) MS,0, Kcfstringtransformtolatin, NO); //Cfstringtransform ((__bridge cfmutablestringref) MS, 0, Kcfstringtransformstripdiacritics, NO);People.pinyinname=[NSString Stringwithstring:ms]; People.pinyinname=[Addressengine TrimIlegalChar:people.pinyinName]; //NSLog (@ "pinyin:%@", people.pinyinname);}Else{people.pinyinname=[People.name lowercasestring]; } }Else{people.name=[people.phnum Objectatindex:0]; People.pinyinname=@"#FX"; } }Else{people.name=[people.phnum Objectatindex:0]; People.pinyinname=@""; } Nsinteger Rec=Abrecordgetrecordid (person); People.recordid=[nsstring stringWithFormat:@"%ld", REC]; People.remark= (__bridge NSString *) (Abrecordcopyvalue (person, kabpersonnoteproperty));//Notes if(!people.remark) {People.remark=@""; } if(people.remark.length>0) {NSString*re=People.remark; People.remark=[Addressengine Cleanquoteswithstr:re]; } people.ischoose=NO; People.ismark=NO; [Addrdic setobject:people Forkey:akey];//[AddArray addobject:people]; } //NSLog (@ "Jieshu"); } Else{Uialertview*alert=[[uialertview Alloc]initwithtitle:@"Restricted Permissions"message:@"Settings-Phone Vatican-enable Address Book permissions" Delegate: nil Cancelbuttontitle:@"Determine"Otherbuttontitles:nil]; [Alert show]; }//return addarray; returnAddrdic;}
2. Determine if you have permission
+(BOOL) getaccessgranted{abaddressbookref addressbook=NULL; __block BOOL accessgranted=NO; if(&abaddressbookrequestaccesswithcompletion! =NULL) {AddressBook=abaddressbookcreatewithoptions (null, NULL); dispatch_semaphore_t Sema= Dispatch_semaphore_create (0); Abaddressbookrequestaccesswithcompletion (AddressBook,^(BOOLgranted, cferrorref error) {accessgranted=granted; Dispatch_semaphore_signal (SEMA); }); Dispatch_semaphore_wait (Sema, dispatch_time_forever); } Else{accessgranted=YES; } returnaccessgranted;}
iOS Read contacts