Strange Invisible Character in String causing NSURL to fail
Asked Answered
U

1

0

I am having a String to Number problem that is taking me days to resolve. So it all started out in my app that involves taking a phone number from the addressbook and making a call. Everything was working fine until I noticed some phone numbers were causing my program to crash.

After some time, I finally figured that they were crashing for the phone numbers that had a space in it, which finally made sense because even though it is a phone number I am using NSURL to call "tel://(555) 555 5555" and having a space between it will make the URL invalid which was causing my program to crash.

Now I was happy I figured out the second problem and decided to do a simple character replacement to replace all whitespace with "-". This worked for the test string I tested it on and everything looked good. I decided to hook this back up to my code and then my program is still crashing.

I then did some debugging and noticed even though my test case worked, the phone numbers I am pulling from the iOS address book were not having the whitespace replaced. It seems that whatever character is being used to space out the phone digits is not a whitespace or at least it is not a whitespace that I can simply type like this -> " "

Any idea how to resolve this? Either (1) to know what exactly this invisible character is and replace it or (2) is there a way to just extract a phone number as all numbers instead of the extra chars "+(.)- " or any option here?

Uncover answered 26/2, 2015 at 4:38 Comment(0)
E
1

You have to use .stringByAddingPercentEscapesUsingEncoding to convert special characters.

let myLink = "http://google.com"

let myUrl = NSURL(string: myLink.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!)!
Exurbanite answered 26/2, 2015 at 4:40 Comment(2)
OMG Thank you!!! Just tried it and it worked. I can't believe how the smallest things sometimes are the things that can cause the biggest headaches. This literally took me days and slowed me down so much. I knew it had to be something so silly. What threw me off is that my messaging part was working fine, until I realized messaging wasnt' using NSURL, so the results wouldn't affect each other.Uncover
I will accept the answer once my 8 mins timer is up.Uncover

© 2022 - 2024 — McMap. All rights reserved.