I'm copying phone number from iPhone address book to a text field. In text field it is showing as 1 (234) 567-8901. I want format it to 12345678901.
Any help?
You can use stringByReplacingOccurrencesOfString: withString: to remove characters you don't want such as #"(" with #""
EDIT: Better solution.
NSCharacterSet *charSet =[NSCharacterSet characterSetWithCharactersInString:#"()- "];
cleanedPhoneNumber = [phoneNumberString stringByTrimmingCharactersInSet:charSet];
Related
We have code like the following to retrieved the user language preference:
NSString *language = [[NSLocale preferredLanguages] firstObject];
Before iOS 8.4, language is "zh-Hans", "de", "ru", "ja" and etc. But since iOS 9, I notice that there is additional three characters "-US" appended to language. For example, "zh-Hans" becomes "zh-Hans-US"
I can find any documentation about this change. I assume that I could do something like the following to workaround this issue.
NSRange range = [language rangeOfString:#"-US"];
if (range.location!=NSNotFound && language.length==range.location+3) {
// If the last 3 chars are "-US", remove it
language = [language substringToIndex:range.location];
}
However, I am not sure whether it is safe to do so. It seems that "-US" is the location where the user is using the app? But this doesn't really make sense because we are in Canada. Has any body from other part of the world tried this?
Apple has started adding regions onto the language locales in iOS 9. Per Apple's docs, it has a fallback mechanism now if no region is specified. If you need to only support some languages, here is how I worked around it, per Apple's docs suggestion:
NSArray<NSString *> *availableLanguages = #[#"en", #"es", #"de", #"ru", #"zh-Hans", #"ja", #"pt"];
self.currentLanguage = [[[NSBundle preferredLocalizationsFromArray:availableLanguages] firstObject] mutableCopy];
This will automatically assign one of the languages in the array based off the User's language settings without having to worry about regions.
Source: Technical Note TN2418
To extract the region I think this is a better solution:
// Format is Lang - Region
NSString *fullString = [[NSLocale preferredLanguages] firstObject];
NSMutableArray *langAndRegion = [NSMutableArray arrayWithArray:[fullString componentsSeparatedByString:#"-"]];
// Region is the last item
NSString *region = [langAndRegion objectAtIndex:langAndRegion.count - 1];
// We remove region
[langAndRegion removeLastObject];
// We recreate array with the lang
NSString *lang = [langAndRegion componentsJoinedByString:#"-"];
Swift 5: Remove region from preferred language
Using Locale.preferredLanguages.first gives you the preferred App language (which can be different than device language for the user).
In order to support the script code and language code (but to remove the region code) I think it is best to create a locale given the preferred language and grab the information we need from there.
if let pref = Locale.preferredLanguages.first {
let locale = Locale(identifier: pref)
let code = [locale.languageCode, locale.scriptCode].compactMap{$0}.joined(separator: "-")
print(code)
}
So first we get the preferred app language, Then create a locale from the language.
To get the language code we create an array with locale.languageCode and the locale.scriptCode (which may be nil), remove any nil values with compactMap and then join the values with a "-".
This should allow support for Simplified Chinese and Traditional, and let Apple handle the region instead of assuming it will always be there.
I have a number of strings that have internet links embedded within them that worked fine until I applied NSLocalizedString to each of them for a localization in Spanish. Now the links in the strings are not recognized or operate as such in my app either for English (the base language) or Spanish.
I have been unable to determine why this is happening and haven't found any reference to this issue online. Is there some special formatting that I have to do to the URL part of my strings when using NSLocalizedString that I didn't have to when using NSString? I would greatly appreciate any help that anyone could offer with a solution to my issue?
Here is an example of one of my NSLocalizedStrings and its use in forming the contentString:
aboutContentText = NSLocalizedString(#"\"The Visitation\", by 1737, Jerónimo Ezquerra (1660-1737), http://commons.wikimedia.org/wiki/File:Jerónimo_Ezquerra_Visitation.jpg\n", #"aboutContentText-2nd Joyful Mystery");
contentString = [[NSMutableAttributedString alloc]
initWithString: aboutContentText attributes: contentAttributes2];
Don't localize the URLs, localize only the text:
NSString *preamble = NSLocalizedString(#"\"The Visitation\", by 1737, Jerónimo Ezquerra (1660-1737)", #"preamble aboutContentText-2nd Joyful Mystery");
NSString *urlString = #"http://commons.wikimedia.org/wiki/File:Jerónimo_Ezquerra_Visitation.jpg";
NSString *aboutContentText = [NSString stringWithFormat:#"%#, %#\n", preamble, urlString];
NSLog(#"aboutContentText: %#", aboutContentText);
NSLog output:
aboutContentText: "The Visitation", by 1737, Jerónimo Ezquerra (1660-1737), http://commons.wikimedia.org/wiki/File:Jerónimo_Ezquerra_Visitation.jpg
My app is displaying the price of an in app purchase product. How can I (at design time) enumerate all the currency symbols and characters used in all of Apple's international app stores? I am displaying text in my app using "texture atlas" based bitmap fonts, i.e. I have to manually include each character I want to display.
I realize that this is a moving target, so I plan to make my logic forgiving. For example if some future equivalent of the Euro symbol is added by Apple and somebody's running an old version of my app, I will silently drop that character and just display the numeric part as "2.99" or "2,99" etc.
But how can I make my list as accurate as possible today, per Apple's official list?
Here's how the string is formatted (straight from Apple's sample):
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[numberFormatter setLocale:product.priceLocale];
NSString *formattedString = [numberFormatter stringFromNumber:product.price];
Nobody's rushing to answer this, so here's the best option I've found so far. Basically, the approach would be scraping the currency symbols and Latin character alternates from the following page:
http://en.wikipedia.org/wiki/Currency_symbol#List_of_presently-circulating_currency_symbols
Notice that there is a generic currency symbol that can be used as a fallback.
If anybody has a better answer (meaning somehow gleaned from Apple), I'll be happy to accept your answer over this one.
I quickly wrote a Swift playground to grab the NumberFormatter's output for every available locale. Filtering this to only include currency symbols and punctuation gives a relatively complete set of characters to include.
let price = 0 as NSDecimalNumber
let availableIdentifiers = Locale.availableIdentifiers
var allCurrencySymbols: String = ""
for identifier in availableIdentifiers
{
let locale = Locale(identifier: identifier)
let formatter = NumberFormatter()
formatter.formatterBehavior = NumberFormatter.Behavior.behavior10_4
formatter.numberStyle = NumberFormatter.Style.currency
formatter.locale = locale
let formattedPrice = formatter.string(from: price)!
let currencySymbolsOnly = formattedPrice.replacingOccurrences(of: "0", with: "")
allCurrencySymbols.append(currencySymbolsOnly)
}
var set = Set<Character>()
let allCurrencySymbolsMinusDuplicates = String(allCurrencySymbols.characters.filter{ set.insert($0).inserted } )
print(allCurrencySymbolsMinusDuplicates)
On my Mac, this produces the output…
, ¤KMFCABuRE.₪٠٫۰$০₹YDTShN¥H€₺₦L₸rOP£៛၀nG₵денКМأم०UفجقVsk/zł؋Q༠Zد֏رسل₱یاoʻmكብርXdjbI₾ع₽сом₼ت₩f₡¥Wරුeب₭नेरूtإë₴l৳يp₫лвč₮
…which you can use to create your bitmap font. But remember your source font will need to support the characters, too.
This question already has answers here:
Remove white space from contact number fetched from phone book
(2 answers)
Closed 6 years ago.
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
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: "")
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.
Some iOS7 phone numbers are encoded with non-breaking space. Try this:
NSString* stringFormatted = [phoneNumber stringByReplacingOccurrencesOfString:#"\u00a0" withString:#""];
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...
Add a contact in iOS7 and Apple stores this: (555).555-5555 (where . is actually U00A0 or  )
My app copies a contact's info in a CFStringRef from AddressBook (when NSLogged the . shows)
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
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"
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
I made a small adjustment to the poster's answer in case someone wants to maintain the + at the begining of the number.
I made this small adjustment if you want to keep the plus after removing the spaces.
Boolean shouldAddPlus = NO;
if([[phoneNumber substringToIndex:1] isEqualToString:#"+"])
{
shouldAddPlus = YES;
}
phoneNumber = [[phoneNumber componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:#""];
if(shouldAddPlus)
phoneNumber = [base stringByAppendingString:phoneNumber];
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.
My App sends html to a Arduino with an Ethernet Shield. The Ethernet shield acts as a webserver and sends a simple message back to a UIWebview in the App. I'm using
NSString *myText = [myWebView2 stringByEvaluatingJavaScriptFromString:#"document.body.innerHTML"];
If I ask NSLog to print my "myText" It prints out
<h2>Relay1 ON</h2>
Which is what is sent to the webview. Now if I try to Compare myText with a static string that matches exactly, I get no result.
Heres the entire code block.
- (void)webViewDidFinishLoad:(UIWebView *)myWebView2;
{
NSString *myText = [myWebView2
stringByEvaluatingJavaScriptFromString:#"document.body.innerHTML"];
NSLog(#"my Text=%#",myText);
if ([myText isEqualToString:#"<h2>Relay1 ON</h2>"]) {
NSLog (#"If statement was triggered");
}
}
If I look at the value of myText in NSLog it exactly matches yet the if statement is never triggered.
What am I missing in that if statement?
Thanks!!
Not Really Solved but I used NSRange to search myText for "Relay1 ON" and that works more efficiently than dealing with hidden Characters. Thanks so Much for your help.