Search code examples
iphonedelegatesrotationorientationuidevice

How to get correct interface orientation in appDidBecomeActive:(UIApplication *)app?


I need to get interface orientation in appDidBecomeActive:(UIApplication *)application

[application statusBarOrientation];

but if the app starts from closed(ie not resumed from background), this always returns portrait, it works when resumed from background.

Also, I tried to use UIDevice orientation along with status bar orientation, but UIDevice orientation may not be the interface orientation.

So is there any way to get interface orientation in app delegate, appDidBecomeActive?

Thanks!


Solution

  • What you need to do is handle this in your splash view controller. You can use a combination interfaceOrientation, shouldAutorotateToInterfaceOrientation, didAutorotateToInterfaceOrientation, etc.

    Essentially, create a view controller that you will have as your root view controller. In there, determine the orientation changes in shouldAutorotateToInterfaceOrientation (it will always be portrait or landscape in viewDidLoad depending on your xib, so don't do it there). Do your image display with an NSTimer or whatever. After the timer, show your regular app screens.

    You can't display the image until you have a view controller anyway, so you must wait until a view controller gives you the interfaceOrientation changes. You should focus on that first view controller, not the app delegate.

    AppDelegate.h

    #import <UIKit/UIKit.h>
    
    @class SplashViewController;
    @interface AppDelegate : UIResponder <UIApplicationDelegate>
    
    @property (retain, nonatomic) IBOutlet UIWindow *window;
    @property (retain, nonatomic) SplashViewController *splashController;
    
    -(void)showSplash;
    @end
    

    AppDelegate.m

    #import "AppDelegate.h"
    #import "SplashViewController.h"
    
    @implementation AppDelegate
    @synthesize window = _window, splashController = _splashController;
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        [self showSplash];
        [self.window makeKeyAndVisible];
        [self performSelector:@selector(registerBackground) withObject:nil afterDelay:5.0];
        return YES;
    }
    
    -(void)showSplash
    {
        SplashViewController *splash = [[SplashViewController alloc] initWithNibName:@"SplashViewController" bundle:nil];
        [self.window addSubview:splash.view];
        self.splashController = splash;
        [splash release];
        //have to add a delay, otherwise it will be called on initial launch.
        [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(removeSplash:) userInfo:nil repeats:NO];
    
    }
    
    -(void)registerBackground
    {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(returnFromBackground:)
                                                     name:UIApplicationDidBecomeActiveNotification
                                                   object:nil];
    }
    
    -(void)returnFromBackground:(NSNotification *)notification
    {
        [self showSplash];
    }
    
    -(void)removeSplash:(NSTimer *)timer
    {
        [self.splashController.view removeFromSuperview];
        self.splashController = nil;
    }
    
    
    - (void)dealloc
    {
        [_window release];
        [_splashController release];
        [super dealloc];
    }