S.D S.D - 7 months ago 27
Swift Question

How to load the link i’m navigating in my webView when clinking on the Smart App Banner?

I am using a Smart App Banner to promote an application and it works well! However, I would like to load the link i’m navigating in my webView(I am using a WKWebView) when clinking on the Smart App Banner.

Here is the following code that I am using in my AppDelegate.swift file:

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
}


and it's not working!

Answer

You can try this:

in app delegate:

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
}

in the view controller:

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)
}