天天看點

iOS開發實戰之app擷取通訊錄(iOS 9)

在做通訊類APP的時候,時常會通路到手機的通訊錄,來擷取聯系人的各種屬性,那麼本文就來讨論一下怎麼擷取通訊錄。

注意:iOS 9+版本上可以行,其他版本未試。

第一步:先導入系統庫 《Contacts.framework》

然後在.h上導入頭檔案

第二步:直接在.m檔案中寫代碼

//通訊錄存儲的管理類
CNContactStore * stroe = [[CNContactStore alloc]init];
//取出聯系人的請求類
    CNContactFetchRequest * request = [[CNContactFetchRequest alloc]initWithKeysToFetch:@[CNContactPhoneNumbersKey,CNContactGivenNameKey,CNContactFamilyNameKey]];
   //取出聯系人的block執行,有多少聯系人就執行多少次
    [stroe enumerateContactsWithFetchRequest:request error:nil usingBlock:^(CNContact * _Nonnull contact, BOOL * _Nonnull stop) {

        NSString * LinkName = [NSString stringWithFormat:@"%@%@",contact.familyName,contact.givenName];

        NSArray * parr = contact.phoneNumbers;

    }];
           

我們注意到請求類後面的參數是一個Array,這個Array代表的意思就是你要請求的東西,比如名字,号碼,位址,Email等等。參數有以下:

CONTACTS_EXTERN NSString * const CNContactPropertyNotFetchedExceptionName;

// Properties that are always fetched. Can be used with key value coding and observing.
CONTACTS_EXTERN NSString * const CNContactIdentifierKey                      NS_AVAILABLE(_11, _0);

// Optional properties that can be fetched. Can be used with key value coding and observing.
CONTACTS_EXTERN NSString * const CNContactNamePrefixKey                      NS_AVAILABLE(_11, _0);
CONTACTS_EXTERN NSString * const CNContactGivenNameKey                       NS_AVAILABLE(_11, _0);
CONTACTS_EXTERN NSString * const CNContactMiddleNameKey                      NS_AVAILABLE(_11, _0);
CONTACTS_EXTERN NSString * const CNContactFamilyNameKey                      NS_AVAILABLE(_11, _0);
CONTACTS_EXTERN NSString * const CNContactPreviousFamilyNameKey              NS_AVAILABLE(_11, _0);
CONTACTS_EXTERN NSString * const CNContactNameSuffixKey                      NS_AVAILABLE(_11, _0);
CONTACTS_EXTERN NSString * const CNContactNicknameKey                        NS_AVAILABLE(_11, _0);
CONTACTS_EXTERN NSString * const CNContactPhoneticGivenNameKey               NS_AVAILABLE(_11, _0);
CONTACTS_EXTERN NSString * const CNContactPhoneticMiddleNameKey              NS_AVAILABLE(_11, _0);
CONTACTS_EXTERN NSString * const CNContactPhoneticFamilyNameKey              NS_AVAILABLE(_11, _0);
CONTACTS_EXTERN NSString * const CNContactOrganizationNameKey                NS_AVAILABLE(_11, _0);
CONTACTS_EXTERN NSString * const CNContactDepartmentNameKey                  NS_AVAILABLE(_11, _0);
CONTACTS_EXTERN NSString * const CNContactJobTitleKey                        NS_AVAILABLE(_11, _0);
CONTACTS_EXTERN NSString * const CNContactBirthdayKey                        NS_AVAILABLE(_11, _0);
CONTACTS_EXTERN NSString * const CNContactNonGregorianBirthdayKey            NS_AVAILABLE(_11, _0);
CONTACTS_EXTERN NSString * const CNContactNoteKey                            NS_AVAILABLE(_11, _0);
CONTACTS_EXTERN NSString * const CNContactImageDataKey                       NS_AVAILABLE(_11, _0);
CONTACTS_EXTERN NSString * const CNContactThumbnailImageDataKey              NS_AVAILABLE(_11, _0);
CONTACTS_EXTERN NSString * const CNContactImageDataAvailableKey              NS_AVAILABLE(NA, _0);
CONTACTS_EXTERN NSString * const CNContactTypeKey                            NS_AVAILABLE(_11, _0);
CONTACTS_EXTERN NSString * const CNContactPhoneNumbersKey                    NS_AVAILABLE(_11, _0);
CONTACTS_EXTERN NSString * const CNContactEmailAddressesKey                  NS_AVAILABLE(_11, _0);
CONTACTS_EXTERN NSString * const CNContactPostalAddressesKey                 NS_AVAILABLE(_11, _0);
CONTACTS_EXTERN NSString * const CNContactDatesKey                           NS_AVAILABLE(_11, _0);
CONTACTS_EXTERN NSString * const CNContactUrlAddressesKey                    NS_AVAILABLE(_11, _0);
CONTACTS_EXTERN NSString * const CNContactRelationsKey                       NS_AVAILABLE(_11, _0);
CONTACTS_EXTERN NSString * const CNContactSocialProfilesKey                  NS_AVAILABLE(_11, _0);
CONTACTS_EXTERN NSString * const CNContactInstantMessageAddressesKey         NS_AVAILABLE(_11, _0);
           

你寫了哪些,請求對象就請求聯系人的哪些屬性,沒寫就不請求。。好了,選擇你要的屬性吧。

第三步:使用你取到的聯系人。

注意第二步裡的block中傳回的參數CNContact * _Nonnull contact,contact就是你取出來的聯系人對象,接下來就可以用起來了。注意的是聯系号碼屬性是一個Array,因為有多個号碼的可能性,是以用的時候注意一下。

繼續閱讀