[英]WebView load crashes on url opening
I have an app that retrieves urls from a json and when the user clicks on a button the UIWebView opens to the url.我有一个从 json 检索 url 的应用程序,当用户单击按钮时,UIWebView 打开到 url。 The problem is that when I added utm codes to the urls the app started to crash and the UIWebView returns nil.
问题是当我将 utm 代码添加到 url 时,应用程序开始崩溃并且 UIWebView 返回 nil。 My guess is that the symbol "&" cannot be resolved by the app and it crashes.
我的猜测是应用程序无法解析符号“&”并且它崩溃了。 I tried to modify the url by putting the encoded symbol "%26" but then I have the problem that Google Analytics doesn't read it... Is there a way to make the "&" symbol work?
我试图通过输入编码符号“%26”来修改 url,但后来我遇到了 Google Analytics 无法读取它的问题……有没有办法让“&”符号起作用? Thank you in advance.
先感谢您。
I call the UiWebView this way:我这样称呼 UiWebView:
self.webView.load(URLRequest(url: URL(string: self.link)!))
As mentioned in the comments, it's hard to tell what exactly is wrong with your string that you are trying to convert into an URL without seeing it.正如评论中提到的,很难在没有看到它的情况下说出您试图转换为 URL 的字符串到底有什么问题。
However, to avoid unexpected crashes, you should safe unwrap your URL instead of explicitly unwrapping it by using URL(string: self.link)!
但是,为避免意外崩溃,您应该安全地打开 URL 而不是使用
URL(string: self.link)!
. .
You could for example create an if let:例如,您可以创建一个 if let:
if let safeURL = URL(string: self.link) {
self.webView.load(URLRequest(url: url)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.