1

I need to make a phone call from the app, so far it's working. The number consists of two numbers: phone number, and pass code followed by hashtag. But some users need to put double hashtag in the end of the number, and that crashes the app when it's calling that URL at once:

UIApplication.sharedApplication().openURL(NSURL (string: "tel//:1111111,,222##"))

With single hashtag it's working, and it is possible to press hashtag on the keyboard after device has dialed already. I tried to append ASCII table - hex number, 23 (means #) - it did not help.

EDIT:

I've found something here:

let encodedHost = 
numberToDial.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())

which does swap the hashtags like this:

enter image description here

But when trying to call this number (NSURL) - the app still crashes. How can I still have double hashtag in the end of the URL and not crash the app?

Community
  • 1
  • 1
Async-
  • 3,140
  • 4
  • 27
  • 49

2 Answers2

2

I think answer to your question is in the documentation.

To prevent users from maliciously redirecting phone calls or changing the behavior of a phone or account, the Phone app supports most, but not all, of the special characters in the tel scheme. Specifically, if a URL contains the * or # characters, the Phone app does not attempt to dial the corresponding phone number. If your app receives URL strings from the user or an unknown source, you should also make sure that any special characters that might not be appropriate in a URL are escaped properly. For native apps, use the stringByAddingPercentEscapesUsingEncoding: method of NSString to escape characters, which returns a properly escaped version of your original string.

Especially method stringByAddingPercentEscapesUsingEncoding try to use this method with string that you would like to dial.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Oleg Gordiichuk
  • 15,240
  • 7
  • 60
  • 100
  • this is deprecated since iOS9, but I tried the stringByAddingPercentEncodingWithAllowedCharacters like this: let address = "#"; let escapedAddress = address.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet()); trimmedText += NSString(format: "\(escapedAddress)") as String; As a result, instead of hashtags, it swaps them to "2", which is not what i want – Async- Jul 29 '16 at 13:52
2

The problem is that NSURL thinks that isn’t a valid URL. This:

NSURL(string: "tel//:1111111,,222##")

…returns nil. The problem isn’t the the escaping; it’s that NSURL apparently doesn’t accept anything with two # symbols in it. (Paste it into a playground to experiment for yourself.) It looks like it ought to, so you might file that as a bug with Apple. However, note this from Apple:

To prevent users from maliciously redirecting phone calls or changing the behavior of a phone or account, the Phone app supports most, but not all, of the special characters in the tel scheme. Specifically, if a URL contains the * or # characters, the Phone app does not attempt to dial the corresponding phone number.


I don’t know why this code compiles:

UIApplication.sharedApplication().openURL(NSURL(string: "tel//:1111111,,222##"))

NSURL(string:) is a failable initializer, meaning it returns an optional, and openURL()’s arg is non-optional. That cues you to do a check like this, which would prevent the crash:

if let url = NSURL(string: "tel//:1111111,,222##") {
    UIApplication.sharedApplication().openURL(url)
} else {
    showErrorMessage("Unable to dial this number")
}

Did you perhaps have an ! after the NSURL constructor that got dropped when you trimmed it for this question? If so, that would be the cause of your crash.

Community
  • 1
  • 1
Paul Cantrell
  • 9,175
  • 2
  • 40
  • 48
  • You are right, in my case I have: let nsurr = NSURL (string: "\(self.callOptions.devicephoneURL)\(numberToDial)")! , but i can not get rid of the "!" – Async- Oct 19 '16 at 09:44
  • I have updated my code like you show now before you updated it, this way I can at least avoid crash of the app. But do you know how can I allow two hashtags? (advantage of this: it skips the intro-voice when you put double hashtags in theend of the number) – Async- Oct 19 '16 at 11:49
  • You can’t. Read the doc link I pasted above. (I see Oleg also pasted it their answer.) For security reasons, Apple **does not support `#` in `tel:` URLs**, full stop. As for why you needed the `!`, and why my code fixed it, see this: http://stackoverflow.com/questions/24018327/what-does-an-exclamation-mark-mean-in-the-swift-language/33399973#33399973 – Paul Cantrell Oct 19 '16 at 14:56