2016-10-22 68 views
0

我有webView并发送我的cookie到我的网站使用相同的会话(网上商店)。如何获取在UIWebView中加载的页面的http://地址?

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中加载页面的网址?

+1

[获取UIWebview的当前URL]的可能重复(http://stackoverflow.com/questions/2491410/get-current-url-of-uiwebview) –

回答

0

请让你的类使用

webView.delegate = self 

然后重写委托方法web视图的委托,

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

这会给当前的URL将被加载

0

使用UIWebView代表方法

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