3

im working on WKWebView and when i load a url without parameters like this it works fine

func loadAddress(lat:Double,lng:Double){
    let requestURL = NSURL(string:"http://url.com/dev2/billboard/geo")
    let request = NSURLRequest(url: requestURL as! URL)
    webview.load(request as URLRequest)
}

but i want to send user's location with get parameters like this

func loadAddress(lat:Double,lng:Double){
    let requestURL = NSURL(string:"http://url.com/dev2/billboard/geo/\(lat)/\(lng)")
    let request = NSURLRequest(url: requestURL as! URL)
    webview.load(request as URLRequest)
}

it shows this error:

fatal error: unexpectedly found nil while unwrapping an Optional value 2017-02-02 16:43:36.645898 pixel[5112:1996136] fatal error: unexpectedly found nil while unwrapping an Optional value

thanks is advance

Wasif Khalil
  • 2,217
  • 9
  • 33
  • 58
  • If the first function works, then there is issue with the requestURL. In case of second function, check the url exists or not trying to load it in safari browser of iPhone. – Istiak Morsalin Feb 02 '17 at 11:57
  • Can you step through your function and please check what value is getting `nil`? Is it `requestURL`? And try to use the new versions of the API without the NS prefixes. – kiecodes Feb 02 '17 at 12:01

1 Answers1

8

Try to encode your url and use Swift native type URL and URLRequest instead of NSURL and NSURLRequest.

let stringUrl = "http://url.com/dev2/billboard/geo/\(lat)/\(lng)"
if let encodedURL = stringUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed),
   let url = URL(string: encodedURL) {
      webview.load(URLRequest(url: url))
}

Edit: I have tried url that you have added in comment it is showing perfectly when you encode it with urlQueryAllowed. May be you have not added NSAppTransportSecurity with your info.plist file try to add once and it will works for you too.

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>
Nirav D
  • 71,513
  • 12
  • 161
  • 183
  • 1
    I was thinking almost the same that the doubles injected into the URL mess up the URL format and therefore URL(string:) return nil. – kiecodes Feb 02 '17 at 11:55
  • when i print(url.absoluteString) the url it shows is correct but the problem is still same – Wasif Khalil Feb 02 '17 at 11:57
  • It should be the same. Because you need to check the url if its correct or not. I had the same issue just a week ago @WasifKhalil – Istiak Morsalin Feb 02 '17 at 11:58
  • @WasifKhalil Try once `.urlPathAllowed` and `.urlHostAllowed` encoding also. – Nirav D Feb 02 '17 at 11:59
  • @WasifKhalil Also is your url valid? Have you check on browser as Jason suggested? – Nirav D Feb 02 '17 at 12:00
  • im now guessing the problem is something else because i tried to pass string in a hardcoded url and the problem is same? what could be wrong? like this http://url.com/dev2/billboard/geo/abc/def – Wasif Khalil Feb 02 '17 at 12:03
  • my url with lat lng in the log is valid, i have checked it in browser – Wasif Khalil Feb 02 '17 at 12:03
  • @WasifKhalil Have you tried with `.urlPathAllowed` and `.urlHostAllowed` once? – Nirav D Feb 02 '17 at 12:05
  • @WasifKhalil got 404 for this http://lyrics.url.com/dev2/billboard/geo/abc/def url. – Istiak Morsalin Feb 02 '17 at 12:07
  • .urlHostAllowed messed up by url like this "http:%2F%2Fmigapixel.com%2Fdev2%2Fbillboard%2Fgeo%2F24.8212602082871%2F67.0793047361701" and with urlPathAllowed the url was correct but still didnt work – Wasif Khalil Feb 02 '17 at 12:09
  • @JasonBourne the actual url is http://migapixel.com/dev2/billboard/geo/24.8211901802791/67.0792666255154 – Wasif Khalil Feb 02 '17 at 12:09
  • @WasifKhalil does the above url works with your code? – Istiak Morsalin Feb 02 '17 at 12:10
  • yes it works on browser http://migapixel.com/dev2/billboard/geo/24.8211901802791/67.0792666255154 – Wasif Khalil Feb 02 '17 at 12:10
  • yes it does not even work hardcoded whoever migapixel.com/dev2/billboard/geo this works fine without error on ios but i need to send location – Wasif Khalil Feb 02 '17 at 12:13
  • tried hardcoded but doesnt work like this, let url = URL(string: "http://migapixel.com/dev2/billboard/geo/24.8211679235332/67.0792313945173") – Wasif Khalil Feb 02 '17 at 12:16
  • @WasifKhalil I have tried with the url that you given with `urlQueryAllowed` like my answer, First it will not show anything after that I have add `App Transport Security Settings` in `info.plist` file and it is showing perfectly in my simulator check this image also https://i.stack.imgur.com/YUIcl.png If you doesn't add `Transport Security Settings` in plist try to add and it will works for you too – Nirav D Feb 02 '17 at 12:19
  • i already added Transport Security Settings, said i said it works without parameters – Wasif Khalil Feb 02 '17 at 12:47
  • @WasifKhalil It is perfectly working for me url that you have added with lat long this one http://migapixel.com/dev2/billboard/geo/24.8211679235332/67.0792313945173 You can check this simulator image of my https://i.stack.imgur.com/YUIcl.png – Nirav D Feb 02 '17 at 12:49
  • @NiravD yes i believe you but for some reason it doesnt work for me and im scratching my head :S i was trying on my phone, going to try simulator now – Wasif Khalil Feb 02 '17 at 12:52
  • @WasifKhalil Ok, And Are you still using `urlQueryAllowed` encoding like my answer right? – Nirav D Feb 02 '17 at 12:53
  • Thanks! the same problem/answer here [link](https://stackoverflow.com/a/55909990/5869217) – Álvaro Agüero Apr 29 '19 at 19:58