2013-11-22 39 views
4

我試圖讓選擇的移動電話號碼,peoplePickerNavigationController:shouldContinueAfterSelectingPerson:屬性:標識符:

ABMultiValueRef phones = ABRecordCopyValue(person, property); 
CFStringRef phoneNumber = ABMultiValueCopyValueAtIndex(phones, identifier); 

我有好幾部手機聯繫人(所有標有「移動」)。當我選擇的第一個,phoneNumber的給我的第一個,但如果我選擇任何一個連續的,phoneNumber的讓我對上號:

聯繫人: 周杰倫Jaymes 移動1111111111 移動2222222222 移動3333333333

點擊第一個,phoneNumber = 1111111111

點擊第二個,phoneNumber = 1111111111

點擊第三個,phoneNumber = +2222222222

+0

你如何選擇它們?你可以粘貼代碼嗎?它是tableview,以及如何將它們映射到單元格。 –

+0

什麼是「手機」? –

+0

Grzegorz,請參閱其中一個答案。 – user3003787

回答

4

這是我使用的代碼。它會給只

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier { 

     if (property == kABPersonPhoneProperty) { 

      ABMultiValueRef phoneProperty = ABRecordCopyValue(person,property); 
      CFIndex peopleIndex = ABMultiValueGetIndexForIdentifier(property, identifier); 
      NSString *phone = (__bridge_transfer NSString*)ABMultiValueCopyValueAtIndex(phoneProperty, peopleIndex); 

      [self dismissModalViewControllerAnimated:YES]; 
     } 
    return NO; 
} 
+0

這正是我正在做的。除了__bridge_transfer我只使用__bridge,但我懷疑這會改變任何東西。它開始按預期工作(至少在模擬器上) – user3003787

+1

@ user3003787如果使用'__bridge_transfer',ARC將負責爲您釋放此對象。如果你使用'__bridge',你將不得不手動'CFRelease'對象,否則你會泄漏。如果你通過靜態分析器運行你的代碼(在Xcode的「產品」菜單上「分析」),它很適合爲你識別這些類型的問題。 – Rob

+0

@Rob這只是示例如何檢索電話號碼。有很多情況下數字是錯誤的。在這種情況下,你可以放一些'正則表達式'來檢查。 –

0

你可以很容易地迭代那樣。嘗試NSLoging該代碼,以確保它的工作。 我認爲你的「選擇」邏輯有一些錯誤。

ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty); 

for (CFIndex i=0; i < ABMultiValueGetCount(phones); i++) 
{ 
    NSString* phoneLabel = (NSString*)ABMultiValueCopyLabelAtIndex(phones, i); 
    NSString* phoneNumber = ABMultiValueCopyValueAtIndex(phones, i); 

    //release variables since you were using COPY !!! 
    CFRelease(phoneNumber); 
    CFRelease(phoneLabel); 
} 

CFRelease(phones); 
0

端了正確的電話號碼實施這樣說:

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier 
{ 
    if (property == kABPersonPhoneProperty) 
    { 
     ABMultiValueRef phones = ABRecordCopyValue(person, property); 
     CFStringRef phoneNumber = ABMultiValueCopyValueAtIndex(phones, identifier); 
     NSLog(@"%@", phoneNumber); 
     NSMutableString *tmp = [NSMutableString stringWithFormat:@"%@", (__bridge_transfer NSString *)phoneNumber]; 
     NSString *strippedPhoneNumber = [tmp stringByReplacingOccurrencesOfString:@" " withString:@""]; 
     NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@"()-"]; 
     strippedPhoneNumber = [[strippedPhoneNumber componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString: @""]; 
     NSString *firstName = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty); 
     [peoplePicker dismissViewControllerAnimated:YES completion:nil]; 
     return NO; 
} 
+0

兩個問題:首先,你不應該在'ABMultiValueCopyValueAtIndex'中使用'identifier'。您應該首先使用'ABMultiValueGetIndexForIdentifier'從'identifier'中獲取索引,然後在'ABMultiValueCopyValueAtIndex'中使用該'CFIndex'。其次,你沒有執行'CFRelease(手機)',所以這會泄漏。通過靜態分析器運行(在Xcode的「產品」菜單上「分析」),它很好地識別這些問題。 – Rob

0

你不應該使用identifier作爲內ABMultiValueCopyValueAtIndex索引。你應該叫ABMultiValueGetIndexForIdentifierABMultiValueIdentifier標識轉換爲CFIndex指數:

ABMultiValueRef phones = ABRecordCopyValue(person, property); 
CFIndex index = ABMultiValueGetIndexForIdentifier(phones, identifier); 
NSString *phoneNumber = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phones, index)); 
CFRelease(phones); 

一般來說,ABMultiValueIdentifier值匹配ABMultiValueGetIndexForIdentifier檢索到的CFIndex值,但如果接觸是編輯(特別是如果前面的其中一個電話號碼是刪除),使用ABMultiValueIdentifier中的ABMultiValueCopyValueAtIndex將返回錯誤的記錄。

相關問題