-1

I have link as below,

https://payood.test/loofpay/XUYZGlobal/WebForms/checkoutservice%20.aspx?paymentchannel=ddd&isysid=37268474138868&amount=25&description=Transaction From XXX&description2=dsdsd&tunnel=&original=ZXz4kfH9fiVIZ1jWBaGjww3hgwX84CGAahlCcsKWXvs%3d&responseUrl=http://localhost:55766/dsss/Response.aspx&hash=BE0481E5F9AA1C9F5B26A8E93A6ACAAD5888EDE9

When I try to open, its crashing saying below error.

fatal error: unexpectedly found nil while unwrapping an Optional value

Below is the code I am using

link = above link....
webView.loadRequest(URLRequest(url: URL(string: link)!))

Note:

If I use simple link as http://www.google.com, it works.

Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278
Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276

3 Answers3

2

The link you've posted isn't a valid URL. It includes spaces in your description. You didn't encode this correctly.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • this is what I received from the web api... I tried to various `addingPercentEncoding` but still have same issue... – Fahim Parkar Dec 03 '18 at 14:15
  • `link = link.replacingOccurrences(of: " ", with: "%20")` I add this line manually and now its working... I had no other option then this... – Fahim Parkar Dec 03 '18 at 14:23
0

As others mentioned, the problem is the link you provide to URL initializer is not a valid url and because of the !, your code can not initial a URL from the string in the following code and it will crash:

URL(string: link)!

So you have to change the string to some valid url before initializing a URL from it. Like this:

guard let escapedURLString = link.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) else {
    fatalError("Unknown URL string:\(link)")
}

guard let finalURL = URL(string: escapedURLString) else {
    fatalError("Can not create a url from:\(escapedURLString)")
}

print(finalURL) //to check if it works
webView.loadRequest(URLRequest(url: finalURL))
Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278
0

I found that url is already encoded but API have issues that they are sending spaces in url.

So I replace spaces with %20

link = link.replacingOccurrences(of: " ", with: "%20")

This way all is working fine now.

Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276