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

来源:互联网 发布:邮箱验证正则表达式 js 编辑:程序博客网 时间:2024/05/18 01:32

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 {

    [superviewDidLoad];

  

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

}


-(void)loadView{

    

    [super loadView];

    UIButton *button = [UIButtonbuttonWithType:UIButtonTypeSystem];

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

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

    [self.viewaddSubview:button];

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

    

    UIButton *button2 = [UIButtonbuttonWithType:UIButtonTypeSystem];

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

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

    [self.viewaddSubview:button2];

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

  

}


- (void)saveNewContact{

    

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

    CNMutableContact *contact = [[CNMutableContactalloc] init];

    

    //2.contact赋值

    [selfsetValueForContact:contact existContect:NO];

    

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

   _controller = [CNContactViewControllerviewControllerForNewContact:contact];

    

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

    _controller.delegate =self;

    

    //4.跳转

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

    

    [selfpresentViewController:navigation animated:YEScompletion:^{

        

    }];

    

   

  

}


//设置要保存的contact对象

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

    

    if (!exist) {


        //名字和头像

        contact.nickname = @"xiaoming";

       UIImage *photo = [UIImageimageNamed:@"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 = [[CNInstantMessageAddressalloc]initWithUsername:@"123456"service:@"wechat"];

        CNLabeledValue *IMS = [CNLabeledValuelabeledValueWithLabel:nilvalue:IMSData];

        

        if (!exist) {

            contact.instantMessageAddresses = @[IMS];

        }else{

            if ([contact.instantMessageAddressescount] > 0) {

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

                [imsArr addObject:IMS];

                contact.instantMessageAddresses = imsArr;

            }else{

                contact.instantMessageAddresses =@[IMS];

            }

        }


        

        

        

        //SocialProfile,即时信息

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

        CNLabeledValue *socilaProfile = [CNLabeledValuelabeledValueWithLabel:nilvalue:socialProfileData];

        if (!exist) {

            contact.socialProfiles = @[socilaProfile];

            

        }else{

            if ([contact.socialProfilescount] > 0) {

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

                [socialProfileArr addObject:socilaProfile];

                contact.socialProfiles = socialProfileArr;

            }else{

                contact.socialProfiles = @[socilaProfile];

            }

        }


       

    }

    //电话

    CNLabeledValue *phoneNumber = [CNLabeledValuelabeledValueWithLabel:CNLabelPhoneNumberMobilevalue:[CNPhoneNumberphoneNumberWithStringValue:@"18888888888"]];

    

    

    if (!exist) {

        contact.phoneNumbers = @[phoneNumber];

    }

    //现有联系人情况

    else{

        if ([contact.phoneNumberscount] >0) {

            NSMutableArray *phoneNumbers = [[NSMutableArrayalloc] initWithArray:contact.phoneNumbers];

            [phoneNumbers addObject:phoneNumber];

            contact.phoneNumbers = phoneNumbers;

        }else{

            contact.phoneNumbers = @[phoneNumber];

        }

    }

    

    

//    网址:

    CNLabeledValue *url = [CNLabeledValuelabeledValueWithLabel:CNLabelURLAddressHomePagevalue:@"http://www.baidu.com"];

   

       contact.urlAddresses = @[url];

    

    

    

    

    //邮箱:

    CNLabeledValue *mail = [CNLabeledValuelabeledValueWithLabel:CNLabelWorkvalue:@"gha@163.com"];

    

             contact.urlAddresses = @[mail];


    

    

    

    

//    地址

    CNMutablePostalAddress *address = [[CNMutablePostalAddressalloc] init];

    address.state = @"广东";

    address.city = @"深圳";

    address.postalCode = @"111111";

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

    address.country = @"中国";

    

    //生成的上面地址的CNLabeledValue

    

    CNLabeledValue *addressLabel = [CNLabeledValuelabeledValueWithLabel:CNLabelWorkvalue:address];

    if (!exist) {

        contact.postalAddresses = @[addressLabel];

    }else{

        if ([contact.postalAddressescount] >0) {

            NSMutableArray *addresses = [[NSMutableArrayalloc] initWithArray:contact.postalAddresses];

            [addresses addObject:addressLabel];

            contact.postalAddresses = addresses;

        }else{

            contact.postalAddresses = @[addressLabel];

        }

    }

    

    

}



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

    if (contact) {

        NSLog(@"保存成功");

    }else{

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

    }

    

    [viewController dismissViewControllerAnimated:YEScompletion:nil];

   

    

}




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

- (void)saveExistContact{

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

    CNContactPickerViewController *controller = [[CNContactPickerViewControlleralloc] init];

    

    controller.delegate = self;

    [selfpresentViewController:controller animated:YEScompletion:^{

        

    }];

}


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


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

    

    [picker dismissViewControllerAnimated:YEScompletion:^{

        

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

        CNMutableContact *c = [contact mutableCopy];

        //4.contact赋值

        [selfsetValueForContact:c existContect:YES];

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

        CNContactViewController *controller = [CNContactViewControllerviewControllerForNewContact:c];

        controller.delegate = self;

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

        [selfpresentViewController:navigation animated:YEScompletion:^{

        }];

    }];

}



- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end




0 0
原创粉丝点击