每当更改网址时,如何从webView获取网址?
我想在更改当前网址时更改按钮颜色。所以我需要一直检查当前的URL。
我还希望将URL值作为字符串。
我尝试了以下代码,但它根本不起作用。
NotificationCenter.default.addObserver(self, selector: #selector(urlChecker), name: NSNotification.Name.NSURLCredentialStorageChanged, object: webView)
答案 0 :(得分:1)
你可以使用:
<强> UIWebViewDelegate 强>
https://developer.apple.com/reference/uikit/uiwebviewdelegate/1617945-webview
optional func webView(_ webView: UIWebView,
shouldStartLoadWith request: URLRequest,
navigationType: UIWebViewNavigationType) -> Bool
<强> UIWebViewNavigationType:强>
https://developer.apple.com/reference/uikit/uiwebviewnavigationtype
请勿忘记返回true
case linkClicked
用户点击了一个链接。
case formSubmitted
用户提交了一份表格。
case backForward
用户点击后退或前进按钮。
case reload
用户点击了重新加载按钮。
case formResubmitted
用户重新提交表单。
case other
发生了其他一些行动。
注入Javascript JavaScript MessageHandler
(归功于Vasily Bodnarchuk)
解决方案在这里:https://stackoverflow.com/a/40730365/1930509
Swift 3的例子。
描述
将脚本插入到将在WKWebView中显示的页面中。 此脚本将返回页面URL(但您可以编写另一个URL) JavaScript代码)。这意味着脚本事件就生成了 网页,但它将在我们的功能中处理:
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {...}
完整代码示例
import UIKit import WebKit class ViewController: UIViewController, WKNavigationDelegate { var webView = WKWebView() let getUrlAtDocumentStartScript = "GetUrlAtDocumentStart" let getUrlAtDocumentEndScript = "GetUrlAtDocumentEnd"
override func viewDidLoad() {
super.viewDidLoad()
let config = WKWebViewConfiguration()
config.addScript(script: WKUserScript.getUrlScript(scriptName: getUrlAtDocumentStartScript),
scriptHandlerName:getUrlAtDocumentStartScript, scriptMessageHandler:
self, injectionTime: .atDocumentStart)
config.addScript(script: WKUserScript.getUrlScript(scriptName: getUrlAtDocumentEndScript),
scriptHandlerName:getUrlAtDocumentEndScript, scriptMessageHandler:
self, injectionTime: .atDocumentEnd)
webView = WKWebView(frame: UIScreen.main.bounds, configuration: config)
webView.navigationDelegate = self
view.addSubview(webView)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
webView.loadUrl(string: "http://apple.com")
}
}
extension ViewController: WKScriptMessageHandler { func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) { switch message.name { case getUrlAtDocumentStartScript: print("start: \(message.body)") case getUrlAtDocumentEndScript: print("end: \(message.body)") default: break; } } } extension WKUserScript { class func getUrlScript(scriptName: String) -> String { return "webkit.messageHandlers.\(scriptName).postMessage(document.URL)" } } extension WKWebView { func loadUrl(string: String) { if let url = URL(string: string) { load(URLRequest(url: url)) } } }
extension WKWebViewConfiguration {
func addScript(script: String, scriptHandlerName:String, scriptMessageHandler: WKScriptMessageHandler,injectionTime:WKUserScriptInjectionTime) {
let userScript = WKUserScript(source: script, injectionTime: injectionTime, forMainFrameOnly: false)
userContentController.addUserScript(userScript)
userContentController.add(scriptMessageHandler, name: scriptHandlerName)
}
}
的Info.plist
添加Info.plist传输安全设置
<key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryLoads</key> <true/> </dict>
结果
资源##