关闭

AddressBook

标签: 通讯录
446人阅读 评论(0) 收藏 举报
在Build Phases的Link Binary With Libraries中加两个框架ContactsUI.framework 和 Contacts.framework

Main.storyboard

#import "ViewController.h"
#import <ContactsUI/CNContactViewController.h>
#import <ContactsUI/CNContactPickerViewController.h>

@interface ViewController ()<CNContactViewControllerDelegate, CNContactPickerDelegate>

- (IBAction)newContact:(UIButton *)sender;
- (IBAction)currentContact:(UIButton *)sender;
//CNContactStore    是一个用来读取和保存联系人的新的类
@property(nonatomic, strong)CNContactStore *contactStore;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//      创建新的联系人
- (IBAction)newContact:(UIButton *)sender {
    [self saveNewContact];
}

- (IBAction)currentContact:(UIButton *)sender {
    [self saveExistContact];
}

//      保存新的联系人
- (void)saveNewContact{

    //      这个对象是用来配置联系人信息的,有可变的CNMutaleContact和CNContact,区别用来读取和创建联系人。CNContact对象中有许多属性,对应联系人的一些信息


    //  1.  创建Contact对象, 必须是可变的
    CNMutableContact *contact = [[CNMutableContact alloc] init];
    //  2.  创建新建好友页面
    CNContactViewController *controller = [CNContactViewController viewControllerForNewContact:contact];

    //  3.  代理内容根据自己需要实现
    controller.delegate = self;
    //  4.  跳转
    UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:controller];

    [self presentViewController:navigation animated:YES completion:^{
        //  5.  为contact赋值
        //      设置联系人头像
        contact.imageData = UIImagePNGRepresentation([UIImage imageNamed:@""]);
        //      设置联系人的昵称
        contact.nickname = @"";
        //      设置联系人名字
        contact.givenName = @"";
        //      设置联系人姓氏
        contact.familyName = @"";
        //      设置联系人邮箱(邮箱信息有多种, 可以任意选择, 点击类查看CNLabeledValue)
        CNLabeledValue *email = [CNLabeledValue labeledValueWithLabel:CNLabelHome value:@""];
        contact.emailAddresses = @[email];
        //      设置联系人电话号(电话信息有多种, 可以任意选择, 点击类查看CNPhoneNumber)
        //      电话号码, 可以通过CNPhoneNumber的+号类方法创建, 或者用初始化方法创建
        contact.phoneNumbers = @[[CNLabeledValue labeledValueWithLabel:CNLabelPhoneNumberiPhone value:[CNPhoneNumber phoneNumberWithStringValue:@""]]];
        //      设置联系人地址
        //      点击CNMutablePostalAddress查看设置地址详细信息
        CNMutablePostalAddress *address = [[CNMutablePostalAddress alloc] init];
        address.state = @"中国";
        //      设置生日
        NSDateComponents *birthday = [[NSDateComponents alloc] init];
        contact.birthday = birthday;
        //      创建添加联系人请求CNSaveRequest
        CNSaveRequest *save = [[CNSaveRequest alloc] init];
        //      添加联系人
        [save addContact:contact toContainerWithIdentifier:nil];
        //      写入联系人CNContactStore
        CNContactStore *store = [[CNContactStore alloc] init];
        [store executeSaveRequest:save error:nil];
    }];



/*
        //添加一个联系人
        - (void)addContact:(CNMutableContact *)contact toContainerWithIdentifier:(nullable NSString *)identifier;

        //更新一个联系人
        - (void)updateContact:(CNMutableContact *)contact;
        //删除一个联系人
        - (void)deleteContact:(CNMutableContact *)contact;
        //添加一组联系人
        - (void)addGroup:(CNMutableGroup *)group toContainerWithIdentifier:(nullable NSString *)identifier;
        //更新一组联系人
        - (void)updateGroup:(CNMutableGroup *)group;
        //删除一组联系人
        - (void)deleteGroup:(CNMutableGroup *)group;
        //向组中添加子组
        - (void)addSubgroup:(CNGroup *)subgroup toGroup:(CNGroup *)group NS_AVAILABLE(10_11, NA);
        //在组中删除子组
        - (void)removeSubgroup:(CNGroup *)subgroup fromGroup:(CNGroup *)group NS_AVAILABLE(10_11, NA);
        //向组中添加成员
        - (void)addMember:(CNContact *)contact toGroup:(CNGroup *)group;
        //向组中移除成员
        - (void)removeMember:(CNContact *)contact fromGroup:(CNGroup *)group;

*/
}

- (void)saveExistContact{
    //1.跳转到联系人选择页面,注意这里没有使用UINavigationController
    CNContactPickerViewController *controller = [[CNContactPickerViewController alloc] init];

    controller.delegate = self;

    [self presentViewController:controller animated:YES completion:^{

    }];
}


#pragma mark - CNContactViewControllerDelegate
- (void)contactViewController:(CNContactViewController *)viewController didCompleteWithContact:(nullable CNContact *)contact{
    [viewController dismissViewControllerAnimated:YES completion:^{

    }];
}

#pragma mark - CNContactPickerDelegate
//2.实现点选的代理,其他代理方法根据自己需求实现
- (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.跳转到新建联系人页面
        CNContactViewController *controller = [CNContactViewController viewControllerForNewContact:c];
        controller.delegate = self;
        UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:controller];
        [self presentViewController:navigation animated:YES completion:^{
        }];
    }];
}


//设置要保存的contact对象
- (void)setValue4Contact:(CNMutableContact *)contact existContect:(BOOL)exist{
            //      ContactFramework获取格式化的联系人姓名
            NSString *strName = [CNContactFormatter stringFromContact:contact style:CNContactFormatterStyleFullName];
            NSLog(@"%@", strName);

//    if (!exist) {
//        //名字和头像
//        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];
//    }
//    //现有联系人情况
//    else{
//        if ([contact.phoneNumbers count] >0) {
//            NSMutableArray *phoneNumbers = [[NSMutableArray alloc] initWithArray:contact.phoneNumbers];
//            [phoneNumbers addObject:phoneNumber];
//            contact.phoneNumbers = phoneNumbers;
//        }else{
//            contact.phoneNumbers = @[phoneNumber];
//        }
//    }
//    
//    //网址:CNLabeledValue *url = [CNLabeledValue labeledValueWithLabel:@"" value:@""];
//    //邮箱:CNLabeledValue *mail = [CNLabeledValue labeledValueWithLabel:CNLabelWork value:self.poiData4Save.mail];
//    
//    //特别说一个地址,PostalAddress对应的才是地址
//    CNMutablePostalAddress *address = [[CNMutablePostalAddress alloc] init];
//    address.state = @"辽宁省";
//    address.city = @"沈阳市";
//    address.postalCode = @"111111";
//    //外国人好像都不强调区的概念,所以和具体地址拼到一起
//    address.street = @"沈河区惠工街10号";
//    //生成的上面地址的CNLabeledValue,其中可以设置类型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];
//        }
//    }
}

@end
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:4274次
    • 积分:393
    • 等级:
    • 排名:千里之外
    • 原创:36篇
    • 转载:1篇
    • 译文:0篇
    • 评论:0条
    文章存档