jonypz
jonypz

Reputation: 1555

playing youtube video inside uiwebview. How to handle the "done" button?

I have a uiwebview that plays a youtube video. How can I handle the done button action? Right now, when I tap the done button it changes back to my app main menu (not the menu that was supposed to dismiss to) and it just freezes. Can anyone help me please?

Ps: the menu where the uiwebview is located, was previously presented modally.

Upvotes: 10

Views: 6215

Answers (4)

netliner
netliner

Reputation: 159

Simply remove UIWebView when it enters full screen; add back UIWebView when it exit full screen. Sample code below assuming a UIViewController with subview of UIWebView, and your UIWebView should have youtube iframe.

- (void)viewDidAppear:(BOOL)animated {

[super viewDidAppear:animated];
// Add observer for "Done" button click
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(playerWillExitFullscreen:)
                                             name:@"UIMoviePlayerControllerWillExitFullscreenNotification"
                                           object:nil];

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

- (void)viewDidDisappear:(BOOL)animated {

// Remove observers for "Done" button click
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"UIMoviePlayerControllerWillExitFullscreenNotification" object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil];

}

- (void)playerWillExitFullscreen:(NSNotification *)notification {

// Before exit full screen, add back UIWebView that have been removed earlier
[self.view addSubview:self.webView];
}

- (void)playerDidEnterFullscreen:(NSNotification *)notification {

if (self.presentingViewController) { // UIWebView is presenting the build-in movie player controller
    [self.webView removeFromSuperview]; // Built-in movie player controller is already entering full screen mode
}
}

Upvotes: 0

Liqun
Liqun

Reputation: 4171

@Gdx Wu @lambmj

Thanks for your methods, they work fine. But there is some small problem that after clicking the done button & jumping directly to the presenting view controller, we need to dismiss the presented modal view controller and present it again, which would bring some dither(like flash) between these view controller switches.

Based on this, I highly recommend @IsaacCisneros 's method which would switch seamlessly.

Upvotes: 0

Gdx Wu
Gdx Wu

Reputation: 21

This thread is very useful and help me to find the problem!

The answer of lambmj works fine, but I found a better way. In presenting view controller:

  - (void)viewDidAppear:(BOOL)animated {
      [super viewDidAppear:animated];
      if (self.presentedViewController) {
          UIViewController *vc = self.presentedViewController;
          [vc dismissModalViewControllerAnimated:NO];
          [self presentModalViewController:vc
                                  animated:NO];
      }
  }

Hope this helps!

Upvotes: 2

lambmj
lambmj

Reputation: 1843

The YouTube plug-in player is itself a modal view controller. It is returning to its presentingViewController when the done button is pressed. Its presentingViewController is not your modal view controller but is instead the viewController that called [presentModalViewController:animated:] to present your modal view controller. Since the original modal view controller is still active, the app behaves badly.

To fix the problem,

1) Track whether the modal view controller has been presented but not dismissed.

2) In the viewDidAppear method of the presenting view controller, if the modal view controller was presented and not dismissed, dismiss and present it again.

For example, in controller that is presenting the modal web view controller:

 - (void) presentModalWebViewController:(BOOL) animated {
      // Create webViewController here.
      [self presentModalViewController:webViewController animated:animated];
      self.modalWebViewPresented = YES;
  }

  - (void) dismissModalWebViewController:(BOOL) animated {
      self.modalWebViewPresented = NO;
      [self dismissModalViewControllerAnimated:animated];
  }

  - (void) viewDidAppear:(BOOL)animated {
      [super viewDidAppear:animated];
      if (self.modalWebViewPresented) {
           // Note: iOS thinks the previous modal view controller is displayed.
           // It must be dismissed first before a new one can be displayed.  
           // No animation is needed as the YouTube plugin already provides some.
           [self dismissModalWebViewController:NO];
           [self presentModalWebViewController:NO];
      }
  }

Upvotes: 8

Related Questions