【问题标题】:webView:shouldStartLoadWithRequest:navigationType: not getting called on webkit with youtube video link iphonewebView:shouldStartLoadWithRequest:navigationType: 在 webkit 上没有被调用 youtube 视频链接 iphone
【发布时间】:2011-04-16 04:56:51
【问题描述】:

我正在使用 webkit 在 iOS4 上显示 YouTube 视频。单击视频后,它应该会自动启动播放器并显示视频,但是发生的情况是视频在播放器中呈现,但在 web 视图下。我想要做的是让 webview 的 alpha 0 所以我使用委托

      webView:shouldStartLoadWithRequest:navigationType:

但是当点击视频启动它时,这个代表没有被调用。当第一个 webview 本身从 url 启动时,它会被调用。

编辑

- (void)loadView {
[super loadView];

activityIndicatorView = [[ActivityIndicator alloc] init];

web = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
web.delegate = self;
NSString *videoUrl = @"http://www.youtube.com/watch?v=";
NSString *fullVdoUrl = [videoUrl stringByAppendingFormat:_videoUrl];

NSString *urlAddress = [NSString stringWithFormat:fullVdoUrl];   
NSURL *url = [NSURL URLWithString:urlAddress];           //Create a URL object.
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];            //URL Requst Object
[web loadRequest:requestObj];              //Load the request

[self.view addSubview:web];
[web release];

}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
[activityIndicatorView.view removeFromSuperview];
web.alpha = 1.0;
}

- (void)webViewDidStartLoad:(UIWebView *)webView {     
[self.view addSubview:activityIndicatorView.view];
}

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
web.alpha = 0.0;
return YES;
}

【问题讨论】:

  • 你找到答案了吗?我也面临同样的问题。
  • 当我尝试实现这一点时,我发现了一些错误,它给出了以下日志。从日志中可以明显看出,webview 在内部使用了 AVFoundation 和 Movie Player,也许我们可以以某种方式使用该信息? - 2011-10-19 15:19:22.567 PlayYoutube[2720:707] _serverConnectionDiedNotification。 Info -- notification=Error Domain=AVFoundationErrorDomain Code=-11819 "Cannot Complete Action" UserInfo=0x19cf20 {NSLocalizedRecoverySuggestion=稍后再试。, NSLocalizedDescription=Cannot Complete Action}, AVPlayer = , currentTime = 0.00
  • 也许这有帮助:groups.google.com/group/phonegap/browse_thread/thread/…, devforums.apple.com/message/26261#26261。我认为问题在于当视频加载 JavaScript 时,委托没有被调用。您可能需要扩展 JavaScript 代码,但这并不总是可行的,尤其是在使用外部站点时。

标签: iphone uiwebview


【解决方案1】:
- (void)loadView {
[super loadView];

activityIndicatorView = [[ActivityIndicator alloc] init];

web = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
web.delegate = self;
NSString *videoUrl = @"http://www.youtube.com/watch?v=";
NSString *fullVdoUrl = [videoUrl stringByAppendingFormat:_videoUrl];

NSString *urlAddress = [NSString stringWithFormat:fullVdoUrl];   
NSURL *url = [NSURL URLWithString:urlAddress];           //Create a URL object.
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];            //URL Requst Object
[web loadRequest:requestObj];              //Load the request

[self.view addSubview:web];
[web release];

}

这里:

[self.view addSubview:web];

self.view 为 nil,将视图添加到 nil 不会保留您的 webview。

[网络发布];

您正在发布保留计数为 0 的 webview。

试试

self.view = web;
[web release];

这对你有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-18
    • 2012-06-14
    • 1970-01-01
    • 2017-04-06
    • 2015-02-04
    • 2012-04-12
    • 2011-11-26
    相关资源
    最近更新 更多