Supported Orientation not working like I want it to

I have an app that can run in landscape OR portrait mode, but I don’t want it to change while it is running. So when you open the app while the iPad is in landscape mode it will only run in both the landscape modes, and vice versa.

I start the program like this (even before setup(), as this prevents the brief flashing of the parameter window):

if WIDTH >= HEIGHT then
    supportedOrientations(LANDSCAPE_ANY)  
else
    supportedOrientations(PORTRAIT_ANY)  
end

This works fine in Codea, but when I run the exported project in Xcode (with all orientations supported) I can still change from landscape to portrait while it is running. And this will cause it to crash (because the app only loads the images it needs for the mode it started in, to save a lot of loading time).

What am I doing wrong ?

I’m not sure if this is correct, but I don’t think the if statements will run outside of a specified functions. I may be wrong though.

EDIT; this is incorrect, as you can see below.

I usually use supportedOrientations(CurrentOrientation), which locks the orientation to whatever it was previously. Or, if you just want to lock it to LANDSCAPE_ANY or PORTRAIT_ANY based on the current orientation, this works:

if CurrentOrientation == LANDSCAPE_LEFT or CurrentOrientation == LANDSCAPE_RIGHT then
    supportedOrientations(LANDSCAPE_ANY)
else
    supportedOrientations(PORTRAIT_ANY)
end

@Mr_Ninja If statements work outside of functions, the reason the code didn’t work is because the OpenGL context hadn’t been created yet, and so WIDTH and HEIGHT were both 0 (until setup is ran). This is changed in the beta, I think, so WIDTH and HEIGHT work before setup is ran.

@SkyTheCoder, WIDTH and HEIGHT are set before setup, so his way should (and he says does) work.

The problem is that supportedOrientations just doesn’t work in Xcode. This has always been the case I believe, you have to set the orientation using the Xcode options, but that will not allow what he is trying to do.

@eriksw, maybe the easiest way would be to use Obj-C to lock the orientation to the one it is launched in?

I have tried both solutions mentioned above (had not seen JakAttak’s reply yet… ), but I can confirm that JakAttak is right: supportedOrientations has no effect at all. No matter what the settings in Xcode are, these are always dictating what happens. I even tried unchecking all of them in Xcode, still not working.

@JakAttak: unfortunately I don’t speak a word of Obj-C… I’m over 50 and you wouldn’t believe the amount of computer languages I have learned in my life, most of them obsolete now, so please God, not ANOTHER one… especially as I shouldn’t be needing it, and I have no plans to use it for anything…

Perhaps you can explain how to do this in a few lines ? Help would be GREATLY appreciated.

Otherwise I will have to put two separate apps in the AppStore, one for portrait and one for landscape. That would be silly…

And @Simeon - would it be possible to fix Codea so that supportedOrientations DOES work in Xcode ?

@eriksw, try this:

Add the following code to AppDelegate.mm, before the @end line.

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    NSUInteger orientations = UIInterfaceOrientationMaskAllButUpsideDown;

    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
    
    if (deviceOrientation == UIDeviceOrientationPortrait) {
        orientations = UIInterfaceOrientationMaskPortrait;
    } else if (deviceOrientation == UIDeviceOrientationPortraitUpsideDown) {
        orientations = UIInterfaceOrientationMaskPortraitUpsideDown;
    } else if (deviceOrientation == UIDeviceOrientationLandscapeLeft) {
        orientations = UIInterfaceOrientationMaskLandscapeLeft;
    } else if (deviceOrientation == UIDeviceOrientationLandscapeRight) {
        orientations = UIInterfaceOrientationMaskLandscapeRight;
    }
    
    return orientations;
}

@JakAttak Sorry, I missed the question being about Xcode.

@JakAttak : Thanks for this. It ALMOST works… When launched in portrait mode it works like it should, so it opens in portrait mode and works in both portrait modes, so that’s good.
However, when launched in landscape it will start in landscape, but when the iPad is turned then it changes to portrait, both modes possible, but won’t go back to landscape.
Any idea what causes this?
(I have tried changing the orientation settings in Xcode, that doesn’t help. I guess all modes should be switched on there?)

PS

I don’t mind loosing the possibility to flip the iPad upside down while running, so if it’s easier to just lock the orientation that it started in I would be very happy with that.

Update: I spent some time searching through Google. I found this page that seems to have answers:

http://koreyhinton.com/blog/lock-screen-rotation-in-ios8.html

Among other stuff it says:

-quote

Lock to the rotation they start in

This way is easiest. Just return false to the |shouldAutorotate| method.

class ViewController:UIViewController {
override func shouldAutorotate() ->Bool {
return false
}
}

-unquote

Seems like it could work but I have no clue where to stick this into Xcode.

And perhaps this does not work, as I keep coming across the StandaloneCodeaViewController, which seems to be running things instead of the standard view controller. But as said: this language is 99% gibberish to me, so if anybody can tell me what to do I would be most grateful.

To make matters worse I also found a page where there’s people complaining that the orientation control has radically changed in iOS8 and seems to behave weird. Which might be the cause that JakAttak’s code is ALMOST working: it’s OK when launching in portrait, but not OK when launching in landscape, and I can’t figure out why, as the code is the same for both cases.

To recap:
All I want to do is force rotation lock while my app runs (with (if possible) the option to flip the iPad 180 degrees, but not 90, but I can live with just locking rotation).
It doesn’t seem that much to ask for. Looks like this should be easy if it wasn’t for the StandaloneCodeaViewController that takes over but then fails to execute the Lua supportedOrientations commands in my code. But that’s just my rather uneducated guess at this point.

ANY SUGGESTIONS ANYONE ? PLEASE ?
I will be gifting my app (which is ready for publishing and fully working except for this last problem) to all that offer useful support. First one goes to JakAttak. Won’t tell you what it is, but I’m sure it will entertain you…

@JakAttak: please send your email address to info@yourapphere.nl so I can send you a link to download the app for free

@eriksw, sorry to have ignored this for so long, I’ve been pretty busy. The reason it wasn’t working seems to be that it was not properly getting the current orientation.

Try this:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    NSUInteger orientations = UIInterfaceOrientationMaskAll;
    
    UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
    
    if (UIInterfaceOrientationIsPortrait(interfaceOrientation)) {
        orientations = UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
    } else if (UIInterfaceOrientationIsLandscape(interfaceOrientation)) {
        orientations = UIInterfaceOrientationMaskLandscape;
    }
    
    return orientations;
}

Thanks, I’ll try that as soon as I can. As I don’t have a mac, I need to borrow it from a friend, so it might take a week or more before I can try it. I’ll let you know if it works as soon as I have this macbook available again.

@eriksw, glad it works :slight_smile:

Instead of sending you an email, perhaps I’ll just PM you if that’s alright?

@JakAttak - you have my eternal gratitude: this works exactly like it should. Marvelous ! App update is now waiting for review, should be up in a week or so. So please leave your mail address at info@yourapphere.nl so I can send you a link to download the app for free.

What’s PM ?

Personal message. Hit their user name, then “message”

@eriksw, I sent you a message, you should see it at the top of the screen labeled ‘Inbox’