码迷,mamicode.com
首页 > 移动开发 > 详细

iOS学习 - 14.本地联系人

时间:2016-05-16 17:41:43      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:

苹果在iOS9的SDK中废除了AddressBookUI.framework的一些功能(是不是这个库都废除了,有待验证),具体和保存联系人相关的几个系统界面如下:
联系人选择:AddressBookUI/ABPeoplePickerNavigationController.h
联系人详情:AddressBookUI/ABPersonViewController.h
未知联系人:AddressBookUI/ABUnknownPersonViewController.h
新建联系人:AddressBookUI/ABNewPersonViewController.h
新的SDK中使用
联系人选择:ContactsUI/CNContactPickerViewController.h
联系人详情、新建联系人、未知联系人:ContactsUI/CNContactViewController.h(使用不同方法创建,下面会说)

下面的例子,仍存在问题

1.UINavgationController 标题、左右 item 都是系统自带的,联系人详情的时候,navgationItem 的返回按钮无点击事件,应该怎么自定义?

2.联系人 logo 拍照黑屏(真机测试已获取权限),待解决

#import "ViewController.h"
#import <ContactsUI/CNContactPickerViewController.h>
#import <ContactsUI/CNContactViewController.h>
@interface ViewController ()<CNContactViewControllerDelegate,CNContactPickerDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (IBAction)Click:(id)sender {
    [self set1];
}
//保存新联系人
-(void)set{
    //1.创建 Contact 对象,必须是可变的
    CNMutableContact *contact = [[CNMutableContact alloc]init];
    //2.为 contact 赋值
    [self setValue4Contact:contact existContect:NO];
    //3.创建新建好友页面
    CNContactViewController *controller = [CNContactViewController viewControllerForNewContact:contact];
    controller.delegate = self;
    
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:controller];
    [self presentViewController:nav animated:YES completion:nil];
}

//保存现有联系人
-(void)set1{
    //跳转到联系人选择页面,注意这里没有使用 UINavgationController
    CNContactPickerViewController *picker = [[CNContactPickerViewController alloc]init];
    picker.delegate = self;
    [self presentViewController:picker animated:YES completion:nil];
}

//实现点选的代理,其他代理方法根据自己需求实现
-(void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact{

    [picker dismissViewControllerAnimated:YES completion:^{
    // copy 一份可写的 contact 对象,不要尝试 alloc 一类,mutableCopy 独此一家
    CNMutableContact *c = [contact mutableCopy];
    //为 contact 赋值
    [self setValue4Contact:c existContect:YES];
    //跳转到新建联系人页面
    CNContactViewController *controller = [CNContactViewController viewControllerForNewContact:c];
    controller.delegate = self;
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:controller];
    
    [self presentViewController:nav animated:YES completion:nil];

    }];
}
//设置要保存的 contact 对象
- (void)setValue4Contact:(CNMutableContact *)contact existContect:(BOOL)exist{
    if (!exist) {
        //名字和头像
        contact.nickname = @"oriccheng";
        /*
         UIImage *logo = [UIImage imageNamed:@""];
         NSData *dataRef = UIImagePNGRepresentation(logo);
         contact.imageData = dataRef;
         
         */
    }
    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 = @"20000";
    address.street = @"新街口王八街18号";
    //生成的上面直至的 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];
        }
    }
    

}

 

iOS学习 - 14.本地联系人

标签:

原文地址:http://www.cnblogs.com/asamu/p/5498479.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!