Contact結(jié)構(gòu)打印圖片
前言
Contacts.framework
是蘋果推出的新聯(lián)系人框架膳帕。應(yīng)用于iOS9.0之后蔗候,9.0之后將會全面取代iOS 9 以前的通訊錄框架 AddessBook.framework
笛园。
手機通訊錄iOS9前后對比 :
| iOS9之前| 描述 | iOS9之后 |描述 |
| -------------- | -------------- | -------------- |
|AddressBook| 純 C 語言的 API蛔钙,僅僅是獲得聯(lián)系人數(shù)據(jù)拯辙。沒有提供 UI 界面展示丰涉,需要自己搭建聯(lián)系人展示界面裳擎。 | Contacts|擁有 AddressBookUI 框架的所有功能涎永,使用起來更加的面向?qū)ο蟆
| AddressBookUI |提供了聯(lián)系人列表界面、聯(lián)系人詳情界面羡微、添加聯(lián)系人界面等谷饿,一般用于選擇聯(lián)系人| ContactsUI |擁有 AddressBook框架的所有功能,不再是 C 語言的 API妈倔,使用簡單博投。|
通訊錄框架ContactsUI使用
1、導(dǎo)入框架
#import <ContactsUI/ContactsUI.h>
2盯蝴、遵循代理
<CNContactPickerDelegate>
代理方法:
/*!
* @abstract Invoked when the picker is closed.
* @discussion The picker will be dismissed automatically after a contact or property is picked.
*/
- (void)contactPickerDidCancel:(CNContactPickerViewController *)picker;
/*!
* @abstract Singular delegate methods.
* @discussion These delegate methods will be invoked when the user selects a single contact or property.
*/
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact;
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty;
/*!
* @abstract Plural delegate methods.
* @discussion These delegate methods will be invoked when the user is done selecting multiple contacts or properties.
* Implementing one of these methods will configure the picker for multi-selection.
*/
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContacts:(NSArray<CNContact*> *)contacts;
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperties:(NSArray<CNContactProperty*> *)contactProperties;
3毅哗、授權(quán)
CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
if (status == CNAuthorizationStatusNotDetermined) {
[[[CNContactStore alloc] init] requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
NSLog(@"點擊同意");
[self openContact];
}else{
NSLog(@"點擊拒絕");
}
}];
} else if (status == CNAuthorizationStatusAuthorized) {
NSLog(@"已經(jīng)授權(quán)");
[self openContact];
} else {
NSLog(@"沒有授權(quán)");
UIAlertController *alert =[UIAlertController alertControllerWithTitle:@"We Need Permission To Access Your Contacts"
message:@"Settings on iphone >Privacy>Photos>seagullstudio" preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"Setting" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{UIApplicationOpenSettingsURLString:@YES} completionHandler:^(BOOL success) {
if (success) {
// NSLog(@"成功打開");
} else {
// NSLog(@"打開失敗");
}
}];
}]];
[self presentViewController:alert animated:YES completion:nil];
}
4、獲取通訊錄控制器
- (void)openContact {
CNContactPickerViewController *contactVC = [CNContactPickerViewController new];
contactVC.delegate = self;
[self presentViewController:contactVC animated:YES completion:nil];
}
5捧挺、實現(xiàn)delegate方法虑绵,獲取聯(lián)系人地址
#define kNULLString(string) ((![string isKindOfClass:[NSString class]])||[string isEqualToString:@""] || (string == nil) || [string isEqualToString:@""] || [string isKindOfClass:[NSNull class]]||[[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0)
//選擇單個聯(lián)系人,不展開聯(lián)系人的詳細信息闽烙,獲取后推出CNContactPickerViewController
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact {
[self parseAddressWithContact:contact];
[picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)parseAddressWithContact:(CNContact *)contact {
//contact的結(jié)構(gòu)分析我貼在文章底部
NSArray<CNLabeledValue *> *postAddressArray = contact.postalAddresses;
for (CNLabeledValue *addressLabel in postAddressArray) {
CNPostalAddress *address = addressLabel.value;
if (!kNULLString(address.street)) {
NSLog(@"%@", address.street );
}
if (!kNULLString(address.city)) {
NSLog(@"%@", address.city );
}
if (!kNULLString(address.postalCode)) {
NSLog(@"%@", address.postalCode );
}
if (!kNULLString(address.country)) {
NSLog(@"%@", address.country );
}
if (!kNULLString(address.state)) {
NSLog(@"%@", address.state );
}
}
}
//取消選擇的回調(diào)
- (void)contactPickerDidCancel:(CNContactPickerViewController *)picker {
[picker dismissViewControllerAnimated:YES completion:nil];
}
CNContact結(jié)構(gòu)圖
結(jié)構(gòu)圖
CNLabeledValue結(jié)構(gòu)圖
CNPostalAddress結(jié)構(gòu)圖
寫在最后
感謝:
JoySeeDog