It seems that when my app loads, it does not know its current orientation:
UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation == UIDeviceOrientationPortrait) {
NSLog(@"portrait");// only works after a rotation, not on loading app
}
Once I rotate the device, I get a correct orientation, but when I load the app, without changing the orientation, it seems that using [[UIDevice currentDevice] orientation]
doesn't know the current orientation.
Is there another way to check this when I first load my app?
I think this will work:
According to the UIDevice reference:
Quote:
"The value of this property always returns 0 unless orientation notifications have been enabled by calling beginGeneratingDeviceOrientationNotifications"
I had initially assumed that this property contained the current orientation at all times, but not so, apparently. I guess that turning on notifications is being handled for us behind the scenes in other situations where the orientation property is typically accessed, so it wasn't obvious that this needs to be done manually inside the app delegate
for those who looking answer for Swift 3 or 4. just add that code inside of viewDidLoad() block.
the problem is that
[UIDevice currentDevice]orientation]
sometimes reports the the device's orientation incorrectly.instead use
[[UIApplication sharedApplication]statusBarOrientation]
which is aUIInterfaceOrientation
so to check it you'll need to use theUIInterfaceOrientationIsLandscape(orientation)
hope this helps.
I still use this working code snippet for iphone 4:
}
In order to obtain the orientation from the status bar it is also important to have the all the orientations enabled at the plist file.
Swift 3 based on @Marjin 's code.