關(guān)于通訊錄的操作,系統(tǒng)提供了2個(gè)框架:
AddressBookUI.framework
主要提供了通訊錄操作的相關(guān)界面API
AddressBook.framework
主要提供了與通訊錄數(shù)據(jù)庫(kù)交互的API
在iOS9.0以后系統(tǒng)新添加了Contacts.framework
、ContactsUI.framework
2個(gè)框架,這兩個(gè)框架可以實(shí)現(xiàn)以上兩個(gè)框架的所有功能。
接下來(lái)主要介紹AddressBook.framework
和新添加的Contacts.framework
框架
申請(qǐng)通訊錄授權(quán)
#import <AddressBook/AddressBook.h>
ABAddressBookRef addressRef = ABAddressBookCreateWithOptions(nil, nil);
ABAddressBookRequestAccessWithCompletion(addressRef, ^(bool granted, CFErrorRef error) {
if (granted) {
// 授權(quán)成功
}else{
// 授權(quán)失敗
}
});
執(zhí)行以上代碼后,系統(tǒng)會(huì)自動(dòng)彈出提示框,讓用戶進(jìn)行授權(quán)。如果用戶拒絕,再次調(diào)用以上代碼則不彈出提示框,只有在用戶手機(jī)的設(shè)置中更改權(quán)限。
Contacts框架的實(shí)現(xiàn)
#import <Contacts/Contacts.h>
CNContactStore * store = [[CNContactStore alloc] init];
[store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
// 授權(quán)成功
}else{
// 授權(quán)失敗
}
}];
通訊錄權(quán)限狀態(tài)
通過(guò)以下函數(shù)獲得通訊錄權(quán)限狀態(tài)
ABAddressBookGetAuthorizationStatus()
狀態(tài)枚舉如下
typedef CF_ENUM(CFIndex, ABAuthorizationStatus) {
// 用戶還沒(méi)有決定是否授權(quán)你的程序進(jìn)行訪問(wèn)
kABAuthorizationStatusNotDetermined = 0, // deprecated, use CNAuthorizationStatusNotDetermined
// iOS設(shè)備上的家長(zhǎng)控制或其它一些許可配置阻止程序與通訊錄數(shù)據(jù)庫(kù)進(jìn)行交互
kABAuthorizationStatusRestricted, // deprecated, use CNAuthorizationStatusRestricted
// 用戶明確的拒絕了你的程序?qū)νㄓ嶄浀脑L問(wèn)
kABAuthorizationStatusDenied, // deprecated, use CNAuthorizationStatusDenied
// 用戶已經(jīng)授權(quán)給你的程序?qū)νㄓ嶄涍M(jìn)行訪問(wèn)
kABAuthorizationStatusAuthorized // deprecated, use CNAuthorizationStatusAuthorized
} AB_DEPRECATED("use CNAuthorizationStatus");
AB_EXTERN ABAuthorizationStatus ABAddressBookGetAuthorizationStatus(void) AB_DEPRECATED("use [CNContactStore authorizationStatusForEntityType:]");
Contacts框架的實(shí)現(xiàn)
CNAuthorizationStatus
權(quán)限狀態(tài)
CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
if (status == CNAuthorizationStatusRestricted || status == CNAuthorizationStatusDenied) {
// 未授權(quán)
// do something...
}
typedef NS_ENUM(NSInteger, CNAuthorizationStatus)
{
/*! The user has not yet made a choice regarding whether the application may access contact data. */
CNAuthorizationStatusNotDetermined = 0,
/*! The application is not authorized to access contact data.
* The user cannot change this application’s status, possibly due to active restrictions such as parental controls being in place. */
CNAuthorizationStatusRestricted,
/*! The user explicitly denied access to contact data for the application. */
CNAuthorizationStatusDenied,
/*! The application is authorized to access contact data. */
CNAuthorizationStatusAuthorized
} NS_ENUM_AVAILABLE(10_11, 9_0);
獲取所有聯(lián)系人
CFArrayRef dataArr = ABAddressBookCopyArrayOfAllPeople(addressRef);
for (int i = 0; i < CFArrayGetCount(dataArr); i ++) {
ABRecordRef person = CFArrayGetValueAtIndex(dataArr, i);
/*得到聯(lián)系人對(duì)象,后續(xù)解析都是基于person對(duì)象進(jìn)行操作*/
}
Contacts框架的實(shí)現(xiàn)
CNContactFetchRequest * request = [[CNContactFetchRequest alloc] initWithKeysToFetch:@[CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey]];
[store enumerateContactsWithFetchRequest:request error:nil usingBlock:^(CNContact * _Nonnull contact, BOOL * _Nonnull stop) {
// block會(huì)不斷調(diào)用...
//(contact參數(shù),在下文獲取聯(lián)系人各種屬性中會(huì)用到)
}];
聯(lián)系人屬性以及獲取
每個(gè)聯(lián)系人對(duì)象都有很多屬性,有的是單一屬性,我們可以直接通過(guò)key
來(lái)獲取,像姓、名等。有的屬性比較復(fù)雜可能需要我們進(jìn)行多次解析才能獲取到我們想要的值,比如多重屬性:號(hào)碼、郵件、地址等。
單一屬性
對(duì)于單一屬性我們可以這樣直接獲取:
// 姓
ABRecordCopyValue(person, kABPersonLastNameProperty);
// 名
ABRecordCopyValue(person, kABPersonFirstNameProperty);
ABRecordCopyValue()
函數(shù)返回值是CFStringRef
類型,我們可以通過(guò)橋接的方式轉(zhuǎn)換NSString * str = (__bridge NSString *)(ABRecordCopyValue())
,這里不講解關(guān)于CoreFoundation
與 Foundation
橋接的知識(shí)。
// Property keys
/*名*/
AB_EXTERN const ABPropertyID kABPersonFirstNameProperty AB_DEPRECATED("use CNContact.givenName"); // First name - kABStringPropertyType
/*姓*/
AB_EXTERN const ABPropertyID kABPersonLastNameProperty AB_DEPRECATED("use CNContact.familyName"); // Last name - kABStringPropertyType
/*中間名*/
AB_EXTERN const ABPropertyID kABPersonMiddleNameProperty AB_DEPRECATED("use CNContact.middleName"); // Middle name - kABStringPropertyType
/*前綴*/
AB_EXTERN const ABPropertyID kABPersonPrefixProperty AB_DEPRECATED("use CNContact.namePrefix"); // Prefix ("Sir" "Duke" "General") - kABStringPropertyType
/*后綴*/
AB_EXTERN const ABPropertyID kABPersonSuffixProperty AB_DEPRECATED("use CNContact.nameSuffix"); // Suffix ("Jr." "Sr." "III") - kABStringPropertyType
/*昵稱*/
AB_EXTERN const ABPropertyID kABPersonNicknameProperty AB_DEPRECATED("use CNContact.nickname"); // Nickname - kABStringPropertyType
/*名字拼音或音標(biāo)*/
AB_EXTERN const ABPropertyID kABPersonFirstNamePhoneticProperty AB_DEPRECATED("use CNContact.phoneticGivenName"); // First name Phonetic - kABStringPropertyType
/*姓氏拼音或音標(biāo)*/
AB_EXTERN const ABPropertyID kABPersonLastNamePhoneticProperty AB_DEPRECATED("use CNContact.phoneticFamilyName"); // Last name Phonetic - kABStringPropertyType
/*中間名拼音或音標(biāo)*/
AB_EXTERN const ABPropertyID kABPersonMiddleNamePhoneticProperty AB_DEPRECATED("use CNContact.phoneticMiddleName"); // Middle name Phonetic - kABStringPropertyType
/*公司*/
AB_EXTERN const ABPropertyID kABPersonOrganizationProperty AB_DEPRECATED("use CNContact.organizationName"); // Company name - kABStringPropertyType
/*部門*/
AB_EXTERN const ABPropertyID kABPersonDepartmentProperty AB_DEPRECATED("use CNContact.departmentName"); // Department name - kABStringPropertyType
/*職位*/
AB_EXTERN const ABPropertyID kABPersonJobTitleProperty AB_DEPRECATED("use CNContact.jobTitle"); // Job Title - kABStringPropertyType
/*生日*/
AB_EXTERN const ABPropertyID kABPersonBirthdayProperty AB_DEPRECATED("use CNContact.birthday"); // Birthday associated with this person - kABDateTimePropertyType
/*備注*/
AB_EXTERN const ABPropertyID kABPersonNoteProperty AB_DEPRECATED("use CNContact.note"); // Note - kABStringPropertyType
/*這條聯(lián)系人信息的創(chuàng)建時(shí)間*/
AB_EXTERN const ABPropertyID kABPersonCreationDateProperty AB_DEPRECATED(""); // Creation Date (when first saved)
/*這條聯(lián)系人信息的最后保存時(shí)間*/
AB_EXTERN const ABPropertyID kABPersonModificationDateProperty AB_DEPRECATED(""); // Last saved date
Contacts框架的實(shí)現(xiàn)
在上文block中得到的contact
對(duì)象,我們可以直接通過(guò)點(diǎn)語(yǔ)法獲取相關(guān)屬性。
// 姓
contact.familyName
// 名
contact.givenName
以上是獲取姓氏、名字的方法,更多的屬性可以對(duì)照上文中的ABPropertyID
這里不再一一列舉。
多重屬性
- 號(hào)碼
// Phone numbers
AB_EXTERN const ABPropertyID kABPersonPhoneProperty AB_DEPRECATED("use CNContact.phoneNumbers"); // Generic phone number - kABMultiStringPropertyType
/*手機(jī)*/
AB_EXTERN const CFStringRef kABPersonPhoneMobileLabel AB_DEPRECATED("use CNLabelPhoneNumberMobile");
/*iPhone*/
AB_EXTERN const CFStringRef kABPersonPhoneIPhoneLabel AB_DEPRECATED("use CNLabelPhoneNumberiPhone");
/*主要*/
AB_EXTERN const CFStringRef kABPersonPhoneMainLabel AB_DEPRECATED("use CNLabelPhoneNumberMain");
/*住宅*/
AB_EXTERN const CFStringRef kABPersonPhoneHomeFAXLabel AB_DEPRECATED("use CNLabelPhoneNumberHomeFax");
/*工作*/
AB_EXTERN const CFStringRef kABPersonPhoneWorkFAXLabel AB_DEPRECATED("use CNLabelPhoneNumberWorkFax");
/*其他*/
AB_EXTERN const CFStringRef kABPersonPhoneOtherFAXLabel AB_DEPRECATED("use CNLabelPhoneNumberOtherFax");
/**/
AB_EXTERN const CFStringRef kABPersonPhonePagerLabel AB_DEPRECATED("use CNLabelPhoneNumberPager");
ABMultiValueRef phoneNos = ABRecordCopyValue(person, kABPersonPhoneProperty);
NSArray* phoneNosArr = CFBridgingRelease(ABMultiValueCopyArrayOfAllValues(phoneNos));
for(int i = 0; i< phoneNosArr.count; i++){
// 號(hào)碼類型(包括:住宅、工作、iPhone、手機(jī)、主要、住宅傳真、工作傳真、傳呼、其他)
NSString * phoneNoLabel = CFBridgingRelease(ABMultiValueCopyLabelAtIndex(phoneNos, i));
// 號(hào)碼
NSString * phoneNo = [phoneNosArr objectAtIndex:i];
NSLog(@"-PhoneNo--%@:%@-",phoneNoLabel,phoneNo);
}
- 郵件
/*郵件*/
AB_EXTERN const ABPropertyID kABPersonEmailProperty AB_DEPRECATED("use CNContact.emailAddresses"); // Email(s) - kABMultiStringPropertyType
ABMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);
NSArray* emailsArr = CFBridgingRelease(ABMultiValueCopyArrayOfAllValues(emails));
for(int i = 0; i< emailsArr.count; i++){
// 郵件類型(包括:住宅、工作、iCloud、其他)
NSString * emailLabel = CFBridgingRelease(ABMultiValueCopyLabelAtIndex(emails, i));
// 郵件地址
NSString * email = [emailsArr objectAtIndex:i];
NSLog(@"-email--%@:%@-",emailLabel,email);
}
- 地址
// Addresses
AB_EXTERN const ABPropertyID kABPersonAddressProperty AB_DEPRECATED("use CNContact.postalAddresses"); // Street address - kABMultiDictionaryPropertyType
/*街道*/
AB_EXTERN const CFStringRef kABPersonAddressStreetKey AB_DEPRECATED("use CNPostalAddress.street");
/*城市*/
AB_EXTERN const CFStringRef kABPersonAddressCityKey AB_DEPRECATED("use CNPostalAddress.city");
/*省份、州*/
AB_EXTERN const CFStringRef kABPersonAddressStateKey AB_DEPRECATED("use CNPostalAddress.state");
/*郵政編碼*/
AB_EXTERN const CFStringRef kABPersonAddressZIPKey AB_DEPRECATED("use CNPostalAddress.postalCode");
/*國(guó)家*/
AB_EXTERN const CFStringRef kABPersonAddressCountryKey AB_DEPRECATED("use CNPostalAddress.country");
/*國(guó)家編碼*/
AB_EXTERN const CFStringRef kABPersonAddressCountryCodeKey AB_DEPRECATED("use CNPostalAddress.ISOCountryCode");
ABMultiValueRef addresses = ABRecordCopyValue(person, kABPersonAddressProperty);
NSArray* addressesArr = CFBridgingRelease(ABMultiValueCopyArrayOfAllValues(addresses));
for(int i = 0; i< addressesArr.count; i++){
// 地址類型(包括:住宅、工作、其他)
NSString * addressLabel = CFBridgingRelease(ABMultiValueCopyLabelAtIndex(addresses, i));
// 地址(注意這里得到的是字典類型,其中包括了:City、Country、CountryCode、State、Street、ZIP等信息)
NSDictionary * address = [addressesArr objectAtIndex:i];
NSLog(@"-Address--%@:%@-",addressLabel,address);
}
- 日期
// Dates
AB_EXTERN const ABPropertyID kABPersonDateProperty AB_DEPRECATED("use CNContact.dates"); // Dates associated with this person - kABMultiDatePropertyType
/* 紀(jì)念日*/
AB_EXTERN const CFStringRef kABPersonAnniversaryLabel AB_DEPRECATED("use CNLabelDateAnniversary");
ABMultiValueRef dates = ABRecordCopyValue(person, kABPersonDateProperty);
NSArray* datesArr = CFBridgingRelease(ABMultiValueCopyArrayOfAllValues(dates));
for(int i = 0; i< datesArr.count; i++){
// 日期類型(包括:紀(jì)念日、其他)
NSString * dateLabel = CFBridgingRelease(ABMultiValueCopyLabelAtIndex(dates, i));
// 日期
NSString * date = [datesArr objectAtIndex:i];
NSLog(@"-Date--%@:%@-",dateLabel,date);
}
- 即時(shí)信息
// IM
AB_EXTERN const ABPropertyID kABPersonInstantMessageProperty AB_DEPRECATED("use CNContact.instantMessageAddresses"); // Instant Messaging - kABMultiDictionaryPropertyType
AB_EXTERN const CFStringRef kABPersonInstantMessageServiceKey AB_DEPRECATED("use CNInstantMessageAddress.service"); // Service ("Yahoo", "Jabber", etc.)
/*Yahoo! Messenger*/
AB_EXTERN const CFStringRef kABPersonInstantMessageServiceYahoo AB_DEPRECATED("use CNInstantMessageServiceYahoo");
/*Jabber*/
AB_EXTERN const CFStringRef kABPersonInstantMessageServiceJabber AB_DEPRECATED("use CNInstantMessageServiceJabber");
/*MSN Messenger*/
AB_EXTERN const CFStringRef kABPersonInstantMessageServiceMSN AB_DEPRECATED("use CNInstantMessageServiceMSN");
/*ICQ*/
AB_EXTERN const CFStringRef kABPersonInstantMessageServiceICQ AB_DEPRECATED("use CNInstantMessageServiceICQ");
/*AIM*/
AB_EXTERN const CFStringRef kABPersonInstantMessageServiceAIM AB_DEPRECATED("use CNInstantMessageServiceAIM");
/*QQ*/
AB_EXTERN const CFStringRef kABPersonInstantMessageServiceQQ AB_DEPRECATED("use CNInstantMessageServiceQQ");
/*Google Talk*/
AB_EXTERN const CFStringRef kABPersonInstantMessageServiceGoogleTalk AB_DEPRECATED("use CNInstantMessageServiceGoogleTalk");
/*Skype*/
AB_EXTERN const CFStringRef kABPersonInstantMessageServiceSkype AB_DEPRECATED("use CNInstantMessageServiceSkype");
/*Facebook Messenger*/
AB_EXTERN const CFStringRef kABPersonInstantMessageServiceFacebook AB_DEPRECATED("use CNInstantMessageServiceFacebook");
/*Gadu-Gadu*/
AB_EXTERN const CFStringRef kABPersonInstantMessageServiceGaduGadu AB_DEPRECATED("use CNInstantMessageServiceGaduGadu");
ABMultiValueRef IMs = ABRecordCopyValue(person, kABPersonInstantMessageProperty);
NSArray* IMsArr = CFBridgingRelease(ABMultiValueCopyArrayOfAllValues(IMs));
for(int i = 0; i< IMsArr.count; i++){
// 即時(shí)信息類型(包括:QQ、Skype、MSN Messenger、Google Talk、Facebook Messenger、AIM、Yahoo! Messenger、ICQ、Jabber、Gadu-Gadu)
NSString * IMLabel = CFBridgingRelease(ABMultiValueCopyLabelAtIndex(IMs, i));
// 即時(shí)信息賬號(hào)(字典中包括:service、username)
NSDictionary * IM = [IMsArr objectAtIndex:i];
NSLog(@"-IM--%@:%@-",IMLabel,IM);
}
- 關(guān)聯(lián)人
// Related names
AB_EXTERN const ABPropertyID kABPersonRelatedNamesProperty AB_DEPRECATED("use CNContact.contactRelations"); // Names - kABMultiStringPropertyType
/*父親*/
AB_EXTERN const CFStringRef kABPersonFatherLabel AB_DEPRECATED("use CNLabelContactRelationFather"); // Father
/*母親*/
AB_EXTERN const CFStringRef kABPersonMotherLabel AB_DEPRECATED("use CNLabelContactRelationMother"); // Mother
/*父母*/
AB_EXTERN const CFStringRef kABPersonParentLabel AB_DEPRECATED("use CNLabelContactRelationParent"); // Parent
/*兄弟*/
AB_EXTERN const CFStringRef kABPersonBrotherLabel AB_DEPRECATED("use CNLabelContactRelationBrother"); // Brother
/*姐妹*/
AB_EXTERN const CFStringRef kABPersonSisterLabel AB_DEPRECATED("use CNLabelContactRelationSister"); // Sister
/*子女*/
AB_EXTERN const CFStringRef kABPersonChildLabel AB_DEPRECATED("use CNLabelContactRelationChild"); // Child
/*朋友*/
AB_EXTERN const CFStringRef kABPersonFriendLabel AB_DEPRECATED("use CNLabelContactRelationFriend"); // Friend
/*配偶*/
AB_EXTERN const CFStringRef kABPersonSpouseLabel AB_DEPRECATED("use CNLabelContactRelationSpouse"); // Spouse
/*伴侶*/
AB_EXTERN const CFStringRef kABPersonPartnerLabel AB_DEPRECATED("use CNLabelContactRelationPartner"); // Partner
/*助理*/
AB_EXTERN const CFStringRef kABPersonAssistantLabel AB_DEPRECATED("use CNLabelContactRelationAssistant"); // Assistant
/*上司*/
AB_EXTERN const CFStringRef kABPersonManagerLabel AB_DEPRECATED("use CNLabelContactRelationManager"); // Manager
ABMultiValueRef relations = ABRecordCopyValue(person, kABPersonRelatedNamesProperty);
NSArray* relationsArr = CFBridgingRelease(ABMultiValueCopyArrayOfAllValues(relations));
for(int i = 0; i< relationsArr.count; i++){
// 關(guān)聯(lián)人類型(包括:父親、母親、父母、兄弟、姐妹、子女、朋友、配偶、伴侶、助理、上司、其他)
NSString * relationLabel = CFBridgingRelease(ABMultiValueCopyLabelAtIndex(relations, i));
// 關(guān)聯(lián)人
NSString * relation = [relationsArr objectAtIndex:i];
NSLog(@"-Relation--%@:%@-",relationLabel,relation);
}
- 個(gè)人資料
// Social Profile
AB_EXTERN const ABPropertyID kABPersonSocialProfileProperty AB_DEPRECATED("use CNContact.socialProfiles"); // kABMultiDictionaryPropertyType
AB_EXTERN const CFStringRef kABPersonSocialProfileURLKey AB_DEPRECATED("use CNSocialProfile.urlString"); // string representation of a url for the social profile
// the following properties are optional
AB_EXTERN const CFStringRef kABPersonSocialProfileServiceKey AB_DEPRECATED("use CNSocialProfile.service"); // string representing the name of the service (Twitter, Facebook, LinkedIn, etc.)
AB_EXTERN const CFStringRef kABPersonSocialProfileUsernameKey AB_DEPRECATED("use CNSocialProfile.username"); // string representing the user visible name
AB_EXTERN const CFStringRef kABPersonSocialProfileUserIdentifierKey AB_DEPRECATED("use CNSocialProfile.userIdentifier"); // string representing the service specific identifier (optional)
/*Twitter*/
AB_EXTERN const CFStringRef kABPersonSocialProfileServiceTwitter AB_DEPRECATED("use CNSocialProfileServiceTwitter");
/*新浪微博*/
AB_EXTERN const CFStringRef kABPersonSocialProfileServiceSinaWeibo AB_DEPRECATED("use CNSocialProfileServiceSinaWeibo");
/*GameCenter*/
AB_EXTERN const CFStringRef kABPersonSocialProfileServiceGameCenter AB_DEPRECATED("use CNSocialProfileServiceGameCenter");
/*Facebook*/
AB_EXTERN const CFStringRef kABPersonSocialProfileServiceFacebook AB_DEPRECATED("use CNSocialProfileServiceFacebook");
/*Myspace*/
AB_EXTERN const CFStringRef kABPersonSocialProfileServiceMyspace AB_DEPRECATED("use CNSocialProfileServiceMySpace");
/*LinkedIn*/
AB_EXTERN const CFStringRef kABPersonSocialProfileServiceLinkedIn AB_DEPRECATED("use CNSocialProfileServiceLinkedIn");
/*Flickr*/
AB_EXTERN const CFStringRef kABPersonSocialProfileServiceFlickr AB_DEPRECATED("use CNSocialProfileServiceFlickr");
ABMultiValueRef socialProfiles = ABRecordCopyValue(person, kABPersonSocialProfileProperty);
NSArray* socialProfilesArr = CFBridgingRelease(ABMultiValueCopyArrayOfAllValues(socialProfiles));
for(int i = 0; i< socialProfilesArr.count; i++){
// 個(gè)人資料類型(包括:新浪微博、Twitter、Facebook、Flickr、LinkedIn、MySpace)
NSString * socialProfileLabel = CFBridgingRelease(ABMultiValueCopyLabelAtIndex(socialProfiles, i));
// 賬號(hào)信息(字典中包括:service、url、username)
NSDictionary * socialProfile = [socialProfilesArr objectAtIndex:i];
NSLog(@"-SocialProfile--%@:%@-",socialProfileLabel,socialProfile);
}
Contacts框架對(duì)應(yīng)屬性
/*號(hào)碼數(shù)組*/
@property (copy, NS_NONATOMIC_IOSONLY) NSArray<CNLabeledValue<CNPhoneNumber*>*> *phoneNumbers;
/*郵件數(shù)組*/
@property (copy, NS_NONATOMIC_IOSONLY) NSArray<CNLabeledValue<NSString*>*> *emailAddresses;
/*郵政地址*/
@property (copy, NS_NONATOMIC_IOSONLY) NSArray<CNLabeledValue<CNPostalAddress*>*> *postalAddresses;
/*url數(shù)組*/
@property (copy, NS_NONATOMIC_IOSONLY) NSArray<CNLabeledValue<NSString*>*> *urlAddresses;
/*關(guān)聯(lián)人數(shù)組*/
@property (copy, NS_NONATOMIC_IOSONLY) NSArray<CNLabeledValue<CNContactRelation*>*> *contactRelations;
/*個(gè)人資料數(shù)組*/
@property (copy, NS_NONATOMIC_IOSONLY) NSArray<CNLabeledValue<CNSocialProfile*>*> *socialProfiles;
/*即時(shí)信息賬號(hào)數(shù)組*/
@property (copy, NS_NONATOMIC_IOSONLY) NSArray<CNLabeledValue<CNInstantMessageAddress*>*> *instantMessageAddresses;
/*! @abstract The Gregorian birthday.
*
* @description Only uses day, month and year components. Needs to have at least a day and a month.
*/
@property (copy, nullable, NS_NONATOMIC_IOSONLY) NSDateComponents *birthday;
/*! @abstract The alternate birthday (Lunisolar).
*
* @description Only uses day, month, year and calendar components. Needs to have at least a day and a month. Calendar must be Chinese, Hebrew or Islamic.
*/
@property (copy, nullable, NS_NONATOMIC_IOSONLY) NSDateComponents *nonGregorianBirthday;
/*! @abstract Other Gregorian dates (anniversaries, etc).
*
* @description Only uses day, month and year components. Needs to have at least a day and a month.
*/
@property (copy, NS_NONATOMIC_IOSONLY) NSArray<CNLabeledValue<NSDateComponents*>*> *dates;
頭像
判斷是否有頭像
if (ABPersonHasImageData(person) == YES) {
NSLog(@"有頭像");
}else{
NSLog(@"無(wú)頭像");
}
頭像獲取
CFDataRef imgRef = ABPersonCopyImageData(person);
頭像設(shè)置
ABPersonSetImageData(person, imgRef, nil);
Contacts框架的實(shí)現(xiàn)
Contacts
框架中對(duì)于聯(lián)系人頭像的操作通過(guò)CNContact
對(duì)象的imageData
屬性進(jìn)行操作
contact.imageData
頭像設(shè)置的時(shí)候contact
對(duì)象的類型要是CNMutableContact
類型
添加聯(lián)系人
// 創(chuàng)建一個(gè)聯(lián)系人
ABRecordRef recordRef = ABPersonCreate();
// 設(shè)置屬性
ABRecordSetValue(recordRef, kABPersonLastNameProperty, @"姓", nil);
ABRecordSetValue(recordRef, kABPersonFirstNameProperty, @"名", nil);
// 將新建的聯(lián)系人添加到通訊錄
ABAddressBookAddRecord(addressRef, recordRef, nil);
// 保存通訊錄
ABAddressBookSave(addressRef, nil);
Contacts框架的實(shí)現(xiàn)
// 創(chuàng)建一個(gè)聯(lián)系人
CNMutableContact * contact = [[CNMutableContact alloc] init];
// 設(shè)置屬性
contact.familyName = @"姓1";
contact.givenName = @"名1";
// 創(chuàng)建一個(gè)保存請(qǐng)求對(duì)象
CNSaveRequest * request = [[CNSaveRequest alloc] init];
[request addContact:contact toContainerWithIdentifier:nil];
// 保存到通訊錄
[store executeSaveRequest:request error:nil];
以上的出錯(cuò)信息參數(shù)都設(shè)置為了nil
,大家在實(shí)際應(yīng)用的時(shí)候可以添加上,以便查看錯(cuò)誤信息。
版權(quán)聲明:出自MajorLMJ技術(shù)博客的原創(chuàng)作品 ,轉(zhuǎn)載時(shí)必須注明出處及相應(yīng)鏈接!