Get clicked linked on uiwebview navigating on m.youtube.com on ios

有些话、适合烂在心里 提交于 2019-12-08 04:17:50

问题


I use uiwebview on my ios application. I have textbox above it and it navigates the uiwebview. I enter m.youtube.com and the webview perfectly loads it. I made a search on the youtube.com and the results is loaded perfectly. When I click any of the video I go to the video page, and I want to have that url link of the page will appear with video to watch. m.youtube.com/watch?v=sa+2DFdfd .. but how ?

search notes:

When I try this on my iphone 5 using safari I see the navigation bar changes perfectly according to which page I am in. in this example I see; m.youtube.com/watch?v=34124S on safari but I can not cacth it on uiwebview.... I can not get the latest loaded web page link.

ShouldLoadStart does only work only for the first time page loaded. Then when you click on the page; if the link is type of <a href='/someLink' /> link the ShouldLoadStart catches the link but if it is a made of javascript type of link , the clicked link is not caught by delegates. Please help to catch m.youtube.com/watch?v=34124S like safari does on youtube pages.


回答1:


If you want to store the url this will do:

NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:@"window.location"];

or

currentURL = currentWebView.request.URL.absoluteString;

Try above two in

-(void)webViewDidFinishLoad:(UIWebView *)webView

Also this can be done:

-(void)webViewDidFinishLoad:(UIWebView *)webView
{
  NSLog(@"%@",webView.request.mainDocumentURL);

 [_activityindicator stopAnimating];
}



回答2:


implement this in your webview delegate class

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType 
{
    //CAPTURE USER LINK-CLICK.
    NSURL *url = [request URL];
    NSLog(@"clicked url: %@",[url absoluteString]);

    return YES;
}



回答3:


I got the link by using central notification. when the video appears full view, I catch that event and I get the loaded pages inner html and get the unique link..

If you have better idea please share below!




回答4:


I had the same issue. If it is the case you can use WKWebView.

For UIWebView the only solution I found is to get current URL via javascript:

    [webView stringByEvaluatingJavaScriptFromString: @"location.href"];

The next issue is that shouldStartLoadWithRequest is not triggered. To solve it you can override requests with NSURLProtocol (btw it's not possible with WKWebView). Or just check current URL every second by timer :)



来源:https://stackoverflow.com/questions/18692540/get-clicked-linked-on-uiwebview-navigating-on-m-youtube-com-on-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!