Get current URL of UIWebView

后端 未结 14 1959
天命终不由人
天命终不由人 2020-11-29 16:08

I already tried getting the current URL of my UIWebView with: webview.request.URL. Unfortunately the NSURL was empty. Anything wrong h

14条回答
  •  再見小時候
    2020-11-29 16:36

    This is not correct, and will return a nil:

    NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:@"window.location"];
    

    However, the code below can get a URL, but the url may not be the current URL:

    NSString *url = _webView.request.URL.absoluteString;
    

    The correct one is:

    NSString *currentURL = [_webView stringByEvaluatingJavaScriptFromString:@"window.location.href"];
    

提交回复
热议问题