2016-10-22 58 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; 
} 
相關問題