iOS 使用ContactsUI保存新联系人到系统通讯录

iOS 9 中已经不建议使AddressBook来进行对系统通讯录的操作的,而是采用了新的库-ContactsUI.framework,通过它,我们可以访问系统通讯录,保存新的联系人,或者修改已有联系人的信息。代码如下,有不足之处还望大家积极指出。

//

//  ViewController.m

//  contacts

//

//  Created by WEIXIAOZHEN on 13/1/16.

//  Copyright © 2016 WEIXIAOZHEN. All rights reserved.

//


#import "ViewController.h"

#import <ContactsUI/CNContactViewController.h>

#import <ContactsUI/CNContactPickerViewController.h>



@interface ViewController ()<CNContactPickerDelegate,CNContactViewControllerDelegate>

@property(nonatomic, strong)CNContactViewController *controller;


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

  

    // Do any additional setup after loading the view, typically from a nib.

}


-(void)loadView{

    

    [super loadView];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

    button.frame = CGRectMake(100, 100, 150, 50);

    [button setTitle:@"修改保存现有联系人" forState:UIControlStateNormal];

    [self.view addSubview:button];

    [button addTarget:self action:@selector(saveExistContact) forControlEvents:UIControlEventTouchUpInside];

    

    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem];

    button2.frame = CGRectMake(100, 200, 150, 50);

    [button2 setTitle:@"保存新联系人" forState:UIControlStateNormal];

    [self.view addSubview:button2];

    [button2 addTarget:self action:@selector(saveNewContact) forControlEvents:UIControlEventTouchUpInside];

  

}


- (void)saveNewContact{

    

    //1.创建Contact对象,须是可变

    CNMutableContact *contact = [[CNMutableContact alloc] init];

    

    //2.contact赋值

    [self setValueForContact:contact existContect:NO];

    

    //3.创建新建联系人页面

   _controller = [CNContactViewController viewControllerForNewContact:contact];

    

    //代理内容根据自己需要实现

    _controller.delegate = self;

    

    //4.跳转

    UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:_controller];

    

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

        

    }];

    

   

  

}


//设置要保存的contact对象

- (void)setValueForContact:(CNMutableContact *)contact existContect:(BOOL)exist{

    

    if (!exist) {


        //名字和头像

        contact.nickname = @"xiaoming";

       UIImage *photo = [UIImage imageNamed:@"2.jpeg"];

       NSData *dataRef = UIImagePNGRepresentation(photo);

        contact.imageData = dataRef;

        contact.familyName = @"";

        contact.givenName = @"小明";

    

        //公司

        contact.organizationName = @"百度";

        

        //备注

        contact.note = @"jdfuenknfkdjgkjgadkgml";

        

        

        

 //        contact.socialProfiles = @[socialProfile];

//        CNInstantMessageAddress *im = [[CNInstantMessageAddress alloc]initWithUsername:@"wechat" service:CNInstantMessageServiceFacebook];

//         CNInstantMessageAddress *im2 = [[CNInstantMessageAddress alloc]initWithUsername:@"wechat" service:CNInstantMessageServiceQQ];

        

        //InstantMessage,个人信息

         CNInstantMessageAddress *IMSData = [[CNInstantMessageAddress alloc]initWithUsername:@"123456" service:@"wechat"];

        CNLabeledValue *IMS = [CNLabeledValue labeledValueWithLabel:nil value:IMSData];

        

        if (!exist) {

            contact.instantMessageAddresses = @[IMS];

        }else{

            if ([contact.instantMessageAddresses count] > 0) {

                NSMutableArray *imsArr = [[NSMutableArray alloc] initWithArray:contact.instantMessageAddresses];

                [imsArr addObject:IMS];

                contact.instantMessageAddresses = imsArr;

            }else{

                contact.instantMessageAddresses = @[IMS];

            }

        }


        

        

        

        //SocialProfile,即时信息

        CNSocialProfile *socialProfileData = [[CNSocialProfile alloc] initWithUrlString:@"http://ww.baidu.com" username:@"119" userIdentifier:@"119" service:@"weibo"];

        CNLabeledValue *socilaProfile = [CNLabeledValue labeledValueWithLabel:nil value:socialProfileData];

        if (!exist) {

            contact.socialProfiles = @[socilaProfile];

            

        }else{

            if ([contact.socialProfiles count] > 0) {

                NSMutableArray *socialProfileArr = [[NSMutableArray alloc] initWithArray:contact.socialProfiles];

                [socialProfileArr addObject:socilaProfile];

                contact.socialProfiles = socialProfileArr;

            }else{

                contact.socialProfiles = @[socilaProfile];

            }

        }


       

    }

    //电话

    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:CNLabelURLAddressHomePage value:@"http://www.baidu.com"];

   

       contact.urlAddresses = @[url];

    

    

    

    

    //邮箱:

    CNLabeledValue *mail = [CNLabeledValue labeledValueWithLabel:CNLabelWork value:@"gha@163.com"];

    

             contact.urlAddresses = @[mail];


    

    

    

    

//    地址

    CNMutablePostalAddress *address = [[CNMutablePostalAddress alloc] init];

    address.state = @"广东";

    address.city = @"深圳";

    address.postalCode = @"111111";

    address.street = @"南山区10";

    address.country = @"中国";

    

    //生成的上面地址的CNLabeledValue

    

    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];

        }

    }

    

    

}



- (void)contactViewController:(CNContactViewController *)viewController didCompleteWithContact:(nullable CNContact *)contact{

    if (contact) {

        NSLog(@"保存成功");

    }else{

        NSLog(@"点击了取消,保存失败");

    }

    

    [viewController dismissViewControllerAnimated:YES completion:nil];

   

    

}




//修改保存现有联系人实现

- (void)saveExistContact{

    //1.跳转到联系人选择页面,注意这里没有使用UINavigationController

    CNContactPickerViewController *controller = [[CNContactPickerViewController alloc] init];

    

    controller.delegate = self;

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

        

    }];

}


//2.实现点击联系人跳转到新建界面的代理


- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact{

    

    [picker dismissViewControllerAnimated:YES completion:^{

        

        //3.copy一份可写的Contact对象,不能用alloc

        CNMutableContact *c = [contact mutableCopy];

        //4.contact赋值

        [self setValueForContact:c existContect:YES];

        //5.跳转到新建联系人页面

        CNContactViewController *controller = [CNContactViewController viewControllerForNewContact:c];

        controller.delegate = self;

        UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:controller];

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

        }];

    }];

}



- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值