2011-05-07 73 views
1

我有加載YouTube視頻的網絡視圖。我正在使用以下方法在Web視圖加載時自動啓動YouTube視頻。檢查YouTube視頻(MPMoviePlayer)是否已結束

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]; 
} 

回答

3

蘋果不會推[記錄]通知這一點,所以你必須得有點棘手。

我這樣做的方式是檢查應用程序的keyWindow。我從here得到了這個想法。

在.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 ... 
    } 
} 
+0

這非常聰明。真棒!這正是我需要的。非常感謝你! – simonbs 2011-05-14 08:24:17