jtrim
jtrim

Reputation: 3485

shouldAutorotateToInterfaceOrientation doesn't work on first launch after fresh app install

When the app I'm working on is installed either via Ad-Hoc thru iTunes or built directly to the device, upon launching for the first time, the only view controller in my app that responds to orientation changes doesn't receive calls to shouldAutorotateToInterfaceOrientation: with a landscape argument passed in; debugging shows that it's only being called for portrait. Every subsequent launch behaves as I would expect - that is, there are calls to shouldAutorotateToInterfaceOrientation: made both with landscape and portrait arguments. This exact behavior can be seen in the iPhone simulator, on the iPhone and on the iPod touch.

So my question is: why would orientation notifications be different for the first launch of an app than they would be for every subsequent launch? Am I mistaken in believing that I have no control over orientation changes beyond responding to shouldAutorotateToInterfaceOrientation:?

Inside the ViewController in question:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
 return YES;
}

and inside of viewDidLoad and viewDidUnload I've got (respectively):

 [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
and

 [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];

Update July 6, 2010:

Still no luck solving the problem. I dropped the issue for a little while and came back to it and am still seeing the problem under completely different circumstances. Anyone?

Update July 13, 2010:

From Apple's View Controller Programming Guide:

"...the window object does much of the work associated with changing the current orientation. [...] Specifically, it works with the view controller whose root view was most recently added to, or presented in, the window. In other words, the window object works only with the frontmost view controller whose view was displayed..."

I'm adding the root view controller to the window differently on the first launch compared to every subsequent launch, so I thought maybe it had something to do with this. I have yet to trace anything back to here though...just a thought.

This thing has had around 175 views at the time of this update...no one has even the most far out obscure suggestion? Come on, throw something out there. I'm willing to entertain any guesses or suggestions at this point. I don't care if it's stupidly obscure or potentially irrelevant.

Upvotes: 0

Views: 3962

Answers (3)

Ash Furrow
Ash Furrow

Reputation: 12421

I had a similar problem - the UIDevice.h header lists endGeneratingDeviceOrientationNotifications and beginGeneratingDeviceOrientationNotifications as "nestable." It turns out I had unbalanced calls to these methods.

I solved this quickly with the following change to beginGeneratingDeviceOrientationNotifications:

if (![[UIDevice currentDevice] isGeneratingDeviceOrientationNotifications])
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

Upvotes: 0

EarlGrey
EarlGrey

Reputation: 2543

also make sure you set:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return YES;}

in ALL ViewControllers.m in your app, not just one you're working on (if you have more than one). I was struggling trying to get it going for the FirstViewController, but it wouldn't work no matter what. As soon as I added the above code to all four view controllers, it started to work just fine (in all four)

Upvotes: 0

jtrim
jtrim

Reputation: 3485

Never did solve this problem - I left the company where I encountered it before I had a chance to. However, I had a pretty good lead on it by the time I left. I contacted Apple DTS about the issue and they noted that for autorotation to work properly, all ViewControllers in the view stack related to autorotation must call the super methods in the method implementations (i.e. calling [super viewDidLoad] from within the ViewController's viewDidLoad). I don't remember which methods they cited exactly, but it's probably worth a shot to ensure you're properly calling super where appropriate.

[EDIT] If someone can confirm this, I'll mark it as the accepted answer. Thanks!

Upvotes: 2

Related Questions