2015-01-12 54 views
0

我想用“ - ”“”“来代替号码。”同时从电话簿中获取联系人,这里是我的代码。
我的主要动机是从数字中提取国家代码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; 
} 
+0

无关,您可能希望通过静态分析器(在Xcode的“产品”菜单上的“分析”)运行此代码,因为我在使用它之前会很小心地释放电话号码。坦率地说,使用'__bridge_transfer'与电话号码相比更容易,并且您可以完全取消'CFRelease(phoneNumberRef)',并让ARC处理它(假设您使用的是ARC)。 – Rob

回答

0

我不会倾向于做任何的那个字符串操作的东西,而只是使用正则表达式来寻找+随后数字符串的开始,使用捕获括号抢只是国家代码:

NSError *error; 
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^\\s*\\+\\s*(\\d+)[^\\d]*(.*)$" options:0 error:&error]; 

NSTextCheckingResult *result = [regex firstMatchInString:phoneNumber options:0 range:NSMakeRange(0, [phoneNumber length])]; 
if (result) { 
    NSString *countryCode = [phoneNumber substringWithRange:[result rangeAtIndex:1]]; 
    NSString *phoneNumberWithoutCountryCode = [phoneNumber substringWithRange:[result rangeAtIndex:2]]; 
} else { 
    // no country code found 
} 
+0

罗布感谢您的好建议,但问题是我没有发现任何空间的“+”“。” “(”“)”中提取的数字。例如,我得到的号码是+917863838383,而不是+91 78-22-222222。 –

+0

嗯。我从你所有的代码中推断出你要删除空格之类的东西,以及你的原始数字有标点符号,我推测你会在使用上面的代码之前使用上面的代码。或者你是说你的一些电话号码是从没有空格或类似的地方开始的?在这种情况下,您最好在国家代码表中查找国家代码。 – Rob

+0

Rob我没有空间可以删除。我在模拟器和iPhone 5设备(iOS 8)中获取空间,但没有在iPhone 4s(ios7)中获得空间。 –

相关问题