Double hashtag in openURL() crashes the app

痞子三分冷 提交于 2019-12-11 04:46:29

问题


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:

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?


回答1:


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.




回答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.



来源:https://stackoverflow.com/questions/38658649/double-hashtag-in-openurl-crashes-the-app

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!