openURL() 中的双标签会导致应用程序崩溃 [英] Double hashtag in openURL() crashes the app

查看:56
本文介绍了openURL() 中的双标签会导致应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过该应用拨打电话,目前可以正常使用.该号码由两个数字组成:电话号码和密码,后跟主题标签.但是有些用户需要在数字末尾添加双标签,这会导致应用程序在立即调用该 URL 时崩溃:

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

使用单个主题标签就可以了,并且可以在设备拨号后按键盘上的主题标签.我试图附加 ASCII 表 - 十六进制数,23(意味着 #) - 它没有帮助.

我在

但是当尝试拨打这个号码 (NSURL) 时 - 应用程序仍然崩溃.如何在 URL 末尾仍然有双主题标签而不会使应用程序崩溃?

解决方案

问题是 NSURL 认为这不是一个有效的 URL.这:

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

...返回零.问题不在于逃避;显然 NSURL 不接受任何带有两个 # 符号的东西.(将其粘贴到操场上自己进行实验.)它看起来应该,因此您可能会将其作为 Apple 的错误提交.但是,请注意Apple 提供的内容:

<块引用>

为了防止用户恶意重定向电话或更改电话或帐户的行为,电话应用支持电话方案中的大部分(但不是全部)特殊字符.具体来说,如果 URL 包含 * 或 # 字符,则电话应用程序不会尝试拨打相应的电话号码.

<小时>

我不知道为什么这段代码会编译:

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

NSURL(string:) 是一个可失败的初始化器,这意味着它返回一个可选的,而 openURL() 的 arg 是非可选的.这会提示您进行这样的检查,以防止崩溃:

if let url = NSURL(string: "tel//:1111111,,222##") {UIApplication.sharedApplication().openURL(url)} 别的 {showErrorMessage("无法拨打此号​​码")}

您是否在为这个问题修剪它时删除了 NSURL 构造函数之后有一个 ! ?如果是这样,那将是您崩溃的原因.

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?

解决方案

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.

这篇关于openURL() 中的双标签会导致应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆