Search code examples
iosuiwebviewyoutube-apinsnotificationcenteryoutube-javascript-api

Programmatically force a UIWebView to go to full screen mode


When playing a media file in a UIWebview (youtube for instance)

there is a button to "fullscreen" the html5 media player - How can I

  • Know when the player went to full screen
  • Force the HTML5 player to go to full screen

enter image description here


Solution

  • To force videos to always play in fullscreen, when configuring your UIWebView, use this line of code:

    webView.allowsInlineMediaPlayback = NO;
    

    It looks like there are a few ways to detect entering and leaving fullscreen, though it seems like these are not recommended approaches. See the linked StackOverflow question.

    Depending on what you're trying to do, it almost seems like you're better off detecting player state change using the onStateChange callback. In a project I'm working on, the code looks like this:

            function onStateChange(event) {
            window.location.href = 'ytplayer://onStateChange?data=' + event.data;
        }
    

    Then I sniff for this URL in the controller or view wrapper class:

    - (BOOL)webView:(UIWebView *)webView
    shouldStartLoadWithRequest:(NSURLRequest *)request
                navigationType:(UIWebViewNavigationType)navigationType {
    if([request.URL.scheme isEqualToString:@"ytplayer"]) {
        NSString *action = request.URL.host;