简体   繁体   中英

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

I have webView and send my cookies to my site for using same session (web store).

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];

User can walking on my site and i must to know where he is. How can i get web address of loaded page in webView?

Please make your class the delegate of the webview using

webView.delegate = self

Then override the delegate method,

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

This will give the current url going to be loaded

use UIWebView delegate method

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM