Search code examples
objective-cios7uiviewcontrolleruiwebviewyoutube-api

UIWebView youtube video player DONE button dismisses View Controller to main app rootViewController


The Problem..

My app is taking me back to my main screen of the app (rootViewController) everytime I press the done button after a youtube (embedded in an iframe) video gets launched from a UIWebView which is not what I want it to do. I just want to get back to the View Controller from where this youtube video launched and display all the content within that view.

I tried using notifications to catch the state of the UIMoviePlayerController to present the previous View Controller where the youtube vide launched. But, I'm also using a navigation controller, plus this View Controller depends on a previous controller selection which uses some data to display the content within the View Controller.

I REALLY NEED SOME HELP HERE, I DON'T KNOW WHAT'S THE PROBLEM..!!

Suggestions? Do I need to set my own MPMoviePlayerController? if so, do I have to implement some delegates to be able to controller its states and what its doing?

This is how I set my UIWebView to one of the rows in my 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 method...

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

I really hope someone has come across with this problem and be able to share the solution with me.. Thanks! :)


Solution

  • The whole problem was that someone the the viewDidAppear or actually the NavigationViewController was getting call and because it's set to take you back to the main or actually the rootViewController of the app. It's was clearing up all the previous ViewControllers and taking me back there.

    I debugged with a break point and found the problem.. So, the solution for me was setting a variable in the userDefaults just before the video is about to finish with this notification UIMoviePlayerControllerWillExitFullscreenNotification and calling the function to set this variable, and afterwards skipping the code with an if condition in my viewDidAppear which all the re-settings of ViewController whats happening in my case..

    Well, I hope it helps someone else with the same or similar problem..