iOS 7 contact number spaces are not spaces [duplic

2020-02-25 08:40发布

In my app I am trying to retrieve the list of contact's number and try to do operations on them. I realized that whenever I have added new contacts (after updating to iOS 7) the new contacts formatting has changed, as there are spacings in the newly added numbers.

Using the ordinary replace methods does not remove the spaces.

Are these really spaces or what are these ? my objective is to get back the 'space' free number.

for example, if the number is 1 818 323 323 323, I want to get 1818323323323

标签: ios ios6 ios7
8条回答
爷的心禁止访问
2楼-- · 2020-02-25 09:05

The correct solution is to replace the occurences with a valid space

NSString *clean = [dirty stringByReplacingOccurrencesOfString:@"\u00a0" withString:@" "];

So you dont loose the space and the user sees what sees in other apps.

查看更多
等我变得足够好
3楼-- · 2020-02-25 09:09

After way too much string cleaning I finally found an answer after printing the CFStringRef straight from the Address Book. Here's what's going on behind the scenes...

  1. Add a contact in iOS7 and Apple stores this: (555).555-5555 (where . is actually U00A0 or &nbsp)
  2. My app copies a contact's info in a CFStringRef from AddressBook (when NSLogged the . shows)
  3. CFStringRef is cast into NSString (NSLog now shows 555\U00a0555-5555)

To remove the \U00A0 I tried 3 answers from this thread and [NSCharacterSet characterSetWithRange:(160,1)] which didn't work. What finally worked was this line of code:

phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@"." withString:@""];

// where @"." was created by typing Option+Spacebar

查看更多
神经病院院长
4楼-- · 2020-02-25 09:15

Another (very flexible) option is to use a regular expression. This allows you to retain the + or any other characters you want to remain.

let numberFromAddressBook = "+1 818 323 323 323"

let cleanNumber = numberFromAddressBook.stringByReplacingOccurrencesOfString("[^0-9+]", withString: "", options: NSStringCompareOptions.RegularExpressionSearch, range:nil)

"+1818323323323"
查看更多
Melony?
5楼-- · 2020-02-25 09:16

I looked at this as not getting rid of 'spaces' but being left with only decimal characters. This code did that for me:

phoneNumberString = [[phoneNumberString componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""];

This takes out everything that's not a number 0-9.

Swift 4.1:

phoneNumberString = phoneNumberString.components(separatedBy: CharacterSet.decimalDigits.inverted).joined(separator: "")
查看更多
【Aperson】
6楼-- · 2020-02-25 09:21

The cleanest solution I'm using in my apps is:

NSMutableCharacterSet *phoneNubmerCharacterSet = [NSMutableCharacterSet characterSetWithCharactersInString:@"+"];
[phoneNubmerCharacterSet formUnionWithCharacterSet:[NSCharacterSet decimalDigitCharacterSet]];
NSString* phoneString = [[phoneString componentsSeparatedByCharactersInSet:[phoneNubmerCharacterSet invertedSet]] componentsJoinedByString:@""];

No "if" logic, keeps the + in number, removes all kind of random unwanted characters

查看更多
劫难
7楼-- · 2020-02-25 09:23

Try this:

NSString *cleaned = [[phoneNr componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] componentsJoinedByString:@""];

This should work for any kind of space (of which there are many). It may seem somewhat inefficient, but for phone numbers, this should be ok.

查看更多
登录 后发表回答