iOS把數(shù)字,號碼保存到系統(tǒng)通訊錄

首先你要引入兩個庫 在
![Upload 屏幕快照 2017-09-12 上午11.09.04.png failed. Please try again.]
引入之后
引入

import <ContactsUI/CNContactViewController.h>

import <ContactsUI/CNContactPickerViewController.h>

在遵循這個CNContactViewControllerDelegate,CNContactPickerDelegate,
代理
這里的代碼可以直接復(fù)制直接使用很是方便


- (void)saveNewContact{
    //1.創(chuàng)建Contact對象,必須是可變的
    CNMutableContact *contact = [[CNMutableContact alloc] init];
    //2.為contact賦值,setValue4Contact中會給出常用值的對應(yīng)關(guān)系
    [self setValue4Contact:contact existContect:NO];
    //3.創(chuàng)建新建好友頁面
    CNContactViewController *controller = [CNContactViewController viewControllerForNewContact:contact];
    //代理內(nèi)容根據(jù)自己需要實現(xiàn)
    controller.delegate = self;
    //4.跳轉(zhuǎn)
    UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:controller];
    [self presentViewController:navigation animated:YES completion:^{
        
    }];
    
}

//保存現(xiàn)有聯(lián)系人實現(xiàn)
- (void)saveExistContact{
    //1.跳轉(zhuǎn)到聯(lián)系人選擇頁面,注意這里沒有使用UINavigationController
    CNContactPickerViewController *controller = [[CNContactPickerViewController alloc] init];
    controller.delegate = self;
    [self presentViewController:controller animated:YES completion:^{
        
    }];
}

- (void)contactViewController:(CNContactViewController *)viewController didCompleteWithContact:(nullable CNContact *)contact{
    [viewController dismissViewControllerAnimated:YES completion:^{
        
    }];
}

#pragma mark - CNContactPickerDelegate
//2.實現(xiàn)點選的代理,其他代理方法根據(jù)自己需求實現(xiàn)
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact{
    [picker dismissViewControllerAnimated:YES completion:^{
        //3.copy一份可寫的Contact對象,不要嘗試alloc一類,mutableCopy獨此一家
        CNMutableContact *c = [contact mutableCopy];
        //4.為contact賦值
        [self setValue4Contact:c existContect:YES];
        //5.跳轉(zhuǎn)到新建聯(lián)系人頁面
        CNContactViewController *controller = [CNContactViewController viewControllerForNewContact:c];
        controller.delegate = self;
        UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:controller];
        [self presentViewController:navigation animated:YES completion:^{
        }];
    }];
}


//設(shè)置要保存的contact對象
- (void)setValue4Contact:(CNMutableContact *)contact existContect:(BOOL)exist{
    if (!exist) {
        //名字和頭像
        contact.namePrefix = @"3333";
        contact.nickname = @"oriccheng";
        //        UIImage *logo = [UIImage imageNamed:@"..."];
        //        NSData *dataRef = UIImagePNGRepresentation(logo);
        //        contact.imageData = dataRef;
    }
    //電話,每一個CNLabeledValue都是有講究的,如何批評,可以在頭文件里面查找,這里是給出幾個常用的
    CNLabeledValue *phoneNumber = [CNLabeledValue labeledValueWithLabel:CNLabelPhoneNumberMobile value:[CNPhoneNumber phoneNumberWithStringValue:@"18888888888"]];
    if (!exist) {
        contact.phoneNumbers = @[phoneNumber];
    }
    //現(xiàn)有聯(lián)系人情況
    else{
        if ([contact.phoneNumbers count] >0) {
            NSMutableArray *phoneNumbers = [[NSMutableArray alloc] initWithArray:contact.phoneNumbers];
            [phoneNumbers addObject:phoneNumber];
            contact.phoneNumbers = phoneNumbers;
        }else{
            contact.phoneNumbers = @[phoneNumber];
        }
    }
    
    //網(wǎng)址:CNLabeledValue *url = [CNLabeledValue labeledValueWithLabel:@"" value:@""];
    //郵箱:CNLabeledValue *mail = [CNLabeledValue labeledValueWithLabel:CNLabelWork value:self.poiData4Save.mail];
    
    //特別說一個地址,PostalAddress對應(yīng)的才是地址
    CNMutablePostalAddress *address = [[CNMutablePostalAddress alloc] init];
    address.state = @"遼寧省";
    address.city = @"沈陽市";
    address.postalCode = @"111111";
    //外國人好像都不強調(diào)區(qū)的概念,和具體地址拼到一起
    address.street = @"沈河區(qū)惠工街10號";
    //生成的上面地址的CNLabeledValue,其中可以設(shè)置類型CNLabelWork等等
    CNLabeledValue *addressLabel = [CNLabeledValue labeledValueWithLabel:CNLabelWork value:address];
    if (!exist) {
        contact.postalAddresses = @[addressLabel];
    }else{
        if ([contact.postalAddresses count] >0) {
            NSMutableArray *addresses = [[NSMutableArray alloc] initWithArray:contact.postalAddresses];
            [addresses addObject:addressLabel];
            contact.postalAddresses = addresses;
        }else{
            contact.postalAddresses = @[addressLabel];
        }
    }
}


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,537評論 19 139
  • 總結(jié)自己在開發(fā)過程中遇到的BUG ![第12個錯誤]](http://upload-images.jianshu....
    Mg明明就是你閱讀 1,404評論 2 4
  • JavaScript是什么? JS是一種小型的、輕量級的、面向?qū)ο蟮?、跨平臺的客戶端腳本語言。JS是嵌入到瀏覽器軟...
    鉤不了沉閱讀 2,036評論 0 6
  • 我是一名故事家和旅行者,對,就是講故事的人。輾轉(zhuǎn)在旅程的這里和那里。 看過很多風(fēng)景,做過很多夢。 當(dāng)然也認(rèn)識了很多...
    兔子少年閱讀 375評論 1 2
  • Ceph Monitor集群作為Ceph中的元信息管理組件,基于改進(jìn)的Paxos算法,對外提供一致性的元信息訪問和...
    CatKang閱讀 3,246評論 0 5

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