iOS添加和修改聯(lián)系人到通訊錄

iOS9.0新增了通訊錄類(lèi),按照一般教程使用即可完成通常的開(kāi)發(fā)任務(wù)。在實(shí)際工作中,遇到了一個(gè)問(wèn)題,如果通訊錄中存在某一個(gè)指定的姓名的聯(lián)系人,需要修改其聯(lián)系方式為XXXX-XXXXXX,不存在則創(chuàng)建這樣的一個(gè)聯(lián)系人。API中提供了updateContact方法,但直接調(diào)用會(huì)出現(xiàn)崩潰,原因是CNMutableContact的phoneNumbers的屬性為只讀,只有為空時(shí)才可以賦值。
所以解決這個(gè)問(wèn)題的思路改為:如果存在該指定姓名的聯(lián)系人時(shí),先刪除此聯(lián)系人,然后創(chuàng)建一個(gè)新的。

一、添加頭文件

#import <ContactsUI/ContactsUI.h>

二、常用API函數(shù)

2.1 獲取是否授權(quán)

//獲取是否授權(quán)
- (BOOL)getContactAuthorize:(CNContactStore *)contactStore {
    __block BOOL grandted = YES;
    [contactStore requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
        if (granted == YES) {
            grandted = YES;
        }else {
            grandted = NO;
        }
    }];
    return grandted;
}

2.2 創(chuàng)建聯(lián)系人

/**
 創(chuàng)建客服信息
 */
- (CNMutableContact *)crateServiceContact {
    CNMutableContact *contact = [[CNMutableContact alloc] init];
    contact.organizationName = KContactOrganizationName;
    CNPhoneNumber *mobileNumber = [[CNPhoneNumber alloc] initWithStringValue:ServerPhoneNumber];
    CNLabeledValue *mobilePhone = [[CNLabeledValue alloc] initWithLabel:CNLabelPhoneNumberMobile value:mobileNumber];
    contact.phoneNumbers = @[mobilePhone];
    return contact;
}

2.3 添加聯(lián)系人

/**
 添加聯(lián)系人
 */
- (void)addContact:(CNMutableContact *)contact {
    // 創(chuàng)建聯(lián)系人請(qǐng)求
    CNSaveRequest *saveRequest = [[CNSaveRequest alloc] init];
    [saveRequest addContact:contact toContainerWithIdentifier:nil];
    // 寫(xiě)入聯(lián)系人
    CNContactStore *store = [[CNContactStore alloc] init];
    [store executeSaveRequest:saveRequest error:nil];
}

2.4 刪除聯(lián)系人

/**
 刪除客服信息
 */
- (void)deleteContact:(CNMutableContact *)contact{
    // 創(chuàng)建聯(lián)系人請(qǐng)求
    CNSaveRequest *saveRequest = [[CNSaveRequest alloc] init];
    [saveRequest deleteContact:contact];
    // 寫(xiě)入操作
    CNContactStore *store = [[CNContactStore alloc] init];
    [store executeSaveRequest:saveRequest error:nil];
}

2.5判斷指定姓名聯(lián)系人是否存在

/**
 判斷是否存在指定聯(lián)系人
 */
- (CNContact *)isExitContact:(NSString *)organizationName {
    CNContactStore *store = [[CNContactStore alloc] init];
    //檢索條件
    NSPredicate *predicate = [CNContact predicateForContactsMatchingName:organizationName];
    //過(guò)濾的條件
    NSArray *keysToFetch = @[CNContactEmailAddressesKey, [CNContactFormatter descriptorForRequiredKeysForStyle:CNContactFormatterStyleFullName]];
    NSArray *contact = [store unifiedContactsMatchingPredicate:predicate keysToFetch:keysToFetch error:nil];
    return [contact firstObject];
}

2.6 更新聯(lián)系人

/**
 更新聯(lián)系人
 */
- (void)updateContact:(CNMutableContact *)contact{
    // 創(chuàng)建聯(lián)系人請(qǐng)求
    CNSaveRequest *saveRequest = [[CNSaveRequest alloc] init];
    [saveRequest updateContact:contact];
    // 重新寫(xiě)入
    CNContactStore *store = [[CNContactStore alloc] init];
    NSError *error = nil;
     BOOL saveStatus = [store executeSaveRequest:saveRequest error:&error];
    if (saveStatus == NO) {
        NSLog(@"%@", error);
    }
    
}

三、使用

使用上,需要先獲取授權(quán)信息,然后走不同的路徑。

/**
 授權(quán)并新建聯(lián)系人到通訊錄
 */
+ (BOOL)createContact {
    ZTEventKitManager *manager = [ZTEventKitManager sharedManager];
    CNContactStore *contactStore = [[CNContactStore alloc] init];
    
    switch ([CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts]) {
        case CNAuthorizationStatusNotDetermined:{
            BOOL granted = [manager getContactAuthorize:contactStore];
            if (granted) {
                [manager updateContact];
                return YES;
            } else {
                return NO;
            }
        }
        case CNAuthorizationStatusAuthorized:{
            [manager updateContact];
            return YES;
        }
        default:
            return NO;
            break;
    }
}

其中updateContact函數(shù),作用是先判斷是否存在指定的聯(lián)系人如果存在,先刪除然后創(chuàng)建新的。

//更新聯(lián)系人信息
- (void)updateContact {
    ZTEventKitManager *manager = [ZTEventKitManager sharedManager];
    CNContact *contact = [manager isExitContact:KContactOrganizationName];
    if (contact) {
        CNMutableContact *contactOld= [contact mutableCopy];
        [manager deleteContact:contactOld];
        
        CNMutableContact *contactNew = [manager crateServiceContact];
        [manager addContact:contactNew];
        
    }else {
        CNMutableContact *contact = [manager crateServiceContact];
        [manager addContact:contact];
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 生活中經(jīng)常有這些現(xiàn)象:每天忙個(gè)不停卻不知道忙了什么;為了高一點(diǎn)的工資跳槽;天天經(jīng)常加班到深夜;應(yīng)酬不斷、肚子挺大,...
    Mypc閱讀 350評(píng)論 0 0
  • 那天好像第一次見(jiàn)你 對(duì)你的眼睛,我凝視了三秒 我以為我看見(jiàn)日月,星辰,大海 是全世界 但一切我以為的都是我以為的 ...
    悲苦執(zhí)念閱讀 242評(píng)論 0 0
  • 話(huà)說(shuō),九月近在眼前 想想剩下不到十天的假期 我的同學(xué)們已經(jīng)傷心欲絕 作為一名愛(ài)好學(xué)習(xí)的女大學(xué)生 我怎么可以貪戀紅塵...
    lovelylily444閱讀 1,093評(píng)論 0 6
  • 每到晚上感慨總是特別多,其實(shí)也不知道想些什么。 總覺(jué)得人生特別悲涼,總覺(jué)得只有自己一個(gè)人孤苦伶仃的存在著,...
    胡酴閱讀 330評(píng)論 0 0

友情鏈接更多精彩內(nèi)容