Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 2: screen rotation only on full screen video

This is a popular question but I couldn't find any solution that worked on Swift 2.

The app is portrait only. But while watching full screen videos, such as YouTube, the users should be able to rotate to landscape.

On Objective C, this was the easiest solution and I used for a long time:

AppDelegate file:

static NSString * const VIDEO_CONTROLLER_CLASS_NAME_IOS7 = @"MPInlineVideoFullscreenViewController";
static NSString * const VIDEO_CONTROLLER_CLASS_NAME_IOS8 = @"AVFullScreenViewController";

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{

    if ([[window.rootViewController presentedViewController] isKindOfClass:NSClassFromString(VIDEO_CONTROLLER_CLASS_NAME_IOS7)] ||
[[window.rootViewController presentedViewController] isKindOfClass:NSClassFromString(VIDEO_CONTROLLER_CLASS_NAME_IOS8)]) {

        return UIInterfaceOrientationMaskAllButUpsideDown;

    } else {

    return UIInterfaceOrientationMaskPortrait;

    }

}

This allows all orientations while the video is on full screen. Otherwise, Portrait only.

But I'm having a hard time to make this work on Swift. Is it possible to make the screen rotate when full screen videos are player on Swift?

like image 347
tomDev Avatar asked Feb 12 '16 02:02

tomDev


2 Answers

What about something like this?

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

        var classesToCheckFor = [AnyClass]()

        if let ios7Class = NSClassFromString("MPInlineVideoFullscreenViewController") {
            classesToCheckFor.append(ios7Class)
        }

        if let ios8Class = NSClassFromString("AVFullScreenViewController") {
            classesToCheckFor.append(ios8Class)
        }

        for classToCheckFor in classesToCheckFor {
            if (self.window?.rootViewController?.presentedViewController?.isKindOfClass(classToCheckFor) != nil) {
                return .AllButUpsideDown
            }
        }

        return .Portrait
    }

NSClassFromString can potentially return nil, but isKindOfClass requires a non-optional AnyClass. I'm checking to see if each class can be loaded on the platform, adding the classes that are loaded to an array, and then iterating through the array of classes, checking to see if the presentedViewController is of either class. If it is, we return .AllButUpsideDown. If neither class can be loaded, or the presentedViewController's is not of either class, then we return .Portrait.

like image 59
JAL Avatar answered Sep 27 '22 17:09

JAL


Here the solution for iOS 10:

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

}

like image 28
Nati Lara-Diaz Avatar answered Sep 27 '22 17:09

Nati Lara-Diaz