iOS學習,ios開發
蘋果在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]; } } }