Contacts and ContactsUI 通訊錄簡單使用一

Hello?Contacts,say?goodbye?Address?Book?!

Contacts.framework 框架針對線程安全的只讀使用進(jìn)行了優(yōu)化。9.0以上才可以。

contact class 是線程安全的,contact class is like NSDictionary 并且有可變子類CNMutableContact。?you can create your own custom labels.?

不說多,先顯示一下 你的通訊錄

CNContactPickerViewController ?為顯示通訊錄的ViewController?

CNContactPickerDelegate 為代理 引入Contacts 和ContactsUI

#import <Contacts/Contacts.h>

#import <ContactsUI/ContactsUI.h>

@interface ViewController ()<CNContactPickerDelegate,CNContactViewControllerDelegate>

@end

- (void)viewDidLoad {

? ? [super viewDidLoad];

簡單顯示 你的通訊錄

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

? ? // 設(shè)置代理 ? ?contactPickerViewController.delegate=self;

? ? // 顯示聯(lián)系人窗口視圖

? ? [self presentViewController:contactPickerViewController animated:YES completion:nil];

}

當(dāng)然啦,不能只顯示通訊錄。 按鈕時(shí)間都需要代理回調(diào)。

?//點(diǎn)擊聯(lián)系人控制器的Cancel按鈕執(zhí)行該方法

- (void)contactPickerDidCancel:(CNContactPickerViewController*)picker{

? ? NSLog(@"取消");

}

// 選中聯(lián)系人時(shí)執(zhí)行該方法

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

? ? NSLog(@"聯(lián)系人的資料:%@",contact);

? ? [self dismissViewControllerAnimated:YES completion:nil];

? //顯示聯(lián)系人詳細(xì)頁面. ?這個(gè)頁面需要CNContactViewControllerDelegate 代理

? CNContactViewController *contactController = [CNContactViewController viewControllerForNewContact:contact];

? ? contactController.delegate=self;

? ? UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:contactController];

? ? [self settingNavBarForNavController:navController];

? ? [self? presentViewController:navController animated:YES completion:nil];

}

//當(dāng)用戶選擇屬性時(shí)調(diào)用

- (BOOL)contactViewController:(CNContactViewController*)viewController shouldPerformDefaultActionForContactProperty:(CNContactProperty*)property{

? ? return NO;

}

//該協(xié)議是在創(chuàng)建新的名片界面點(diǎn)擊取消或者確定后的回調(diào)

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

? ? [viewControllerdismissViewControllerAnimated:YES completion:nil];

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容