所謂的iOS中的通訊錄(一)(自製簡易視圖版),ios通訊錄
-
- 提供了連絡人清單介面、連絡人詳情介面、新增連絡人...介面等
- 一般用於選擇連絡人
2. AddressBook.framework
-
-
- 純C語言的API,僅僅是擷取連絡人資料
- 沒有提供UI介面顯示,需要自己搭建連絡人展示介面
- 裡面的資料類型大部分基於Core Foundation架構,使用起來效果不佳
- 從iOS6開始。需要得到使用者的授權才能訪問通訊錄,因此在使用之前,需要檢查使用者是否已經授權
1、產生連絡人控制器
1 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 2 { 3 // 1.建立選擇連絡人的介面 4 ABPeoplePickerNavigationController *ppnc = [[ABPeoplePickerNavigationController alloc] init]; 5 6 // 2.設定代理 7 ppnc.peoplePickerDelegate = self; 8 9 // 3.彈出選擇連絡人介面10 [self presentViewController:ppnc animated:YES completion:nil];11 }
2、代理方法
1)點擊連絡人時調用
1 // 系統注釋:Called after a person has been selected by the user. 2 // 直譯:一個人被選中後被稱為使用者後調用 3 // 意譯:選中某一個連絡人的時候,會執行該代理方法 4 // 方法:如果實現了該方法,那麼就不會進入連絡人的詳細介面 5 - (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person NS_AVAILABLE_IOS(8_0) { 6 7 // 1.擷取選中連絡人的姓名(姓lastname和名firstname) 8 CFStringRef firstname = ABRecordCopyValue(person, kABPersonFirstNameProperty); 9 CFStringRef lastname = ABRecordCopyValue(person, kABPersonLastNameProperty);10 NSString *firstName = (__bridge_transfer NSString *)(firstname);11 NSString *lastName = (__bridge_transfer NSString *)(lastname);12 NSLog(@"%@ %@", firstName, lastName);13 14 // 2.擷取連絡人的電話號碼15 ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);16 CFIndex count = ABMultiValueGetCount(phones);17 for (CFIndex i = 0; i < count; i++) {18 NSString *phoneLabel = (__bridge_transfer NSString *)ABMultiValueCopyLabelAtIndex(phones, i);19 NSString *phoneValue = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(phones, i);20 NSLog(@"label : %@ value : %@", phoneLabel, phoneValue);21 }22 23 // 3.釋放不再使用的對象24 CFRelease(phones);25 26 }
2)點擊連絡人屬性時調用
1 // Called after a property has been selected by the user. 2 // 直譯:一個屬性被使用者選定後調用。 3 // 意譯:選中某一個連絡人的屬性的時候,會執行該代理方法 4 // 方法:如果實現了該方法,那麼選中一個連絡人的屬性時,就會推出控制器.不會進入下一個頁面 5 // 參數: arg1:property 選中的屬性 identifier : 每一個屬性都由一個對應標示 6 - (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier NS_AVAILABLE_IOS(8_0) { 7 8 NSLog(@"選擇了某一個連絡人的某一個屬性"); 9 10 }
3)點擊取消按鈕調用
1 // Called after the user has pressed cancel.2 // 直譯:取消被選中後被稱為使用者後調用3 // 意譯:選中取消按鈕的時候,會執行該代理方法4 - (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker {5 6 NSLog(@"使用者點擊了取消按鈕");7 8 }
未完待續~~~~~
by:Coder丶PSS