如何在智能应用横幅上叮当声时加载我在webView中导航的链接?

时间:2016-05-04 09:14:42

标签: ios swift wkwebview

我正在使用智能应用横幅推广应用程序,效果很好!但是,当我在智能应用横幅上叮当作响时,我想加载我在webView中导航的链接(我使用的是WKWebView)。

以下是我在AppDelegate.swift文件中使用的以下代码:

var vc = ViewController()

func application(application: UIApplication, openURL url: NSURL,sourceApplication: String?, annotation: AnyObject) -> Bool {

let url1 = NSURL(string:"\(url)")!

self.vc.webView!.loadRequest(NSURLRequest(URL: url1!))
return true
}

它没有用!

1 个答案:

答案 0 :(得分:2)

你可以试试这个:

app appate中的

func application(application: UIApplication, openURL url: NSURL,sourceApplication: String?, annotation: AnyObject) -> Bool {

let url1 = NSURL(string:"\(url)")!

NSNotificationCenter.defaultCenter().postNotificationName("OpenLinkNotification", object: url1)

return true
}

在视图控制器中:

override func viewDidLoad() {
        super.viewDidLoad()

        NSNotificationCenter.defaultCenter().addObserver(self, selector: "methodOfReceivedNotification:", name:"OpenLinkNotification", object: nil)
       ...
}

...

func methodOfReceivedNotification(notification: NSNotification){
    let url = notification.object as NSURL
    self.vc.webView!.loadRequest(NSURLRequest(URL: url!))
}

override func viewDidDisappear(animated: Bool) {
    NSNotificationCenter.defaultCenter().removeObserver(self)
}