iOS Swift:不能检索当前在UIwebView中加载的URL
我在webview应用程序iOS swift中工作:
我面临的问题是我想要获取每个网页显示的url:
在url上,我必须执行一些其他检查。 但我无法得到WebView加载的url。
webview显示效果很好,点击后加载url。 我想获取所有的Web浏览器的导航
import Foundation import UIKit class seconVC: UIViewController { var toPass:String! var touser:String! var toPassing:String! @IBOutlet weak var webV: UIWebView! @IBOutlet weak var L_back: UIBarButtonItem! @IBOutlet weak var R_frwrd: UIBarButtonItem! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. var domurl = toPass; var username = touser; var passwing = toPassing; var sendurl = stringA+username+"/"+passwing; //exchange login with auth/username/password println(sendurl); let url = NSURL (string: sendurl); let requestObj = NSURLRequest(URL: url!); webV.userInteractionEnabled = true webV.loadRequest(requestObj); } @IBAction func L_back(sender: UIBarButtonItem) { if (webV.canGoBack){ webV.goBack() } } @IBAction func R_frwrd(sender: UIBarButtonItem) { if (webV.canGoForward){ webV.goForward() } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
我想要的是为webview获取当前的url和导航button的string:请帮忙? 我提到所有的互联网没有解决scheme在我的情况下工作。
有几个UIWebViewDelegate
,如shouldStartLoadWithRequest
或webViewDidStartLoad
或webViewDidFinishLoad
或didFailLoadWithError
,可以帮助您实现您的目标。 如果要在视图完成后执行操作,则执行此委托方法
// Swift 3
如果让currentURL = webView.request?.url?.absoluteString {print(currentURL)}
就像这里提到的那样。
选项1
let currentURL = webV.request.URL.absoluteString
选项2
在委托方法中获取URL
func webViewDidFinishLoad(webView: UIWebView){ print(WebView.request?.mainDocumentURL) }
选项-3
func webViewDidFinishLoad(webView: UIWebView) { let currentURL = webView.request().URL() print("\(currentURL.description)") }
或者使用像
func webViewDidFinishLoad(webView: UIWebView){ let currentURL = webView.request?.URL }
选项-4
func webViewDidFinishLoad(webView: UIWebView) { let currentURL: = webView.stringByEvaluatingJavaScriptFromString("window.location.href")! print("\(currentURL)") }
选项-5
func webView(webview: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool { let currentURL = request.URL.absoluteString print("\(currentURL)") }
选项-6
func webViewDidFinishLoad(WebVie: UIWebView) { let currentURL = WebVie.request.mainDocumentURL print("\(currentURL)") }