apache / cordova-plugin-screen-orientation

Apache Cordova Screen Orientation Plugin
https://cordova.apache.org/
Apache License 2.0
219 stars 229 forks source link

iOS 16 may not respect a request to change orientation #100

Closed msmtamburro closed 1 year ago

msmtamburro commented 2 years ago

Bug Report

Problem

Getting this issue on our radar early:

https://developer.apple.com/forums/thread/707735?answerId=721941022#721941022

What is expected to happen?

This approach used to work, but may not on iOS 16: [[UIDevice currentDevice] setValue:@(orientation) forKey:@"orientation"]

We may need to switch to: https://developer.apple.com/documentation/uikit/uiwindowscene/3975944-requestgeometryupdate/ (which would require building from XCode 14)

What does actually happen?

Information

Command or Code

Environment, Platform, Device

Version information

iOS 16 beta 4

Checklist

msmtamburro commented 2 years ago

Specific guidance: https://developer.apple.com/documentation/ios-ipados-release-notes/ios-ipados-16-release-notes

[UIViewController shouldAutorotate] has been deprecated is no longer supported. [UIViewController attemptRotationToDeviceOrientation] has been deprecated and replaced with [UIViewController setNeedsUpdateOfSupportedInterfaceOrientations].

Workaround: Apps relying on shouldAutorotate should reflect their preferences using the view controllers supportedInterfaceOrientations. If the supported orientations change, use `-[UIViewController setNeedsUpdateOfSupportedInterface

msmtamburro commented 2 years ago

Replacing line 78 with something like this might work (untested):

                if (@available(iOS 16.0, *)) {
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_15_5 // Xcode 14 and iOS 16, or greater
                    [self.viewController setNeedsUpdateOfSupportedInterfaceOrientations];
#endif
                } else {
                    [UINavigationController attemptRotationToDeviceOrientation];
                }
tookiez commented 2 years ago

Hello there!

i've tried replacing

[UINavigationController attemptRotationToDeviceOrientation]

with your code. Unfortunately when i force the orientationChange, it doesn't trigger. Meanwhile i cannot find any indication or clear example to use requestGeometryUpdate

tory37 commented 2 years ago

Upvoting that suggested change did not work for us either.

msmtamburro commented 2 years ago

Apologies that I still haven't had time to play with this. For diagnostics: When you're trying to use setNeedsUpdateOfSupportedInterfaceOrientations from Xcode 14 RC on iOS 16, does your list of supported orientations only include the desired orientation?

Robertndrei commented 2 years ago

Hi, the fix is working, but you have to replace line 83 [[UIDevice currentDevice] setValue:value forKey:@"orientation"]; with your code

tookiez commented 2 years ago

@Robertndrei ok, i've replaced every

[UIDevice currentDevice] setValue:value forKey:@"orientation"]

and

[UINavigationController attemptRotationToDeviceOrientation]

with

  if (@available(iOS 16.0, *)) {
      #if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_15_5 // Xcode 14 and iOS 16, or greater
          [self.viewController setNeedsUpdateOfSupportedInterfaceOrientations];
      #endif
  } else {
      [UINavigationController attemptRotationToDeviceOrientation];
  } 

and now seems work correctly

chrisvasz commented 2 years ago

@tookiez's fix (replacing lines 78 and 83 with the suggestion from above) worked for me too. How can we help get this fix released?

Robertndrei commented 2 years ago

Actually only line 83 is necessary to be replaced

jaydrogers commented 2 years ago

Thanks for tracing this down, everyone!

I added a PR, but since the last update on this library was 2019, I have a feeling no PR will be merged in the near future: https://github.com/apache/cordova-plugin-screen-orientation/pull/102

"Band-aid" Repo

In the meantime, I created a repo with the fix: https://github.com/521dimensions/cordova-plugin-screen-orientation

You can use it with:

cordova plugin add git+ssh://git@github.com:521dimensions/cordova-plugin-screen-orientation.git

Hope this helps someone else!

jansgescheit commented 2 years ago

@jaydrogers i tested your fork on a Simulator with a iPhone X on iOS 12.4 Compiled on XCode 14. Add the moment screen orientation does not lock in any directions

jaydrogers commented 2 years ago

Strange!

If you find issues, chime in on this PR. Looks like a contributor is looking at it now: https://github.com/apache/cordova-plugin-screen-orientation/pull/102

laughsky commented 2 years ago

if use xcode 13,the following is better:

if (@available(iOS 16.0, *)) {
    #if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_15_5
        SEL supportedInterfaceSelector = NSSelectorFromString(@"setNeedsUpdateOfSupportedInterfaceOrientations");
        [self.viewController performSelector:supportedInterfaceSelector];
    #endif
} else {
    [UINavigationController attemptRotationToDeviceOrientation];
}