UIWebView youtube视频播放器DONE按钮将View Controller解除为主应用程序rootViewController

时间:2014-08-12 15:27:56

标签: objective-c ios7 uiviewcontroller uiwebview youtube-api

问题..

每当我在youtube之后按完成按钮时,我的应用会将我带回应用的主屏幕 rootViewController )(嵌入在iframe中的视频是从 UIWebView 启动的,这不是我想要的。我只想回到视频控制器,从这个YouTube视频启动的地方,并显示该视图中的所有内容。

我尝试使用通知来捕获 UIMoviePlayerController 的状态,以显示youtube vide启动的上一个View Controller。但是,我也使用导航控制器,此视图控制器依赖于先前的控制器选择,该选择使用一些数据来显示视图控制器中的内容。

我真的需要一些帮助,我不知道该怎么回事...... !!

建议吗 我是否需要设置自己的 MPMoviePlayerController ?如果是这样,我是否必须实现一些代理才能控制其状态及其作用?

这就是我将UIWebView设置为TableView

中的一行的方法
self.videoView = [[UIWebView alloc] initWithFrame:CGRectMake(kElementX, kElementY, kElementWidth, 120)];
self.videoView.backgroundColor = [UIColor clearColor];
self.videoView.opaque = NO;
self.videoView.scalesPageToFit = YES;
//self.videoView.delegate = self;
[self.videoView setTranslatesAutoresizingMaskIntoConstraints:YES];
self.videoView.allowsInlineMediaPlayback = YES;

[cell.contentView addSubview:self.videoView];

NSString *youtubeURL = [NSString stringWithFormat:@"%@", self.url];

NSLog(@"youtube link -> %@", youtubeURL);

NSString *videoHTML = [NSString stringWithFormat:@"\
                       <html>\
                       <head>\
                       <style type=\"text/css\">\
                       iframe {position:absolute; top:0%%; margin-top:0px;}\
                       body {background-color:#000; margin:0;}\
                       </style>\
                       </head>\
                       <body>\
                       <div id=\"player\">\
                       <iframe class=\"youtube-player\" type=\"text/html\" width=\"100%%\" height=\"420px\" src=\"%@\" frameborder=\"0\" allowfullscreen></iframe>\
                       </div>\
                       <script>\
                       var tag = document.createElement('script');\
                       tag.src = \"https://www.youtube.com/iframe_api\";\
                       var firstScriptTag = document.getElementsByTagName('script')[0];\
                       firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);\
                       var player;\
                       var myVideoId = document.getElementById('%@')\
                       function onYouTubeIframeAPIReady() {\
                       player = new YT.Player('player', {\
                            height: '100%%',\
                            width: '100%%',\
                            videoId: myVideoId,\
                            playerVars:{\
                                'autoplay':0,\
                                'controls':1,\
                                'enablejsapi':1,\
                                'playsinline':1,\
                                'showinfo':0\
                       events: {\
                            'onReady': onPlayerReady,\
                       }\
                       });\
                       }\
                       </script>\
                       </body>\
                       </html>", youtubeURL, self.url];

[self.videoView loadHTMLString:videoHTML baseURL:nil];       

viewDidLoad 方法......

-(void)viewDidLoad{
    // Using notifications to get the state of the UIMoviePlayerController
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieIsPlaying:) name:@"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieStopedPlaying:) name:@"UIMoviePlayerControllerDidExitFullscreenNotification" object:nil];
}

// Trying to stop the dismiss of the View Controller
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    if (self.videoLaunched) {
        [super viewWillDisappear:NO];
        [self dismissViewControllerAnimated:NO completion:nil];
    }
}

// apparently this function besides the notifications functions is the only one which gets call after the done button is pressed (here I tried to attempt loading back the View Controller) not much luck 
-(void)viewWillDisappear:(BOOL)animated{
    if(self.videoLaunched)
        [super viewWillDisappear:NO];
}


// Starts playing the video
- (void)movieIsPlaying:(NSNotification *)notification
{
    self.videoLaunched = YES;
}

// The video stop and exit player
- (void)movieStopedPlaying:(NSNotification *)notification
{
    self.videoLaunched = NO;
}

我真的希望有人遇到过这个问题,并能够与我分享解决方案..谢谢! :)

1 个答案:

答案 0 :(得分:0)

整个问题是viewDidAppear或实际NavigationViewController的某个人正在接听电话,因为它会将您带回主电话或实际上rootViewController该应用程序。它正在清理所有先前的ViewControllers并将我带回那里。

我使用断点进行调试并发现了问题..因此,我的解决方案是在视频即将完成此通知userDefaults并调用之前在UIMoviePlayerControllerWillExitFullscreenNotification中设置变量设置此变量的函数,然后在viewDidAppear中使用if条件跳过代码,ViewController的所有重新设置都在我的情况下发生..

嗯,我希望它可以帮助其他有相同或类似问题的人。