gpt4 book ai didi

ios - 当按下带有 html 目标 ="_blank"的链接时,用于 navigationAction 函数的 WKWebView 不会从另一个 swift 类调用

转载 作者:行者123 更新时间:2023-11-30 12:09:58 25 4
gpt4 key购买 nike

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

}

}

我的另一个类(class)是:

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 的框架。

最佳答案

您正在将 ViewController 分配为委托(delegate),并且您已在 AnotherClass 中实现了委托(delegate)。如果您想将委托(delegate)保留在 AnotherClass

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

关于ios - 当按下带有 html 目标 ="_blank"的链接时,用于 navigationAction 函数的 WKWebView 不会从另一个 swift 类调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46198270/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com