当按下html target =“_ blank”的链接时,WKWebView for navigationAction函数不会从另一个swift类调用

时间:2017-09-13 12:58:12

标签: ios swift wkwebview uiwebviewdelegate

我的ViewController类是:

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

}

}

我的另一个课程是:

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

我需要调用名为webView的最后一个函数。虽然我从ViewController类传递uiDelegate / navigationDelegate,但是当我按下任何链接时它不会调用(可能是错误的实现,不清楚uiDelegate或navigationDelegate)。我想在用户点击Play商店链接或社交链接等外部链接时调用此功能,并在默认浏览器上打开该外部链接。

如果我将所有代码放在ViewController类上,它可以正常工作。但我需要从另一个类调用,因为我想创建一个将返回WKWebView的框架。

1 个答案:

答案 0 :(得分:0)

您将ViewController指定为委托,并且已在AnotherClass中实施了委托。如果您想在AnotherClass

中保留代表,请尝试此操作
public func bannerView(nd: WKNavigationDelegate, ud: WKUIDelegate) -> WKWebView{
    myWebView.uiDelegate = self
    myWebView.navigationDelegate = self