iOS Swift:无法检索 UIwebView 中加载的当前 url

iOS Swift : can't Retrieve current url loaded in UIwebView

我正在使用 webview 应用程序 iOS swift :

我面临的问题是我想获取 Webview 显示的每个 url :

在 urls 我必须执行一些 if else 检查。但我无法获取 webview 加载的 urls。

webview 正在显示良好的结果并在单击 时加载 urls。我想获取 webview 导航的所有 urls..

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.


    }



}

我想要的只是获取当前 url 的字符串和 webview 的导航按钮:请帮忙?我参考了所有互联网没有解决方案适用于我的情况。

选项-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)")
}

有几个 UIWebViewDelegate 可以帮助您实现目标,例如 shouldStartLoadWithRequestwebViewDidStartLoadwebViewDidFinishLoaddidFailLoadWithError。如果你想在视图完成后执行操作,那么实现这个委托方法

Swift 3

if let currentURL = webView.request?.url?.absoluteString{
         print(currentURL)
    }

因为它提到

首先,您必须为 WebView class 定义委托。 "WebView.delegate = self"。在上面的函数之一旁边添加此代码:

override func viewDidLoad() {
    super.viewDidLoad()
    myWebView.delegate = self  }

例如:

func webViewDidFinishLoad(_ webView: UIWebView) {
    if let currentURL = WebView.request?.url?.absoluteString{
        print(currentURL)
    }
}