2010-08-23 32 views
5

Tôi sử dụng mã sau để đặt truy xuất số điện thoại trong ứng dụng của mình.Cách lấy số điện thoại di động từ Danh bạ iPhone.

CFStringRef addressBookMobile; 
ABRecordRef person; 
NSString *mobile; 

person = CFArrayGetValueAtIndex(people, i); 
addressBookMobile = ABRecordCopyValue(person, kABPersonPhoneProperty); 
mobile = [NSString stringWithFormat:@"%@", addressBookMobile]; 

Thẻ của địa chỉ liên hệ là 'di động'. Tuy nhiên, khi tôi sử dụng NSLog(@"%@", mobile);. Nó hiển thị <NSCFType: 0x802ffc0>. có bất kỳ vấn đề gì đối với mã của tôi không?

Tôi có nên sử dụng const CFStringRef kABPersonPhoneMobileLabel và cách sử dụng không? Như thể tôi thay thế nó như mã trên, nó có lỗi. Ai giúp tôi với? Cảm ơn bạn.

Trả lời

2
ABMultiValueRef phoneNumbers = (NSString *)ABRecordCopyValue(person, kABPersonPhoneProperty); 
CRStringRef mobileNumber; 
CRStringRef mobileLabel; 
for (CFIndex i = 0; i < ABMultiValueGetCount(phoneNumbers); i++) { 
    mobileLabel = ABMultiValueCopyLabelAtIndex(phoneNumbers, i); 
    if ([mobileLabel isEqualToString:@"_$!<Mobile>!$_"]) { 
     mobileNumber = ABMultiValueCopyValueAtIndex(phoneNumbers,i); 
     break; 
    } 
} 
5

Số điện thoại của một người trong sổ địa chỉ ở dạng thuộc tính nhiều giá trị.

Trong trường hợp của bạn, bạn nên có một cái gì đó như sau (đã không thử nó, gõ trực tiếp ở đây, vì vậy tôi không biết nếu nó biên dịch và/hoặc công trình):

ABMultiValueRef phoneNumbers = (NSString *)ABRecordCopyValue(person, kABPersonPhoneProperty); 
NSString *mobileNumber; 
NSString *mobileLabel; 
for (CFIndex i = 0; i < ABMultiValueGetCount(phoneNumbers); i++) { 
    mobileLabel = (NSString *)ABMultiValueCopyLabelAtIndex(mobilePhones, i); 
    if ([mobileLabel isEqualToString:@"mobile"]) { 
     mobileNumber = (NSString*)ABMultiValueCopyValueAtIndex(mobilePhones,i); 
     break; 
    } 
} 
+0

lancu, cảm ơn bạn đã trả lời của bạn. Nhưng tôi thấy rằng giải pháp có thể không chính xác. kết quả là như nhau và tôi muốn hỏi, các điện thoại di động trong mã là gì? Cảm ơn bạn. – Questions

+0

Xin lỗi Mark, đáng lẽ nó phải là phoneNumbers :-). Như tôi đã nói, tôi đã nhập trực tiếp vào biểu mẫu, chứ không phải trong Xcode :-). Vì vậy, thay vì điện thoại di động, thay thế bằng phoneNumbers và nó nên được tất cả tốt. –

16

kiểm tra ABPerson Refrence và bạn không cần sử dụng @ "$ !! $" nhưng kABPersonPhoneMobileLabel. ví dụ là:

ABMultiValueRef phones = (ABMultiValueRef)ABRecordCopyValue(person, kABPersonPhoneProperty); 
    NSString* [email protected]""; 
    NSString* mobileLabel; 
    for (int i=0; i < ABMultiValueGetCount(phones); i++) { 
     //NSString *phone = (NSString *)ABMultiValueCopyValueAtIndex(phones, i); 
     //NSLog(@"%@", phone); 
     mobileLabel = (NSString*)ABMultiValueCopyLabelAtIndex(phones, i); 
     if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel]) { 
      NSLog(@"mobile:"); 
     } else if ([mobileLabel isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel]) { 
      NSLog(@"iphone:"); 
     } else if ([mobileLabel isEqualToString:(NSString*)kABPersonPhonePagerLabel]) { 
      NSLog(@"pager:"); 
     } 
     [mobile release]; 
     mobile = (NSString*)ABMultiValueCopyValueAtIndex(phones, i); 
     NSLog(@"%@", mobile); 
    } 
+0

lý do tại sao nó cho tôi thấy lỗi trên dòng ABMultiValueRef phones = (ABMultiValueRef) ABRecordCopyValue (người, kABPersonPhoneProperty); NSString * mobile = @ ""; nói người không khai báo. tôi đang sử dụng ios 5 – user2189388

0

Đây là vững chắc cho ARC 64bit iOS8:

- (NSArray *)phoneNumbersOfContactAsStrings:(ABRecordRef)contactRef { 

NSMutableArray *mobilePhones = [NSMutableArray arrayWithCapacity:0]; 

ABMultiValueRef phones = ABRecordCopyValue(contactRef, kABPersonPhoneProperty); 
NSArray *allPhoneNumbers = (NSArray *)CFBridgingRelease(ABMultiValueCopyArrayOfAllValues(phones)); 

for (NSUInteger i=0; i < [allPhoneNumbers count]; i++) { 
    if ([(NSString *)CFBridgingRelease(ABMultiValueCopyLabelAtIndex(phones, (long)i)) isEqualToString:(NSString *)kABPersonPhoneMobileLabel]) { 
     [mobilePhones addObject:CFBridgingRelease(ABMultiValueCopyValueAtIndex(phones, (long)i))]; 
    } 
    if ([(NSString *)CFBridgingRelease(ABMultiValueCopyLabelAtIndex(phones, (long)i)) isEqualToString:(NSString *)kABPersonPhoneIPhoneLabel]) { 
     [mobilePhones addObject:CFBridgingRelease(ABMultiValueCopyValueAtIndex(phones, (long)i))]; 
    } 
} 

CFRelease(phones); 
return mobilePhones; 
} 
Các vấn đề liên quan