WebView load crashes on url opening

459 Views Asked by At

I have an app that retrieves urls from a json and when the user clicks on a button the UIWebView opens to the url. The problem is that when I added utm codes to the urls the app started to crash and the UIWebView returns 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? Thank you in advance.
I call the UiWebView this way:

self.webView.load(URLRequest(url: URL(string: self.link)!))
1

There are 1 best solutions below

0
nbtown On

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.

However, to avoid unexpected crashes, you should safe unwrap your URL instead of explicitly unwrapping it by using URL(string: self.link)!.

You could for example create an if let:

if let safeURL = URL(string: self.link) {
self.webView.load(URLRequest(url: url)
}