iosswiftuiwebviewxcode-6.2

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


i am working in webview application iOS swift :

problem i am facing is i want to get each url which webview displays :

upon urls i have to perform some if else checks . but i could not get the urls which webview loads.

webview is displaying fine results and loading urls upon clicking . i want to fetch all urls which webview navigates on..

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.


    }



}

all i want is to get string of current url and navigation buttons for webview :please help ? i referred all the internet no solution is working in my case .


Solution

  • There are couple of UIWebViewDelegate such as shouldStartLoadWithRequest or webViewDidStartLoad or webViewDidFinishLoad or didFailLoadWithError that help you to accomplish your goal. If you want to perform operation after view did finished then implement this delegate method

    Swift 3

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

    as it mentioned here.