標籤:etc 訪問 cot 全面 book record ini valueat his
地址簿的訪問
地址簿(Address Book)是一個共用的連絡人資訊資料庫。任何iOS應用程式都可以使用。通過提供常用列表資訊,而不是讓每一個應用程式管理獨立的連絡人清單,可改善使用者體驗。在擁有共用的地址簿後,無需在不同的應用程式中新增連絡人...多次,在一個應用程式中更新連絡人資訊後,其他所有應用程式就立刻能夠使用它們。iOS通過兩個架構提供了全面的地址簿資料庫訪問功能,分別是Address Book和Address Book UI。
Address Book UI架構是一組使用者介面類,封裝了Address Book架構,並向使用者提供了使用連絡人資訊的標準方式。使用該介面可以讓使用者在地址簿中瀏覽、搜尋和選擇連絡人,顯示並編輯選定連絡人的資訊,以及建立新的連絡人。
<1>為使用架構Address Book做準備
要想顯示地址簿UI和地址簿資料,必須匯入架構Address Book和Address Book UI的標頭檔,並指出將實現協議ABPersonPickerNavigationControllerDelegate。
開啟檔案ViewController.h,在現有編譯指令#import後面添加如下程式碼:
#import <AddressBook/AddressBook.h>#import <AddressBookUI/AddressBookUI.h>
接下來,修改代碼@interface,在其中添加<ABPersonPickerNavigationControllerDelegate>,功能是指出我們要遵守協議ABPersonPickerNavigationControllerDelegate:
@interface ViewController:UIViewController<ABPersonPickerNavigationControllerDelegate>
<2>顯示地址薄連絡人選取器
當使用者單擊"選擇一個"按鈕時,應用程式需顯示連絡人選取器這一模態視圖,它向使用者提供與應用程式“通訊錄”類似的介面。在檔案ViewController.m的方法newBFF中,分配並初始化一個連絡人選取器,將其委託設定為視圖控制器(self)然後在顯示。這個方法的代碼如下:
//開啟地址薄- (IBAction)newBFF:(id)sender{ ABPersonPickerNavigationController *picker; picker = [[ABPersonPickerNavigationController alloc]init]; picker.peoplePickerDelegate = self; [self presentViewController:picker animate:YES];}
<3>處理取消和挖掘
連絡人的介面彈出後,既可以選取連絡人就取消,也可以繼續深究連絡人的資訊。這裡我就只讓使用者選擇朋友後就取消,而不希望使用者繼續選擇或者編輯連絡人屬性。因此需要將委託方法peoplePickerNavigationController:peoplePicker:shouldContinueAfterSelectingPerson實現為返回NO,這是這個應用程式的核心方法。還需要讓委託方法關閉連絡人選取器模態視圖,並將控制器交給ViewController。
但是還必須實現連絡人選取器委託協議定義如下方法:
1.處理使用者取消選擇的情形(peoplePickerNavigationControllerDidCancel)
2.處理使用者深入挖掘連絡人屬性的情形(peoplePickerNavigationController:shouldContinueAfterSelectingPerson:property:idnetifier)。
在檔案ViewController.m中,實現方法peoplePickerNavigationControllerDidCancel,此方法用於處理使用者在連絡人選取器中取消選擇,具體代碼如下所示:
//關閉地址薄- (void) peoplePickerNavigationControllerDidCancel:(ABPersonPickerNavigationController *)peoplePicker{ [self dissmissViewControllerAnimate:YES];}
將方法peoplePickerNavigationController:shouldContinueAfterSelectingPerson:property:idnetifier實現返回NO,此方法用於處理使用者在連絡人選取器中取消選擇,具體代碼如下所示:
//設定不繼續深挖連絡人資訊-(BOOL)peoplePickerNavigationController:(ABPersonPickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef) person property:(ABPropertyID)property idntifier:(ABMultiValueIdntifier)idntifier{ //We won‘t get to this delegate method Return NO;}
<4>選擇、訪問和顯示連絡人資訊
如果使用者沒有取消選擇,將調用委託方法peoplePickerNavigationController:peoplePicker:shouldCotinueAfterSelectingPerson,並通過一個ABRecordRef將選定連絡人傳遞給該方法。ABRecordRef是在AddressBook架構中定義的。這裡,將分別讀取連絡人的名字、照片、電子郵件、郵遞區號這四項資訊,在讀取照片前需要檢查連絡人是否有照片。在此需要注意,返回的連絡人名字和照片並非Cocoa對象(即NSString和UIImage),而是Core Foundation中的C語言資料,因此需要使用AddressBook架構中的函數ABRecordCopyValue和UIImage的方法ImageWithData進行轉換。
對於電子郵件地址和郵遞區號,必須處理可能返回的多個值的請情形。就這些資料而言,也將使用ABRecordCopyValue擷取指向資料集的引用,再使用函數ABMultiValueGetCount來核實連絡人至少有一個電子郵件地址(或郵遞區號),然後使用ABMultiValueAtIndex複製第一個電子郵件地址或者郵遞區號。
在檔案ViewController.m中添加一個委託方法peoplePickerNavigationController:shouldContinueAfterSelectingPerson,此方法能夠在使用者選擇連絡人時做出響應,具體代碼如下:
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNaivgationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person{ //Retrieve the friend‘s name from the address book person record NSString *friendName; NSString *friendEmail; NSString *friendZip; //擷取連絡人的姓名 friendName = (__bridge NSString *)ABRecordCopyValue(person,kABPersonFristNameProperty); NSLog("連絡人的姓名:%@",friendName); //擷取連絡人郵遞區號 ABMultiValueRef friendAddressSet; NSDictionary *friendFirstAddress; friendAddressSet = ABRecordCopyValue(person,kABPersonAddressProperty); if(ABMultiValueGetCount(friendAddressSet)>0){ friendFirstAddress = (__bridge NSDictionary *)ABRecordCopyValueAtIndex(friendAddressSet,0); friendZip = [friendFirstAddress objectForKey:"ZIP"]; NSLog("連絡人郵遞區號:%@",friendZip); } //擷取連絡人郵件地址 ABMultiValueRef friendEmailAddress; friendEmailAddress = ABRecordCopyValue(person,kABPersonEmailProperty); if(ABMultiValueGetCount(friendEmailAddress)>0){ friendEmail = (__bridge NSString *)ABRecordCopyValueAtIndex(friendEmailAddress,0); NSLog("連絡人郵件地址:%@",friendEmail); } //擷取連絡人的映像 if(ABPersonHasImageData(person)){ UIImage *personImage = [UIImage imageWithData:(__bridge NSData *)ABPersonCopyImageData(person)]; NSLog("連絡人的映像:%@",personImage); } //關閉地址薄 [self dissmissViewControllerAnimate:YES]; return NO; }
iOS:訪問地址薄