关于ios9中得AddressBook和AddressBookUI框架过时问题
2015-10-06 16:45
771 查看
ios9的更新后,AddressBook.framework和AddressBookUI.framework几乎是全部过时,其不支持OC。用Contacts.framework和ContactsUI.framework代替。
显示联系人:
添加联系人:点击按钮触发事件addContact,从而出现添加联系人界面。
/* 添加联系人 */
- (IBAction)addContact:(id)sender {
//获得联系人信息
NSString *name=self.nameField.text;
NSString *phone=self.phoneField.text;
//获得通讯录对象
CNContactStore *contactStore=[[CNContactStorealloc]init];
if ([CNContactStoreauthorizationStatusForEntityType:CNEntityTypeContacts] ==CNAuthorizationStatusNotDetermined) {
[contactStore requestAccessForEntityType:CNEntityTypeContactscompletionHandler:^(BOOL granted,NSError * _Nullable error) {
if (granted) {
NSLog(@"授权允许");
}
else
{
NSLog(@"拒绝访问");
}
}];
}
if ([CNContactStoreauthorizationStatusForEntityType:CNEntityTypeContacts] ==CNAuthorizationStatusAuthorized) {
NSLog(@"已授予过权限");
//初始化单个联系人,注意一定是可变的,不可变的属性是只读的
CNMutableContact *contact=[[CNMutableContactalloc]init];
contact.familyName=name;
//封装电话号码
CNPhoneNumber *phoneNumber=[CNPhoneNumberphoneNumberWithStringValue:phone];
CNLabeledValue *labeledValue=[[CNLabeledValuealloc]initWithLabel:@"手机号码"value:phoneNumber];
NSArray *array=@[labeledValue];
contact.phoneNumbers=array;
//将请求封装成saveRequest
CNSaveRequest *saveRequest=[[CNSaveRequestalloc]init];
//添加单个联系人
[saveRequest addContact:contacttoContainerWithIdentifier:nil];
NSError *error=nil;
//执行请求
[contactStore executeSaveRequest:saveRequesterror:&error];
NSLog(@"添加联系人成功");
}
else
{
UIAlertController *alertController=[UIAlertControlleralertControllerWithTitle:@"提醒"message:@"没有权限访问,请开启权限"preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action=[UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleCancelhandler:nil];
[alertController addAction:action];
[selfpresentViewController:alertControlleranimated:YEScompletion:nil];
NSLog(@"没有权限访问");
}
}
显示联系人:
CNContactPickerViewController *contactPickerViewController=[[CNContactPickerViewController alloc]init]; //该对象保存整个通讯录,可以遍历出来 CNContactStore *contactStore=[[CNContactStore alloc]init]; //判断是否授予过权限。(应用第一次打开通讯录一般运行) if ([CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts]==CNAuthorizationStatusNotDetermined) { //如果没有,弹窗申请权限 [contactStore requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) { //如果允许 if (granted) { NSLog(@"权限允许"); [self presentViewController:contactPickerViewController animated:YES completion:^{ }]; } //不允许 else { NSLog(@"权限不允许"); } }]; } //如果不是第一次访问通讯录,判断是否有允许访问 if ([CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts] == CNAuthorizationStatusAuthorized) { NSLog(@"有权限访问"); [self presentViewController:contactPickerViewController animated:YES completion:^{ }]; } //不允许访问 else { UIAlertController *alertController=[UIAlertController alertControllerWithTitle:@"提醒" message:@"没有权限访问,请开启权限" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *action=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil]; [alertController addAction:action]; [self presentViewController:alertController animated:YES completion:nil]; NSLog(@"没有权限访问"); }
添加联系人:点击按钮触发事件addContact,从而出现添加联系人界面。
/* 添加联系人 */
- (IBAction)addContact:(id)sender {
//获得联系人信息
NSString *name=self.nameField.text;
NSString *phone=self.phoneField.text;
//获得通讯录对象
CNContactStore *contactStore=[[CNContactStorealloc]init];
if ([CNContactStoreauthorizationStatusForEntityType:CNEntityTypeContacts] ==CNAuthorizationStatusNotDetermined) {
[contactStore requestAccessForEntityType:CNEntityTypeContactscompletionHandler:^(BOOL granted,NSError * _Nullable error) {
if (granted) {
NSLog(@"授权允许");
}
else
{
NSLog(@"拒绝访问");
}
}];
}
if ([CNContactStoreauthorizationStatusForEntityType:CNEntityTypeContacts] ==CNAuthorizationStatusAuthorized) {
NSLog(@"已授予过权限");
//初始化单个联系人,注意一定是可变的,不可变的属性是只读的
CNMutableContact *contact=[[CNMutableContactalloc]init];
contact.familyName=name;
//封装电话号码
CNPhoneNumber *phoneNumber=[CNPhoneNumberphoneNumberWithStringValue:phone];
CNLabeledValue *labeledValue=[[CNLabeledValuealloc]initWithLabel:@"手机号码"value:phoneNumber];
NSArray *array=@[labeledValue];
contact.phoneNumbers=array;
//将请求封装成saveRequest
CNSaveRequest *saveRequest=[[CNSaveRequestalloc]init];
//添加单个联系人
[saveRequest addContact:contacttoContainerWithIdentifier:nil];
NSError *error=nil;
//执行请求
[contactStore executeSaveRequest:saveRequesterror:&error];
NSLog(@"添加联系人成功");
}
else
{
UIAlertController *alertController=[UIAlertControlleralertControllerWithTitle:@"提醒"message:@"没有权限访问,请开启权限"preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action=[UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleCancelhandler:nil];
[alertController addAction:action];
[selfpresentViewController:alertControlleranimated:YEScompletion:nil];
NSLog(@"没有权限访问");
}
}
相关文章推荐
- iOS 【语音合成的集成和使用几点建议】
- IOS开发UI进阶之UITableView三
- UE删除掉所有的重复行
- UEFI工程编译命令Build用法
- iOS UITableView
- Arduino制作温湿度计
- Visual Stuido 在文件中查找不显示结果
- UICollectionView自定义布局
- 去掉UItableview headerview(组标题)黏性
- target overrides the `OTHER_LDFLAGS` build setting defined in `Pods/Target Support Files/Pods/Pods.
- NGUI纹理九宫格拉伸
- question(未完待续)
- UVA_12100: Printer Queue
- iOS-UIControls介绍
- iOS-UIResponse之事件响应链及其事件传递
- iOS-UIResponse之事件响应链及其事件传递
- 第二十九篇:UIPickerView简述与代码练习
- 扩展 easyui-tabs 插件 关闭标签页方法
- EasyUI-window包含一个iframe,在iframe中如何关闭window
- iOS之UI随笔-图片轮播器