1

我有一个带有加载 YouTube 视频的网络视图的视图。我正在使用以下方法在加载 Web 视图时自动启动 YouTube 视频。

Web 视图打开 iPhone 的本机电影播放器​​。有什么方法可以检查视频是否已经结束或用户是否按下了电影播放器​​的“确定”按钮并因此关闭了播放器?

这些是我用于自动启动 Web 视图的方法:

- (UIButton *)findButtonInView:(UIView *)view {
    UIButton *button = nil;

    if([view isMemberOfClass:[UIButton class]]) {
        return (UIButton *)view;
    }

    if(view.subviews && [view.subviews count] > 0) {
        for(UIView *subview in view.subviews) {
            button = [self findButtonInView:subview];
            if(button) return button;
        }
    }

    return button;
}

- (void)webViewDidFinishLoad:(UIWebView *)_webView {
    UIButton *b = [self findButtonInView:_webView];
    [b sendActionsForControlEvents:UIControlEventTouchUpInside];
}
4

1 回答 1

3

Apple 不会为此推送 [记录] 通知,因此您必须有点棘手。

我这样做的方法是检查应用程序的 keyWindow。我从这里得到了这个想法。

在您的 .h 文件中,跟踪您的计时器和所需的 keyWindow:

NSTimer *windowTimer;
UIWindow *keyWindow;

在您的 .m 文件中,您需要以下内容:

- (void)viewDidLoad {
    [super viewDidUnload];
    keyWindow = [[UIApplication sharedApplication] keyWindow];
}

然后编辑您的委托方法并添加一个新方法:

- (void)webViewDidFinishLoad:(UIWebView *)_webView {
    UIButton *b = [self findButtonInView:_webView];
    [b sendActionsForControlEvents:UIControlEventTouchUpInside];

    // start checking the current keyWindow
    windowTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(checkWindowStatus) userInfo:nil repeats:YES];
}

- (void) checkWindowStatus {
    // if the key window is back to our application
    if (keyWindow == [[UIApplication sharedApplication] keyWindow]) {
        [windowTimer invalidate];
        windowTimer = nil;

        ... window has dismissed, do your thing ...
    }
}
于 2011-05-14T04:06:48.513 回答