Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 984
jonypz Avatar asked Dec 07 '11 16:12

jonypz


2 Answers

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];
      }
  }
like image 109
lambmj Avatar answered Nov 07 '22 08:11

lambmj


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!

like image 43
Gdx Wu Avatar answered Nov 07 '22 07:11

Gdx Wu