简体   繁体   English

从ABPerson的电话簿中获取联系人时未获得“-”“。”“”

[英]Not getting “-” “.” “ ” while picking up a contact from phonebook from ABPerson

I want to get number as it is with "-" " " "." 我想按“-”“”“来获取数字。 while picking up a contact from phone book here's my code . 从电话簿中获取联系人时,这是我的代码。
My main motive is to extract the country code from the number if + is present. 我的主要动机是,如果存在+,则从数字中提取国家/地区代码。
Also please suggest me if there is any other way to access country code. 另外,如果还有其他访问国家/地区代码的方法,也请提出建议。

    - (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;
 }

I wouldn't be inclined do any of that string manipulation stuff, but just use regular expression to look for + followed by number at start of the string, using capturing parentheses to grab just the country code: 我不会倾向于做任何字符串处理的事情,而只是使用正则表达式在字符串的开头查找+后跟数字,并使用捕获括号仅捕获国家/地区代码:

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM