Question

How to find the Device Orientation in the following application states:

  1. First time launching application
  2. Application entering in to foreground
Was it helpful?

Solution

You Can detect orientation at first time launching application like this :

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
 {

 [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
 [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(deviceOrientationDidChange:) name: UIDeviceOrientationDidChangeNotification object: nil];
 }

To detect the Orientations :

-(void)deviceOrientationDidChange:(NSNotification *)notification
{
         UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

         //Ignoring specific orientations
         if (orientation == UIDeviceOrientationFaceUp || orientation == UIDeviceOrientationFaceDown || orientation == UIDeviceOrientationUnknown || currentOrientation == orientation)
         {
         //To check orientation ;
         }

         if ([[NSUserDefaults standardUserDefaults] boolForKey:@"deveiceorientation"])
         {
           // your orientation
         }
         else
        {
           [[NSUserDefaults standardUserDefaults] setObject:deviceOrientaion forKey:@"deveiceorientation"];
           [[NSUserDefaults standardUserDefaults] synchronize];
           // save your orientation
          }

}

On application enter foreground you can check the same using

-(void)viewDidAppear:(BOOL)animated{

}

OTHER TIPS

If you turn on device orientation notifications, as you tried, you can get notifications about changes in the device orientation, but I'm not sure that you can find out the current orientation at launch reliably that way.

If you happen to be inside of view controller code when you need the device orientation, then it's almost always best to just ask the view controller right when you need the orientation, like:

self.interfaceOrientation

when self is an instance of UIViewController. Frequently the important thing is to know whether you're in portrait or landscape mode, in which case you'll have something like:

const UIInterfaceOrientation currentInterfaceOrientation = self.interfaceOrientation;
if (UIInterfaceOrientationIsLandscape(currentInterfaceOrientation)) {
    // Set up for landscape orientation.
} else {
    // Set up for portrait orientation (including upside-down orientation).
}

Edit: You need to get that initial check for the device orientation outside of the application:didFinishLaunching:withOptions: method. You can use dispatch_after to delay the execution of your initial check of the device orientation. I'm not sure I like the pattern you're using here, but I think this will work for you, at the end of your application:didFinishLaunchingWithOptions: method:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
__block UIDeviceOrientation initialDeviceOrientation;
double delayInSeconds = 0.1;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    initialDeviceOrientation = [[UIDevice currentDevice] orientation];
    NSLog(@"initialDeviceOrientation = %u", initialDeviceOrientation);
});
// Etc.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top