3

我正在使用适用于 ios 的 Cordova/phonegap 框架在 xcode 中构建一个应用程序,该框架显示一些 html,其中包含一些嵌入的 youtube 播放器代码。iOS 在点击此 youtube 播放器时似乎会将用户重定向到 youtube 应用程序。在科尔多瓦 1.5.0 中,以下代码有效,但在 1.6.1 中似乎没有。任何想法为什么或需要改变什么才能让它发挥作用?

阻止 youtube 打开的代码和行为自我的链接

- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSURL *url = [request URL];

    // Intercept the external http requests and forward to Safari.app
    // Otherwise forward to the PhoneGap WebView

    NSString* urlString = [url absoluteString];
    if([urlString rangeOfString:@"http://www.youtube.com/embed"].location != NSNotFound) {
        return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
    }
    else if (([[url scheme] isEqualToString:@"http"] || [[url scheme] isEqualToString:@"https"])) {
        [[UIApplication sharedApplication] openURL:url];
        return NO;
    }
    else {
        return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
    }
}
4

2 回答 2

0

看看http://apiblog.youtube.com/2009/02/youtube-apis-iphone-cool-mobile-apps.html 这可能会有所帮助。

它说这样做:

NSString *htmlString = @"<html><head>
<meta name = \"viewport\" content = \"initial-scale = 1.0, user-scalable = no, width = 212\"/></head>
<body style=\"background:#F00;margin-top:0px;margin-left:0px\">
<div><object width=\"212\" height=\"172\">
<param name=\"movie\" value=\"http://www.youtube.com/v/oHg5SJYRHA0&f=gdata_videos&c=ytapi-my-clientID&d=nGF83uyVrg8eD4rfEkk22mDOl3qUImVMV6ramM\"></param>
<param name=\"wmode\" value=\"transparent\"></param>
<embed src=\"http://www.youtube.com/v/oHg5SJYRHA0&f=gdata_videos&c=ytapi-my-clientID&d=nGF83uyVrg8eD4rfEkk22mDOl3qUImVMV6ramM\"
type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"212\" height=\"172\"></embed>
</object></div></body></html>";

[webView loadHTMLString:htmlString baseURL:[NSURL URLWithString:@"http://www.your-url.com"]];
于 2012-04-27T14:00:14.347 回答
0

我会在方法的开头为 url 登录,然后在 if/elseif/else 的每个子句中添加一个 log 语句,这样您就可以看到拦截了哪些 url 以及该方法对每个 URL 做了什么。

也许 youtube 请求的字符串不再匹配硬编码的“http://www.youtube.com/embed”?值得一看。

于 2012-04-27T19:35:58.630 回答