1

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

  1. First time launching application
  2. Application entering in to foreground
Sergiu Dumitriu
  • 11,455
  • 3
  • 39
  • 62
nagarajan
  • 474
  • 3
  • 21
  • what have u searched so far? – NightFury Oct 21 '13 at 05:01
  • i used [[UIDevice currentDevice] orientation], its returning always 0. and i used [[UIApplication sharedApplication] statusBarOrientation] its returning 1. – nagarajan Oct 21 '13 at 05:03
  • regardless of any orientation? – NightFury Oct 21 '13 at 05:12
  • no any specific orientation. see when ever i launch application first time i want to check device orientation and same if i click home button application will enter into background from when i come to foreground again i want to get current device orientation. so where i have to invoke device orientation functions. – nagarajan Oct 21 '13 at 05:23
  • Use `- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions` and `- (void)applicationWillEnterForeground:(UIApplication *)application` present in your app delegate. – NightFury Oct 21 '13 at 05:37
  • [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; NSLog(@"orientation %d", [[UIDevice currentDevice] orientation]); i added this code inside of - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions but still its giving value 0. – nagarajan Oct 21 '13 at 05:42
  • See this: http://stackoverflow.com/a/5533222/437146 – NightFury Oct 21 '13 at 06:10

2 Answers2

0

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.
Aaron Golden
  • 7,092
  • 1
  • 25
  • 31
  • Aaron Golden, my concept is i have on common class Device Manager there i want to store device orientation, while app first time launch or coming from background to foreground. so that i am going to check that orientation value according that value my viewDidLoad function will execute. so suggest me to how to proceed... – nagarajan Oct 21 '13 at 05:55
  • I can't check right now, but I wonder if the current device orientation doesn't update while you're still inside `applicationDidFinishLaunching:withOptions:` You could try wrapping the first check of the device orientation in `dispatch_after` with a small delay, in case the current device instance needs a few turns of the event loop to update its orientation property. – Aaron Golden Oct 21 '13 at 06:05
  • self.interfaceOrientation is always returning 1 (UIDeviceOrientationPortrait). – nagarajan Oct 21 '13 at 06:06
  • Aaron Golden i think u got what i am looking for right. when ever possible kindly give solution for my problem... – nagarajan Oct 21 '13 at 06:08
  • Aaron Golden as per your code the value of initialDeviceOrientation is 22348. – nagarajan Oct 21 '13 at 06:36
  • @user2882259 That sounds like a junk value. Are you trying to log the value right after the `dispatch_after` block (where I put "Etc.")? That will not work, because the `dispatch_after` will not have executed by the time you log the value. You should put the log *inside* the `dispatch_after` block, immediately following the line `initialDeviceOrientation = [[UIDevice currentDevice] orientation];`. I edited my answer once more to demonstrate. – Aaron Golden Oct 21 '13 at 06:47
  • Aaron Golden as per ur way i tried to put log after the initialDeviceOrientation = [[UIDevice currentDevice] orientation]; inside the block, but still the value is 0 always. even my simulator in landscape while launch the application. – nagarajan Oct 21 '13 at 07:15
0

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{

}
Arun_
  • 1,806
  • 2
  • 20
  • 40
  • Warning: there is a bug in ios framework: if device was rotated during app background, the orientation returned in the viewDidAppear will be previous one – cyrilchampier Jun 04 '14 at 07:49