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

后端 未结 4 681
攒了一身酷
攒了一身酷 2021-02-08 10:57

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 t

4条回答
  •  青春惊慌失措
    2021-02-08 11:06

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

提交回复
热议问题