My ViewController Class is:

import UIKit
import WebKit

class ViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {

@IBOutlet weak var testView: UIView!

override func viewDidLoad() {
    super.viewDidLoad()

    let b = AnotherClass(mUrl: "http://touhidapps.com")
    b.myWebView.navigationDelegate = self
    self.testView.addSubview(b.bannerView(nd: self, ud:self))

}

}

My another class is:

import Foundation
import WebKit

class AnotherClass: NSObject, WKUIDelegate, WKNavigationDelegate {
public var myWebView = WKWebView()

var u = ""

public init(mUrl: String) {
    u = mUrl
}

public func bannerView(nd: WKNavigationDelegate, ud: WKUIDelegate) -> WKWebView{
    myWebView.uiDelegate = ud
    myWebView.navigationDelegate = nd

    let jscript = "var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);"
    let userScript = WKUserScript(source: jscript, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
    let wkUController = WKUserContentController()
    wkUController.addUserScript(userScript)
    let wkWebConfig = WKWebViewConfiguration()
    wkWebConfig.userContentController = wkUController
    wkWebConfig.preferences.javaScriptEnabled = true
    wkWebConfig.preferences.javaScriptCanOpenWindowsAutomatically = true
    myWebView = WKWebView(frame: CGRect(x: 0, y: 0, width: 320, height: 300), configuration: wkWebConfig)

    let mmUrl = u
    NSLog("URL:1 " + mmUrl, "")

    if let myUrl = URL(string: mmUrl){
        let myRequest = URLRequest(url: myUrl)

        myWebView.load(myRequest)
        myWebView.configuration.allowsInlineMediaPlayback = true
        myWebView.configuration.allowsAirPlayForMediaPlayback = true
        myWebView.configuration.allowsPictureInPictureMediaPlayback = true
        myWebView.allowsLinkPreview = false
        myWebView.allowsBackForwardNavigationGestures = true
    }

    return myWebView
}

  func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
    // A nil targetFrame means a new window (from Apple's doc)
    NSLog("Test method call from another class", "")

    if (navigationAction.targetFrame == nil) {
        if #available(iOS 10.0, *) {
            UIApplication.shared.open(navigationAction.request.url!, options: [:], completionHandler: nil)
        } else {
            UIApplication.shared.openURL(navigationAction.request.url!)
        }
        return nil
    }
    return nil;
}
}

I need to call the last function named webView. It is not calling when I press any link although I pass uiDelegate/navigationDelegate both from ViewController class(May be wrong implementation, not clear about uiDelegate or navigationDelegate). I want to call this function when user clicks on external link like play store link or social link and open that external link on default browser.

It works fine if I place all codes on ViewController class. But I need to call from another class because I want to make a framework which will return WKWebView.

1

There are 1 answers

1
Torongo On

You are assigning ViewController as delegate and you have implemented delegates in AnotherClass. Try this if you want to hold the delegates in AnotherClass

public func bannerView(nd: WKNavigationDelegate, ud: WKUIDelegate) -> WKWebView{
    myWebView.uiDelegate = self
    myWebView.navigationDelegate = self