10

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.

2
  • Have found a solution to your problem, can you share it with us ?
    – Illep
    Commented Jan 17, 2012 at 1:32
  • not yet... I haven't found it
    – jonypz
    Commented Jan 17, 2012 at 14:35

4 Answers 4

8

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];
      }
  }
4
  • 1
    This methode works ONLY if you dissmiss the webviewController in VIEWDIDAPPEAR and in my case, it crashes if I do it in VIEWWILLAPPEAR. Be carefull with that. thanks for the tip
    – Vassily
    Commented Mar 6, 2012 at 10:17
  • That works great, thank you. Does anybody know why we have to do this in the first place? Is it a bug in UIKit, or is there something I'm misunderstanding about modal view controllers?
    – Defragged
    Commented Jul 26, 2012 at 8:52
  • 2
    In this case the UIWebView is presented modally and the You Tube plug-in presented on top of it is presented modally as well. So we have a modal view controller presenting a modal view controller. UIKit expects that there only ever be a single modal view controller presented and when the top one is dismissed the ones underneath it are dismissed as well. When that happens the base view controller gets called with viewDidAppear: and gives us a chance to represent our modal view controller again. Is it a bug? Maybe but probably it is a design decision Apple made that we have to work around.
    – lambmj
    Commented Aug 1, 2012 at 21:50
  • On iOS 6 there is no such behavior. My app shows that behavior on iOS 5 and works normally on iOS 6.
    – Petr
    Commented Jan 22, 2013 at 12:54
2

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!

0

@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.

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.