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

后端 未结 4 683
攒了一身酷
攒了一身酷 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:21

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

提交回复
热议问题