The content is from the iOS development guide of Dongsheng Teacher
Some knowledge that has been written about the contact framework
Now write about reading contact data related operations
To read the Address Book database requires
- Create Address Book Object
- Query get data (all or part)
- Get the Address Book a record (all of a person's data)
- Get the various attribute data for this person
That's it
Cferrorref error = NULL; Create an Address Book Action object abaddressbookref addressbook = abaddressbookcreatewithoptions (NULL, &error);
The first parameter is the reserved parameter, and the null can be passed;
Find contact-(void) Filtercontentforsearchtext: (NSString *) searchtext{ //Infer authorization status if ( Abaddressbookgetauthorizationstatus ()!=kabauthorizationstatusauthorized) { return; } Cferrorref error = NULL; Abaddressbookref addressbook = abaddressbookcreatewithoptions (NULL, &error); if (Searchtext.length = = 0) { //Find all listcontacts = Cfbridgingrelease (Abaddressbookcopyarrayofallpeople ( AddressBook)); } else{ //Find prefix by string keyword cfstringref cfsearchtext = (cfstringref) cfbridgingretain (searchtext); Listcontacts = Cfbridgingrelease (Abaddressbookcopypeoplewithname (AddressBook, Cfsearchtext)); Cfrelease (Cfsearchtext); } This should refresh the table [Self.tableview reloaddata]; Cfrelease (addressbook);}
Status of Authorization: kabauthorizationstatusnotdetermined User has not yet decided kabauthorizationstatusrestricted , restricted kabauthorizationstatusdenied, refusing kabauthorizationstatusauthorized permission
Properties of the contact person
Kabpersonfirstnameproperty; Name Kabpersonlastnameproperty; Surname Kabpersonmiddlenameproperty; Middle name Kabpersonprefixproperty; Prefix kabpersonsuffixproperty; suffix kabpersonnicknameproperty; Nickname Kabpersonfirstnamephoneticproperty; The name of the Hanyu Pinyin or phonetic kabpersonlastnamephoneticproperty; Surname Pinyin or phonetic kabpersonmiddlenamephoneticproperty; The middle name of Hanyu Pinyin or phonetic kabpersonorganizationproperty; Organization name Kabpersonjobtitleproperty; Job title Kabpersondepartmentproperty; Department Kabpersonnoteproperty; Remark Kabpersonbirthdayproperty; Birthday Kabpersoncreationdateproperty; Creation time Kabpersonmodificationdateproperty; Change Date
Multivalued attributes (multiple values in one property) have labels, values, id kabpersonphoneproperty; Phone number attribute kabmultistringpropertytype;//type kabpersonemailproperty; e-mail attribute kabmultistringpropertytype;//type kabpersonurlproperty; URL attribute kabmultistringpropertytype;//type kabpersonrelatednamesproperty; Kinship person attribute kabmultistringpropertytype;//type kabpersonaddressproperty; Address attribute kabmultidictionarypropertytype;//type kabpersoninstantmessageproperty;//Instant Chat property kabmultidictionarypropertytype;//type kabpersonsocialprofileproperty;//Social account properties kabmultidictionarypropertytypel;//type
Use the properties above to find the desired value
The following are single-valued properties:
-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{ static NSString *cellidentifier = @ "Cell"; UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:cellidentifier]; if (cell) { cell = [[UITableViewCell alloc]initwithstyle:uitableviewcellstyledefault Reuseidentifier: Cellidentifier]; } Get a piece of data from the searched contact group into abrecordref format abrecordref Thisperson = Cfbridgingretain ([listcontacts objectatindex:[ Indexpath Row]]); Find the name in this record nsstring *firstname = Cfbridgingrelease (Abrecordcopyvalue (Thisperson, Kabpersonfirstnameproperty) ); FirstName = firstName! = nil? firstname:@ ""; Find the last name in this record nsstring *lastname = Cfbridgingrelease (Abrecordcopyvalue (Thisperson, Kabpersonlastnameproperty)) ; Cell.textLabel.text = [NSString stringwithformat:@ "%@%@", Firstname,lastname]; Cfrelease (Thisperson); return cell;}
Here's how to find a multivalued attribute:
//get multi-valued attribute-(void) multivalueproperty{abrecordid personrecordid = [Personidasnumber Intvalue];//personidasnumber is the value of the NSNumber type used to hold the value of RecordID cferrorref error = NULL; Abaddressbookref addressbook = abaddressbookcreatewithoptions (NULL, &error); Find Abrecord abrecordref Personrecord = Abaddressbookgetpersonwithrecordid via the Abrecordid property (AddressBook, Personrecordid); Find multivalued properties by Abrecord abmultivalueref Emailproperty = Abrecordcopyvalue (Personrecord, Kabpersonemailproperty); Convert multiple values of multivalued attributes to arrays Nsarray * Emailarray = Cfbridgingrelease (Abmultivaluecopyarrayofallvalues (Emailproperty)); for (int index = 0; index < emailarray.count; index++) {NSString *email = [Emailarray objectatindex:index]; NSString *emaillabel = Cfbridgingrelease (Abmultivaluecopylabelatindex (Emailproperty, index)); Infer the current worthy tag if ([Emaillabel isequaltostring: (NSString *) Kabworklabel]) {NSLog (@ "%@", email); } }}
Get a picture of a contact:
Get a picture of a contact-(void) setimage{ cferrorref error = NULL; Abaddressbookref addressbook = abaddressbookcreatewithoptions (NULL, &error); Abrecordref cecordref = Abaddressbookgetpersonwithrecordid (Addressbook,[personidasnumber intValue]); Infer if a contact has a photo if (Abpersonhasimagedata (cecordref)) { //Get photo data NSData *photodata = Cfbridgingrelease ( Abpersoncopyimagedata (Cecordref)); Self.imageView.image = [UIImage imagewithdata:photodata]; }}
IOS Contacts-Get Contact properties