【问题标题】:Not getting "-" "." " " while picking up a contact from phonebook from ABPerson没有得到“-”“。” " " 在从 ABPerson 的电话簿中接听联系人时
【发布时间】:2015-01-12 11:31:59
【问题描述】:

我想得到带有“-”“”“。”的数字。从电话簿中获取联系人时,这是我的代码。
我的主要动机是从号码中提取国家代码(如果存在 +)。
如果有任何其他方法可以访问国家代码,也请建议我。

    - (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)。

标签: iphone abpeoplepickerview abperson mobile-country-code


【解决方案1】:

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

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
}

【讨论】:

  • Rob 谢谢你的好建议,但问题是我没有找到任何空格的“+”“。” "(" ")" 中提取的数字。例如,我得到的号码是 +917863838383 而不是 +91 78-22-222222。
  • 嗯。我从您删除空格等的所有代码中推断出您的原始数字带有标点符号,并且我假设您会在删除所有这些之前使用上述内容。或者你是说你的一些电话号码开头没有空格或类似的东西?在这种情况下,您很有可能必须在国家代码表中查找国家代码。
  • Rob 我没有要删除的空格。我在模拟器和 iPhone 5 设备 (iOS 8) 中获得了空间,但在 iPhone 4s(ios7) 中没有。
  • 所以在 iPhone 4s(ios7) 中,整个数字是国家代码。而不是得到分开的号码和国家代码。因为这个数字是纯的,没有空格,也没有任何其他非数字
猜你喜欢
  • 2011-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-17
  • 2016-05-30
  • 1970-01-01
  • 2015-07-20
  • 2014-05-27
相关资源
最近更新 更多