Swift 3 WKWebView 'URLRequest'; did you mean to use 'as' to explicitly convert? ( BUG )

11,959

Use URL and URLRequest instead:

let url = URL(string: "https://facebook.com")!
webView.load(URLRequest(url: url))

This is quite similar to https://stackoverflow.com/a/37812485/2227743: you either use NSURL and have to downcast it as URL, or you directly use the new Swift 3 structs.

If you follow the error message, your example would become:

let url = NSURL(string: "https://facebook.com")!
webView.load(URLRequest(url: url as URL))

It could be even worse:

let url = NSURL(string: "https://facebook.com")!
webView.load(NSURLRequest(url: url as URL) as URLRequest)

All this works but of course it's much better to start using URL and URLRequest in Swift 3, without using all this downcasting.

Share:
11,959
SwiftDeveloper
Author by

SwiftDeveloper

I coding iOS & Swift applications.

Updated on June 07, 2022

Comments

  • SwiftDeveloper
    SwiftDeveloper almost 2 years

    Hello when I use WKWebView codes with Swift 3 gives me this error

    'URLRequest'; did you mean to use 'as' to explicitly convert?

    I think this is bug I need help or ideas ? My codes under below Thanks

    import UIKit
    import WebKit
    
    class SocialsViewController: UIViewController, WKNavigationDelegate {
    
    
        var webView = WKWebView()
    
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            let url = NSURL(string: "https://facebook.com")!
            webView.loadRequest(NSURLRequest(url: url))
            webView.allowsBackForwardNavigationGestures = true
    
    
        }
    
    
    }
    
  • Eric Aya
    Eric Aya over 7 years
    No. In Swift 3, it's load, not loadRequest, and I've already answered with a correct solution.
  • Nirmal Bajpai
    Nirmal Bajpai over 7 years
    Yes! Mr Eric you already provide correct solution. I just change name to differentiate argument "url" and variable of URL struct "myUrl". which is little confusing for freshers. Thanks