獲取iPhone用戶手機號
使用下面的函數可以返回用戶的手機號:
extern NSString *CTSettingCopyMyPhoneNumber();
然后調用即可。
由于這個函數是包含在CoreTelephony中,所以只能用于非官方iPhone SDK。
為了調用系統的通訊錄界面與相應功能,需要引入AddressBook.framework與AddressBookUI.framework,同時,在源文件中需要包含同文件<AddressBook/AddressBook.h>,<AddressBookUI/AddressBookUI.h>.
首先申明變量:
ABPeoplePickerNavigationController * picker ;
在需要的地方調用顯示選擇聯系人界面,同時設置ABPeoplePickerNavigationControllerDelegate委托:
if (! picker ){
picker = [[ ABPeoplePickerNavigationController alloc ] init ];
// place the delegate of the picker to the controll
picker . peoplePickerDelegate = self ;
}
// showing the picker
[ self presentModalViewController : picker animated : YES ];
選擇聯系人界面如下圖所示:
- ( BOOL )peoplePickerNavigationController: ( ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:( ABRecordRef )person
{
return YES ;
}
該方法在用戶選擇通訊錄一級列表的某一項時被調用,通過person可以獲得選中聯系人的所有信息,但當選中的聯系人有多個號碼,而我們又希望用戶可以明確的指定一個號碼時(如撥打電話),返回YES允許通訊錄進入聯系人詳情界面:
當用戶點擊某個字段時,會調用如下方法:
- ( BOOL )peoplePickerNavigationController: ( ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:( ABRecordRef )person
property:( ABPropertyID )property
identifier:( ABMultiValueIdentifier )identifier
{
if (property == kABPersonPhoneProperty ) {
ABMutableMultiValueRef phoneMulti = ABRecordCopyValue (person, property);
int index = ABMultiValueGetIndexForIdentifier (phoneMulti,identifier);
NSString *phone = ( NSString *) ABMultiValueCopyValueAtIndex (phoneMulti, index);
//do something
[phone release ];
[peoplePicker dismissModalViewControllerAnimated : YES ];
}
return NO ;
}
聯系人信息中可能有很多字段,首先需要判斷選擇的是否為電話號碼字段.當滿足要求時,獲取聯系人信息,通過標識符獲得用戶選擇的號碼在該聯系人號碼列表中的索引,最后通過索引獲得選中的電話號碼.
最后還需要實現如下方法使得用戶在點擊"取消"按鈕時關閉聯系人選擇界面:
- ( void )peoplePickerNavigationControllerDidCancel:( ABPeoplePickerNavigationController *)peoplePicker
{
// assigning control back to the main controller
[
picker
dismissModalViewControllerAnimated
:
YES
];
iPhone獲取通訊錄里電話號碼
2 | 字號 訂閱
peopleArray = (NSMutableArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
for (id *people in peopleArray)
{
ABMultiValueRef phones = (ABMultiValueRef) ABRecordCopyValue(people, kABPersonPhoneProperty);
int nCount = ABMultiValueGetCount(phones);
for(int i = 0 ;i < nCount;i++)
{
NSString *phonelLable = (NSString *)ABMultiValueCopyLabelAtIndex(phones, i);
NSString *phoneNO = (NSString *)ABMultiValueCopyValueAtIndex(phones, i); // 這個就是電話號碼
}
}
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
