Open in Safari from UIWebView (using youtube.com)

家住魔仙堡 提交于 2019-12-11 15:04:03

问题


This has been the most frustrating thing I've ever had the misfortune of doing, ever. I have a UIWebView. It goes to http://m.youtube.com. I want any subsequent link I press to then be sent to Safari, not the UIWebView. Simple, right? Here's the code you'll find over and over again:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    if (navigationType == UIWebViewNavigationTypeLinkClicked) {

        [[UIApplication sharedApplication] openURL:request.URL];
        return NO;
    }

    return YES;
}

But this doesn't work. So I changed UIWebViewNavigationTypeLinkClicked to UIWebViewNavigationTypeOther. This instantly open in Safari when the view loads. Again not what I want. So I did this:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    if (navigationType == UIWebViewNavigationTypeOther) {

        NSLog(@"It works!");

//        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[urlArr objectAtIndex:indexPath.row]]];
       return YES;
   }

   return NO;
}

to check if subsequent clicks even go through here and they don't. The log only shows "It works!" when the view initially loads. Any subsequent clicks show nothing.

So I've tried looking out for a URL change and Javascript change but still nothing. Is there anything else I can do?

(By the way I should mention that this code does work on any other website, like stackoverflow.com - any subsequent link clicks and I'm taken to Safari. It only doesn't work with YouTube)

This is what I get from the log when I press a link on youtube:

2013-01-24 21:08:25.886 HatFilms[30079:c07] [MPAVController] Autoplay: Enabling autoplay
2013-01-24 21:08:25.886 HatFilms[30079:c07] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 0, on player: 1)
2013-01-24 21:08:25.887 HatFilms[30079:c07] setting movie path: http://r1---sn-5n3-n1qe.c.youtube.com/videoplayback?mv=m&mt=1359061639&ms=au&yms=Ag6pZvjqA20&source=youtube&key=yt1&el=watch&upn=g2ConWh5QJ0&id=201015c58a31c984&cp=U0hUTlBTVF9LS0NONF9ITVhCOlhkN3FFcm5zWDY5&ip=31.205.57.12&newshard=yes&fexp=909714%2C901802%2C919358%2C914066%2C916625%2C902534%2C920704%2C912806%2C922403%2C922405%2C929901%2C913605%2C925710%2C929104%2C920201%2C913302%2C919009%2C911116%2C926403%2C910221%2C901451&sparams=cp%2Cid%2Cip%2Cipbits%2Citag%2Cratebypass%2Csource%2Cupn%2Cexpire&sver=3&expire=1359086161&dnc=1&app=youtube_mobile&ratebypass=yes&itag=18&signature=A434075DABFEBB44E5F0588B7430D04A931684B0.648F49C3F4D254E29B970A6AA68BAC1C08280B97&ipbits=8
2013-01-24 21:08:25.887 HatFilms[30079:c07] [MPAVController] Autoplay: Enabling autoplay
2013-01-24 21:08:25.895 HatFilms[30079:c07] [MPCloudAssetDownloadController] Prioritization requested for media item ID: 0

And if I log everything that the UIWebView receives:

2013-01-24 21:50:31.642 HatFilms[30372:c07] [MPAVController] Autoplay: Enabling autoplay
2013-01-24 21:50:31.642 HatFilms[30372:c07] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 0, on player: 1)
2013-01-24 21:50:31.642 HatFilms[30372:c07] setting movie path: http://r1---sn-5n3-n1qe.c.youtube.com/videoplayback?expire=1359086161&key=yt1&sver=3&app=youtube_mobile&el=watch&signature=44DA69CF58A649EAA813EE2A8E91B38BB164CB9D.2DA718585DEDB6F55E4ABBDD247602E713981C83&source=youtube&upn=3Qgzgd7FAjY&newshard=yes&ratebypass=yes&ipbits=8&itag=18&yms=Ag6pZvjqA20&fexp=909714%2C901802%2C919358%2C914066%2C916625%2C902534%2C920704%2C912806%2C922403%2C922405%2C929901%2C913605%2C925710%2C929104%2C920201%2C913302%2C919009%2C911116%2C926403%2C910221%2C901451&ip=31.205.57.12&sparams=cp%2Cid%2Cip%2Cipbits%2Citag%2Cratebypass%2Csource%2Cupn%2Cexpire&cp=U0hUTlBTVF9LS0NONF9ITVhCOlhkN3FFcm5zWDY5&dnc=1&id=201015c58a31c984&mv=m&mt=1359064215&ms=au
2013-01-24 21:50:31.643 HatFilms[30372:c07] [MPAVController] Autoplay: Enabling autoplay
2013-01-24 21:50:31.646 HatFilms[30372:c07] [MPCloudAssetDownloadController] Prioritization requested for media item ID: 0
2013-01-24 21:50:32.333 HatFilms[30372:c07] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 0, on player: 1)
2013-01-24 21:50:32.349 HatFilms[30372:c07] [MPAVController] Autoplay: _streamLikelyToKeepUp: 0 -> 1
2013-01-24 21:50:32.350 HatFilms[30372:c07] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 0, on player: 1)
2013-01-24 21:50:32.768 HatFilms[30372:c07] [MPAVController] Autoplay: Enabling autoplay
2013-01-24 21:50:32.786 HatFilms[30372:c07] [MPAVController] Autoplay: Enabling autoplay

Thanks for looking

来源:https://stackoverflow.com/questions/14510191/open-in-safari-from-uiwebview-using-youtube-com

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