Schmopsel Schmopsel - 1 month ago 8
iOS Question

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?

Answer

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