Launching an app only in portrait orientation

Viewed 596

I'm trying to develop an app on Swift that supports both portrait and landscape orientations but I don't want that the user gets the possibility to launch it in landscape mode. So is there a way to prevent this opening in landscape while keeping the app's device orientation parameters on portrait, landscape left and landscape right, or is there a way to change the device orientation parameters from portrait only after the launch of the app with the UIInterfaceOrientationMask ?

Thanks a lot for your help!

1 Answers

Restrict it in AppDelegate's method -

var supportedOrientation: UIInterfaceOrientationMask = .portrait

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

Override following in all view controllers -

override func viewDidAppear(_ animated: Bool) {
    if let delegate = UIApplication.shared.delegate as? AppDelegate {
        var orientation: UIInterfaceOrientationMask = .all
        delegate.supportedOrientation = orientation
    }
}

Try it and let me know if it works.

Related