How to determine iOS device orientation when your app starts up.

I’ve been working on several iPad apps lately that require a loading screen to appear right after launch. All the apps are designed to work in any rotation so getting things to startup in the correct orientation has been a challenge because of some iOS “bugs” with device orientation at startup.

After several attempts of trial and error I believe the following is a foolproof solution for setting the correct orientation of the startup view for an iPad app (assuming the default view was design in portrait orientation). I thought I’d share it here for anyone else who is having a similar problem.

First, in application:didFinishLaunchingWithOptions: add the following:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {	
	
	[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

   // The rest of your code...
   
}

then, on the view you want to rotate, check both interface and device orientation to properly position the view in all four orientations (plus laying flat on a table).

-(void)updateOrientation {

	UIInterfaceOrientation iOrientation = [UIApplication sharedApplication].statusBarOrientation;
	UIDeviceOrientation dOrientation = [UIDevice currentDevice].orientation;

	bool landscape;
	
	if (dOrientation == UIDeviceOrientationUnknown || dOrientation == UIDeviceOrientationFaceUp || dOrientation == UIDeviceOrientationFaceDown) {
		// If the device is laying down, use the UIInterfaceOrientation based on the status bar.
		landscape = UIInterfaceOrientationIsLandscape(iOrientation);
	} else {
		// If the device is not laying down, use UIDeviceOrientation.
		landscape = UIDeviceOrientationIsLandscape(dOrientation);
		
		// There's a bug in iOS!!!! http://openradar.appspot.com/7216046
		// So values needs to be reversed for landscape!
		if (dOrientation == UIDeviceOrientationLandscapeLeft) iOrientation = UIInterfaceOrientationLandscapeRight;
		else if (dOrientation == UIDeviceOrientationLandscapeRight) iOrientation = UIInterfaceOrientationLandscapeLeft;

		else if (dOrientation == UIDeviceOrientationPortrait) iOrientation = UIInterfaceOrientationPortrait;
		else if (dOrientation == UIDeviceOrientationPortraitUpsideDown) iOrientation = UIInterfaceOrientationPortraitUpsideDown;
	}
	
	if (landscape) {
		// Do stuff for landscape mode.
	} else {
		// Do stuff for portrait mode.
	}
	
	// Now manually rotate the view if needed.
	switch (iOrientation) 
	{
		case UIInterfaceOrientationPortraitUpsideDown:
			[self rotate:180.0f];
			break;
		case UIInterfaceOrientationLandscapeRight:
			[self rotate:90.0f];
			break;
		case UIInterfaceOrientationLandscapeLeft:
			[self rotate:-90.0f];
			break;
		case UIInterfaceOrientationPortrait:
			break; //do nothing because it's fine
		default:
			break;
	}
	
	// Set the status bar to the right spot just in case
	[[UIApplication sharedApplication] setStatusBarOrientation:iOrientation];
	
}

Enjoy. If you found it useful or have an improvement, let me know.