IOS Address Book-get contact attributes, ios contact

Source: Internet
Author: User

IOS Address Book-get contact attributes, ios contact

The content is from instructor Guan Dongsheng's ios development guide.

In the previous article, I wrote some essential knowledge about the contact framework.

Write down the operations related to reading contact data.

To read the address book database, you must

That's it.
CFErrorRef error = NULL; // create an address book operation object ABAddressBookRef addressBook = abaddressbookcreatewitexceptions (NULL, & error );

The first parameter is the reserved parameter. Pass NULL;
// Find the contact-(void) filterContentForSearchText :( NSString *) searchText {// determine the authorization status if (ABAddressBookGetAuthorizationStatus ()! = KABAuthorizationStatusAuthorized) {return;} CFErrorRef error = NULL; ABAddressBookRef addressBook = abaddressbookcreatewitexceptions (NULL, & error); if (searchText. length = 0) {// query all listContacts = CFBridgingRelease (ABAddressBookCopyArrayOfAllPeople (addressBook);} else {// query the CFStringRef cfSearchText = (CFStringRef) based on the string prefix) CFBridgingRetain (searchText); listContacts = CFBridgingRelease (ABAddressBookCopyPeopleWithName (addressBook, cfSearchText); CFRelease (cfSearchText);} // refresh the table [self. tableView reloadData]; CFRelease (addressBook );}
 
Authorization status: The kABAuthorizationStatusNotDetermined user has not decided to kABAuthorizationStatusRestricted. The Restricted kABAuthorizationStatusDenied and the kABAuthorizationStatusAuthorized permission is denied.
 
Contact attributes
KABPersonFirstNameProperty; // name kABPersonLastNameProperty; // surname kABPersonMiddleNameProperty; // kABPersonPrefixProperty in the middle; // prefix kABPersonSuffixProperty; // suffix kABPersonNicknameProperty; // nickname; // The Name Of The Chinese pinyin or the kABPersonLastNamePhoneticProperty; // The Name Of The Chinese pinyin or the kABPersonMiddleNamePhoneticProperty; // The Name Of The kABPersonOrganizationProperty in the Chinese pinyin or the kABPersonJobTitleProperty; // job title kABPersonDepartmentProperty; // Department kABPersonNoteProperty; // remarks kABPersonBirthdayProperty; // birthday kABPersonCreationDateProperty; // creation time kABPersonModificationDateProperty; // modification date
// Multi-value attributes (multiple values in one attribute) include tags, values, and id kABPersonPhoneProperty; // call number attribute kABMultiStringPropertyType; // type kABPersonEmailProperty; // email attributes kABMultiStringPropertyType; // type kABPersonURLProperty; // URL attributes kABMultiStringPropertyType; // type attributes; // attributes of kinship attributes kABMultiStringPropertyType; // type kABPersonAddressProperty; // address attribute kABMultiDictionaryPropertyType; // type kABPersonInstantMessageProperty; // chat attribute kABMultiDictionaryPropertyType; // type kABPersonSocialProfileProperty; // attributes of the social account; // type

Use the above attributes to find the desired value
The following is a single-value attribute:
-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {static NSString * handle = @ "Cell"; UITableViewCell * cell = [tableView progress: CellIdentifier]; if (cell) {cell = [[UITableViewCell alloc] initWithStyle: inclureuseidentifier: CellIdentifier];} // retrieve a piece of data from the searched contact array and convert it to ABRecordRef in the ABRecordRef format ThisPerson = Response ([listContacts objectAtIndex: [indexPath row]); // search for the NSString * firstName = CFBridgingRelease (ABRecordCopyValue (thisPerson, expiration) in this record; firstName = firstName! = Nil? FirstName: @ ""; // search for the last name NSString * lastName = CFBridgingRelease (ABRecordCopyValue (thisPerson, kABPersonLastNameProperty) in this record); cell. textLabel. text = [NSString stringWithFormat: @ "% @", firstName, lastName]; CFRelease (thisPerson); return cell ;}


The following describes how to query multi-value attributes:
// Obtain the multi-Value Attribute-(void) multiValueProperty {ABRecordID personRecordID = [personIDASNumber intValue]; // The value of personIDASNumber is an NSNumber type value used to save the value of RecordID CFErrorRef error = NULL; ABAddressBookRef addressBook = abaddressbookcreatewitexceptions (NULL, & error); // locate ABRecord ABRecordRef personRecord = ABAddressBookGetPersonWithRecordID (addressBook, personRecordID) through the ABRecordID attribute ); // use ABRecord to find the multi-value attribute ABMultiValueRef emailProperty = ABRecordCopyValue (personRecord, kABPersonEmailProperty ); // convert multiple values of the multi-value attribute to the array NSArray * emailArray = CFBridgingRelease (ABMultiValueCopyArrayOfAllValues (emailProperty); for (int index = 0; index <emailArray. count; index ++) {NSString * email = [emailArray objectAtIndex: index]; NSString * emailLabel = CFBridgingRelease (ABMultiValueCopyLabelAtIndex (emailProperty, index )); // determine the current value Tag if ([emailLabel isw.tostring :( NSString *) kABWorkLabel]) {NSLog (@ "% @", email );}}}

Get contact image:
// Obtain the contact's image-(void) setImage {CFErrorRef error = NULL; ABAddressBookRef addressBook = abaddressbookcreatewitexceptions (NULL, & error); ABRecordRef cecordRef = contacts (addressBook, [personIDASNumber intValue]); // if (ABPersonHasImageData (cecordRef) {// obtain photo data NSData * photoData = CFBridgingRelease (ABPersonCopyImageData (cecordRef); self. imageView. image = [UIImage imageWithData: photoData] ;}}






IOS obtains the mobile phone address book. If a person has two numbers, how can he obtain and add them to the table?

Qq synchronization assistant, the operation is very simple.
 
How does ios6 obtain the address book?

How can a mobile phone with its own address book disappear? 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.