当打开带有深层链接的应用程序时,无法在WKWebView中加载新的URL

时间:2018-11-25 21:08:01

标签: ios swift xcode webview wkwebview

我一直在尝试在我的应用程序中实现自定义协议深层链接,以使该应用程序打开时会构造正确的URL字符串,然后在ViewController中使用WKWebVeiw来显示页面。

无论我做什么,URL要么都不会加载,否则会抛出错误,表明WKWebView为nil。

这是我正在使用的代码:

AppDelegate.swift中:

func application(_ application: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {

    var urlString:String = "https://example.com"

    print(url.scheme!)

    if url.host != "example.com" {
        print(url.host ?? "")
        urlString += url.host ?? ""
    }
    print(url.path)
    urlString += url.path
    print(url.query ?? "")
    urlString += url.query ?? ""

    print(urlString)
    if verifyUrl(urlString: urlString) {
        vC.url = URL(string: urlString)!
    }

    return true
}

ViewController.swift中:

var url: URL! = URL(string: "https://example.com")!

func loadPage() {
    let urlRequest:URLRequest = URLRequest(url: url)

    webView.load(urlRequest)
}

我尝试过:

  • 在URL变量上使用属性观察器(导致Fatal Error: Unexpectedly found nil while unwrapping an Optional value

  • loadPage()呼叫AppDelegate.swift(同样的事情)

  • loadPage()中调用viewDidAppear()(不执行任何操作)

到目前为止没有任何工作。您可以提供的任何帮助都是巨大的帮助。预先感谢!

0 个答案:

没有答案