獲取iPhone用戶手機(jī)號(hào)
使用下面的函數(shù)可以返回用戶的手機(jī)號(hào):
extern NSString *CTSettingCopyMyPhoneNumber();
然后調(diào)用即可。
由于這個(gè)函數(shù)是包含在CoreTelephony中,所以只能用于非官方iPhone SDK。
為了調(diào)用系統(tǒng)的通訊錄界面與相應(yīng)功能,需要引入AddressBook.framework與AddressBookUI.framework,同時(shí),在源文件中需要包含同文件<AddressBook/AddressBook.h>,<AddressBookUI/AddressBookUI.h>.
首先申明變量:
ABPeoplePickerNavigationController * picker ;
在需要的地方調(diào)用顯示選擇聯(lián)系人界面,同時(shí)設(shè)置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 ];
選擇聯(lián)系人界面如下圖所示:
- ( BOOL )peoplePickerNavigationController: ( ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:( ABRecordRef )person
{
return YES ;
}
該方法在用戶選擇通訊錄一級(jí)列表的某一項(xiàng)時(shí)被調(diào)用,通過(guò)person可以獲得選中聯(lián)系人的所有信息,但當(dāng)選中的聯(lián)系人有多個(gè)號(hào)碼,而我們又希望用戶可以明確的指定一個(gè)號(hào)碼時(shí)(如撥打電話),返回YES允許通訊錄進(jìn)入聯(lián)系人詳情界面:
當(dāng)用戶點(diǎn)擊某個(gè)字段時(shí),會(huì)調(diào)用如下方法:
- ( 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 ;
}
聯(lián)系人信息中可能有很多字段,首先需要判斷選擇的是否為電話號(hào)碼字段.當(dāng)滿足要求時(shí),獲取聯(lián)系人信息,通過(guò)標(biāo)識(shí)符獲得用戶選擇的號(hào)碼在該聯(lián)系人號(hào)碼列表中的索引,最后通過(guò)索引獲得選中的電話號(hào)碼.
最后還需要實(shí)現(xiàn)如下方法使得用戶在點(diǎn)擊"取消"按鈕時(shí)關(guān)閉聯(lián)系人選擇界面:
- ( void )peoplePickerNavigationControllerDidCancel:( ABPeoplePickerNavigationController *)peoplePicker
{
// assigning control back to the main controller
[
picker
dismissModalViewControllerAnimated
:
YES
];
iPhone獲取通訊錄里電話號(hào)碼
2 | 字號(hào) 訂閱
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); // 這個(gè)就是電話號(hào)碼
}
}
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

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