Iphone电话号码格式化基于区域设置

时间:2012-07-23 22:49:06

标签: ios objective-c internationalization string-formatting phone-number

在我的应用程序中,我尝试根据用户区域设置格式化电话号码。我面临的问题如下:

Viewcontroller.m中的代码:

   NSString *contact = [_phone convertContact:myNumber withLocale:typeN];
   NSLog(@"Phone number %@ and number = %@",contact,myNumber);

   //Console output 2012-07-23 15:40:16.994 InternProj[7585:f803] value of en and      23443235434

PhoneNumber.m中的代码:

   - (NSString *) convertContact:(NSNumber *)aNumber withLocale:(NSString *)locale{

  NSString *localeString = locale ;
  NSLog(@"value of %@ and %@",localeString , aNumber);
  NSString *tempStr = [[NSString alloc] initWithString:@""];
  NSRange range;
  range.length = 3;
  range.location = 3;
   //Returns the phone number 2032225200 as 1(203)222-5200
  if([localeString compare:@"en"] == NSOrderedSame) {
    NSLog(@"Inside en");
    NSString *areaCode = [[aNumber stringValue] substringToIndex:3];
    NSString *phone1 = [[aNumber stringValue] substringWithRange:range];
    NSString *phone2 = [[aNumber stringValue] substringFromIndex:6];

    tempStr = [NSString stringWithFormat:@"1(%@)%@-%@", areaCode, phone1, phone2];
    NSLog(@"Value tempStr = %@",tempStr);
}
else {
    tempStr = [[aNumber stringValue] substringToIndex:3];
}


  return tempStr;
}

我从convertContact方法获取null作为返回值。我尝试了所有的排列,但我没有得到错误。如果有人有基于Iphone语言环境的电话号码格式信息,请分享。

1 个答案:

答案 0 :(得分:1)

首先,iOS不支持电话号码格式化。开发人员必须编写自己的插件。还要提一下你在方法中传递的typeN变量。还要检查aNumber是否为空。我很确定你在方法中传递的数字是null的原因。