Recently to help my friends to find out about my address book problems, by the way a demo for everyone to share the study.
#import <UIKit/UIKit.h>@interface viewcontroller:uiviewcontroller <uitableviewdelegate ,uitableviewdatasource>*Personarray; @end
#import "ViewController.h"#import<AddressBook/AddressBook.h>#import<AddressBookUI/AddressBookUI.h>#import<MessageUI/MessageUI.h>#defineJekprompt @ "Tips"#defineJekaddressbookmessage @ "Come and find me."//send text messages in the content@interfaceViewcontroller () {Nsinteger sectionnumber; Nsinteger RecordID; NSString*name; NSString*email; NSString*Tel; Nsmutablearray*addressbooktemp; } @property (Nonatomic, assign) abaddressbookref addressbookref; @property nsinteger sectionnumber; @property Nsinteger RecordID, @property (nonatomic, retain) nsstring*Name: @property (nonatomic, retain) nsstring*e- Mail; @property (nonatomic, retain) nsstring*Tel;@end@implementationViewcontroller@synthesizeName,email,tel,recordid,sectionnumber,personarray;- (void) viewdidload {[Super viewdidload]; Self.title=@"invite in Address book"; Personarray=[Nsmutablearray array]; UITableView*tabview = [[UITableView alloc]initwithframe:cgrectmake (0,0, Self.view.frame.size.width, Self.view.frame.size.height) Style:uitableviewstyleplain]; Tabview.Delegate=Self ; Tabview.datasource=Self ; [Self.view Addsubview:tabview]; [Self getBook];}#pragmaMark-Get contacts on your phone and resolve contacts-(Nsmutablearray *) getbook{if(Personarray = =Nil) {Personarray=[Nsmutablearray array]; } //Create a new address Book classAbaddressbookref Addressbooks =Nil; if([[Uidevice currentdevice].systemversion Floatvalue] >=6.0) {Addressbooks=abaddressbookcreatewithoptions (null, NULL); //get Address Book permissionsdispatch_semaphore_t Sema= Dispatch_semaphore_create (0); Abaddressbookrequestaccesswithcompletion (Addressbooks,^(BOOLgranted, cferrorref error) {dispatch_semaphore_signal (SEMA);}); Dispatch_semaphore_wait (Sema, dispatch_time_forever); } Else{addressbooks=abaddressbookcreate (); } //get everyone in the Address bookCfarrayref allpeople =abaddressbookcopyarrayofallpeople (addressbooks); //Number of contactsCfindex npeople =Abaddressbookgetpersoncount (addressbooks); //Loop to get everyone's personal information for(Nsinteger i =0; i < npeople; i++) { //Create a new AddressBook model classViewcontroller *addressbook =[[Viewcontroller alloc] init]; //Get personalAbrecordref person =Cfarraygetvalueatindex (allpeople, i); //Get your nameCftyperef Abname =Abrecordcopyvalue (person, kabpersonfirstnameproperty); Cftyperef Ablastname=Abrecordcopyvalue (person, kabpersonlastnameproperty); Cfstringref Abfullname=abrecordcopycompositename (person); NSString*namestring = (__bridge NSString *) Abname; NSString*lastnamestring = (__bridge NSString *) Ablastname; if((__bridgeID) Abfullname! =Nil) {namestring= (__bridge NSString *) Abfullname; } Else { if((__bridgeID) Ablastname! =Nil) {namestring= [NSString stringWithFormat:@"%@ %@", namestring, lastnamestring]; }} addressbook.name=namestring; Addressbook.recordid= (int) Abrecordgetrecordid (person);; Abpropertyid multiproperties[]={kabpersonphoneproperty, kabpersonemailproperty}; Nsinteger Multipropertiestotal=sizeof(multiproperties)/sizeof(Abpropertyid); for(Nsinteger j =0; J < Multipropertiestotal; J + +) {Abpropertyid property=Multiproperties[j]; Abmultivalueref Valuesref=Abrecordcopyvalue (person, property); Nsinteger Valuescount=0; if(Valuesref! = nil) Valuescount =Abmultivaluegetcount (VALUESREF); if(Valuescount = =0) {cfrelease (valuesref); Continue; } //get phone numbers and email for(Nsinteger k =0; K < Valuescount; k++) {Cftyperef value=Abmultivaluecopyvalueatindex (Valuesref, k); Switch(j) { Case 0: {//Phone NumberAddressbook.tel = (__bridge nsstring*) value; Break; } Case 1: {//EmailAddressbook.email = (__bridge nsstring*) value; Break; }} cfrelease (value); } cfrelease (Valuesref); } //Add personal information to the array, and after the loop is complete, the addressbooktemp contains information about all the contacts[Personarray Addobject:addressbook]; if(Abname) cfrelease (abname); if(Ablastname) cfrelease (ablastname); if(Abfullname) cfrelease (abfullname); NSLog (@"---%@", Personarray); } returnPersonarray;}#pragmaMark-uitableview Delegate and Datasource functions-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView {return 1;}-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section {returnPersonarray.count;}-(CGFloat) TableView: (UITableView *) TableView heightforheaderinsection: (nsinteger) section{return 0.1;}-(CGFloat) TableView: (uitableview*) TableView Heightforrowatindexpath: (nsindexpath*) Indexpath {return 52.5;}-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath {nsstring*cellidentifier =@"Contactcell"; UITableViewCell*cell =[TableView Dequeuereusablecellwithidentifier:cellidentifier]; if(Cell = =Nil) {Cell=[[UITableViewCell alloc] Initwithstyle:uitableviewcellstylesubtitle reuseidentifier:cellidentifier]; } Viewcontroller*book =[Personarray ObjectAtIndex:indexPath.row]; Cell.textLabel.text=Book.name; Cell.detailTextLabel.text=Book.tel; returncell;}- (void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpath {[TableView deselectrowatindexpath:indexpath animated:yes]; Viewcontroller*book =[Personarray ObjectAtIndex:indexPath.row]; NSString* PhoneNumber = [Book.tel stringbyreplacingoccurrencesofstring:@"-"Withstring:@""]; [Self sendsmsmessagewithphonenumber:phonenumber];}#pragmaMark-SMS-(void) Sendsmsmessagewithphonenumber: (NSString *) phonenumber{if([Mfmessagecomposeviewcontroller Cansendtext]) {[Self displaysmscomposersheetphonenumber:phonenumber]; } Else{Uialertview*msgbox = [[Uialertview alloc] Initwithtitle:jekprompt message:@"the device does not support texting" Delegate: Nil Cancelbuttontitle:@"Cancel"Otherbuttontitles:nil]; [MsgBox Show]; }}-(void) Displaysmscomposersheetphonenumber: (NSString *) phonenumber{Mfmessagecomposeviewcontroller*picker =[[Mfmessagecomposeviewcontroller alloc] init]; Picker.messagecomposedelegate= (ID<MFMessageComposeViewControllerDelegate>) Self; Picker.recipients=[Nsarray Arraywithobject:phonenumber]; Picker.body=[nsstring stringWithFormat:@"%@", Jekaddressbookmessage]; [Self Presentviewcontroller:picker animated:yes completion:nil]; }- (void) Messagecomposeviewcontroller: (Mfmessagecomposeviewcontroller *Controller Didfinishwithresult: (messagecomposeresult) Result {Switch(Result) { CaseMessagecomposeresultcancelled:nslog (@"result:sms Sending canceled"); Break; CaseMessagecomposeresultsent:nslog (@"Result:sms sent"); Break; Casemessagecomposeresultfailed: {uialertview*msgbox = [[Uialertview alloc] Initwithtitle:jekprompt message:@"SMS Send failed" Delegate: Nil Cancelbuttontitle:@"Cancel"Otherbuttontitles:nil]; [MsgBox Show]; } Break; default: NSLog (@"result:sms not sent"); Break; } [self Dismissviewcontrolleranimated:yes completion:nil];}@end
Address Book Development