我想要做的就是让用户从地址簿中选择一个号码.我在这个问题中找到了代码:
如何从地址簿联系人处获取电话号码(iphone sdk)
ABMultiValueRef container = ABRecordCopyValue(person, property); CFStringRef contactData = ABMultiValueCopyValueAtIndex(container, identifier); CFRelease(container); NSString *contactString = [NSString stringWithString:(NSString *)contactData]; CFRelease(contactData);
问题是在第二行(在3.0设备上运行时)我收到以下错误:
客户经理无法找到标识为MobileMe:rustyshelf的帐户
其次是:
程序接收信号:"EXC_BAD_ACCESS".
这都在picker委托方法中:
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{
这只是我的地址簿中的一个联系人,它与Mobile Me同步
编辑:我认为这可能是SDK的一个错误,它发生在我的一些联系人,而不是其他人...
"identifier"参数不包含所触摸记录的CFIndex.我用来判断用户选择的电话号码的方法是:
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier { if (property == kABPersonPhoneProperty) { ABMultiValueRef multiPhones = ABRecordCopyValue(person, kABPersonPhoneProperty); for(CFIndex i = 0; i < ABMultiValueGetCount(multiPhones); i++) { if(identifier == ABMultiValueGetIdentifierAtIndex (multiPhones, i)) { CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(multiPhones, i); CFRelease(multiPhones); NSString *phoneNumber = (NSString *) phoneNumberRef; CFRelease(phoneNumberRef); txtPhoneNumber.text = [NSString stringWithFormat:@"%@", phoneNumber]; [phoneNumber release]; } } } [self dismissModalViewControllerAnimated:YES]; return NO; }
关于JWD的回答,这是一个更安全的版本,它使用内置常量并选择iphone号码而不是另一个手机号码......
ABMultiValueRef phones =(NSString*)ABRecordCopyValue(person, kABPersonPhoneProperty); NSString* mobile=@""; NSString* mobileLabel; for(CFIndex i = 0; i < ABMultiValueGetCount(phones); i++) { mobileLabel = (NSString*)ABMultiValueCopyLabelAtIndex(phones, i); if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel]) { [mobile release] ; mobile = (NSString*)ABMultiValueCopyValueAtIndex(phones, i); } else if ([mobileLabel isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel]) { [mobile release] ; mobile = (NSString*)ABMultiValueCopyValueAtIndex(phones, i); break ; } }
- (BOOL)personViewController:(ABPersonViewController *)personViewController shouldPerformDefaultActionForPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifierForValue { if (property == kABPersonPhoneProperty) { ABMultiValueRef numbers = ABRecordCopyValue(person, property); NSString* targetNumber = (__bridge NSString *) ABMultiValueCopyValueAtIndex(numbers, ABMultiValueGetIndexForIdentifier(numbers, identifierForValue)); NSLog(@"%@", targetNumber); } return NO; }
我用这个问题来构建我自己的解决方案.我发布的代码不是作为答案,仅仅是为了找到有用的人.这些是获取房产的简单步骤.内存管理除外.