我想用「 - 」「」「來代替號碼。」同時從電話簿中獲取聯繫人,這裏是我的代碼。
我的主要動機是從數字中提取國家代碼if + is present。
如果還有其他方式可以訪問國家代碼,請給我建議。未收到「 - 」「。」 「」同時從ABPerson的電話簿拿起聯繫人
- (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 = (__bridge NSString *) phoneNumberRef;
CFRelease(phoneNumberRef);
if ([phoneNumber rangeOfString:@"+"].location == NSNotFound) {
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@"." withString:@""];
self.lblMobileNumber.text = [NSString stringWithFormat:@"%@", phoneNumber];
} else {
NSArray *PhoneNumberComponents = [phoneNumber componentsSeparatedByString:@" "];
NSString * strCountryCode = PhoneNumberComponents[0] ;
[self.btnCountryCode setTitle:strCountryCode forState:UIControlStateNormal];
phoneNumber= [phoneNumber stringByReplacingOccurrencesOfString:PhoneNumberComponents[0] withString:@""];
NSLog(@"countryCodeSepratedStr%@",phoneNumber);
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@"." withString:@""];
self.lblMobileNumber.text = [NSString stringWithFormat:@"%@", phoneNumber];
}
}
}
}
return NO;
}
無關,您可能希望通過靜態分析器(在Xcode的「產品」菜單上的「分析」)運行此代碼,因爲我在使用它之前會很小心地釋放電話號碼。坦率地說,使用'__bridge_transfer'與電話號碼相比更容易,並且您可以完全取消'CFRelease(phoneNumberRef)',並讓ARC處理它(假設您使用的是ARC)。 – Rob