2
votes

I am developing an iPhone application that needs to play a YouTube video.

I have a ViewController in Portrait Orientation only with a UIWebView that loads a YouTube Video.

What I am trying to do is that the video can be played in landscape orientation.

This sounds pretty simple but I can't figure out how to do that. Could someone please help me out?

Thanks!

3

3 Answers

1
votes

This goes in the AppDelegate. Will enable horizontal mode for video players only

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {

    if let presentedViewController = window?.rootViewController?.presentedViewController {

        let className = String(describing: type(of: presentedViewController))
        if ["MPInlineVideoFullscreenViewController", "MPMoviePlayerViewController", "AVFullScreenViewController"].contains(className)
        {
            return UIInterfaceOrientationMask.allButUpsideDown
        }

    }
    return UIInterfaceOrientationMask.portrait
}
0
votes

There is currently no way to start playing a YouTube video programmatically. That said, you can make a UIWebview in the ViewController's willRotateToInterfaceOrientation:duration: checking for the horizontal orientation and display the webview with the code that shows the video's thumbnail. But the user will still have to activate the view. To remove the video you do it in the same method, but for the portrait orientation and remove the webview.

0
votes

I found the answer.

1) I add this code to my viewController.m

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

2) In interface builder I build my interface in Landscape, with every single element rotated by 90°.

When I build and Run, the view seems to be in portrait mode when it is in fact landscape. Therefore, the YouTube Video embedded in the UIWebView plays in Landscape.