iOS Code Snippet [beta] Forcing the app orientation at runtime

Add to main module:
B4X:
#if OBJC
@end
@interface B4IAppDelegate (orientation)
@end
@implementation B4IAppDelegate (orientation)
- (UIInterfaceOrientationMask)application:(UIApplication *)application 
  supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    return [b4i_main new]._morientation;
}
#End If

B4X:
Public Sub SetSupportedOrientation(Orientation As Int)
    mOrientation = Orientation
    If App.KeyController <> Null Then
        App.KeyController.As(NativeObject).RunMethod("setNeedsUpdateOfSupportedInterfaceOrientations", Null)
    End If
End Sub

And Process_Globals:
B4X:
    Public const ORIENTATION_PORTRAIT = 2, ORIENTATION_LANDSCAPE = 24, ORIENTATION_ALL = 30 As Int
    Private mOrientation As Int = ORIENTATION_ALL 'ignore

Call Main.SetSupportedOrientation to change orientation.

Tested on iOS 17.
 

Attachments

  • Project.zip
    8.6 KB · Views: 30

JackKirk

Well-Known Member
Licensed User
Longtime User
Thanks ever so much for this Erel, it is a game changer in my app.

I would suggest 2 relatively minor changes:
B4X:
    Public const ORIENTATION_PORTRAIT = 2, ORIENTATION_LANDSCAPE = 24, ORIENTATION_ALLBUTUPSIDEDOWN = 26, ORIENTATION_ALL = 30 As Int
    Private mOrientation As Int = ORIENTATION_ALL 'ignore
As I understand it the ORIENTATION_ALLBUTUPSIDEDOWN is the normal default.

Public Sub SetSupportedOrientation(Orientation As Int) mOrientation = Orientation If App.KeyController <> Null Then App.KeyController.As(NativeObject).RunMethod("setNeedsUpdateOfSupportedInterfaceOrientations", Null) End If End Sub:
Public Sub SetSupportedOrientation(Orientation As Int)
 
    Private wrk_osversion As String
    wrk_osversion = App.OSVersion & "."
    wrk_osversion = wrk_osversion.SubString2(0, wrk_osversion.IndexOf("."))
 
    'setNeedsUpdateOfSupportedInterfaceOrientations is only supported from iOS 16 on...
    If wrk_osversion >= 16 Then

        mOrientation = Orientation
        If App.KeyController <> Null Then
            App.KeyController.As(NativeObject).RunMethod("setNeedsUpdateOfSupportedInterfaceOrientations", Null)
        End If
   
    End If
 
End Sub
Without this test the app will bomb on iOS versions prior to 16

Thanks again...

PS I applied this to my app and ran it in AWS Device Farm on an iPhone 15 running iOS 17.3.1 - all ran perfectly.
I will test it on a few other Device Farm versions - if you hear no more from me assume all tests were OK
 
Last edited:
Top