如何获取UIWebView加载页面的http://地址?

How to get http:// address of page loaded in UIWebView?

我有 webView 并将我的 cookie 发送到我的站点以使用相同的会话(网上商店)。

UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetMaxX(self.view.frame), CGRectGetMaxY(self.view.frame))];
[webView setDelegate:self];
[self.view addSubview:webView];

NSString *sessionId = [[OCRESTAPIClient sharedClient] sessionId];
NSString *value = [@"xid=" stringByAppendingString:sessionId];

NSURL *url = [NSURL URLWithString:@"http://MySite.ru/cart"];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setValue:value forHTTPHeaderField:@"Cookie"];

[webView loadRequest:request];

用户可以在我的网站上行走,我必须知道他在哪里。如何在 webView 中获取加载页面的网址?

请使用

让您的 class 成为 webview 的代表
webView.delegate = self

然后重写委托方法,

func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        print("current url is \(request.mainDocumentURL!.absoluteString)")
        return true;
}

这将提供当前 url 将被加载

使用UIWebView委托方法

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
  self.url = webView.request.mainDocumentURL;
}