Swift - Allow Rotation on iPad Only

How could I allow my universal app written in Swift on iOS 8.3 SDK to support only portrait mode on iPhone, but both portrait and landscape mode on iPad?

I know in the past this has been done in AppDelegate. How could I do this in Swift?


You can do it programmatically, or better yet, you can simply edit your project's Info.plist (Which should be more practical, since it's a global device configuration)

Just add "Supported Interface orientations (iPad)" key

enter image description here


You could do it programmatically

override func shouldAutoRotate() -> Bool {
    if UIDevice.currentDevice().userInterfaceIdiom == .Pad {
        return true
    }
    else {
        return false
    }
}

and then

override func supportedInterfaceOrientations() -> Int {
    return UIInterfaceOrientation.Portrait.rawValue
}

or any other rotation orientation that you wish to have by default.

That should detect if the device you're using is an iPad and allow rotation on that device only.

EDIT: Since you only want portrait on iPhone,

 override func supportedInterfaceOrientations() -> Int {
    if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
        return UIInterfaceOrientation.Portrait.rawValue
    }
    else {
        return Int(UIInterfaceOrientationMask.All.rawValue)
    }
}