Get clicked linked on uiwebview navigating on m.yo

2019-06-06 06:32发布

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.

4条回答
【Aperson】
2楼-- · 2019-06-06 07:01

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 :)

查看更多
Fickle 薄情
3楼-- · 2019-06-06 07:06

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;
}
查看更多
再贱就再见
4楼-- · 2019-06-06 07:11

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];
}
查看更多
虎瘦雄心在
5楼-- · 2019-06-06 07:14

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!

查看更多
登录 后发表回答