提交 c63ca1f7 authored 作者: jungleiOS's avatar jungleiOS

修复 iOS 16 不能强制旋转问题

上级 917a0248
......@@ -13,3 +13,15 @@ For help getting started with Flutter development, view the
[online documentation](https://flutter.dev/docs), which offers tutorials,
samples, guidance on mobile development, and a full API reference.
## ios 16 旋转
若想在 iOS 16 上强制旋转生效
1. 保证 info.plist 表里 Supported interface orientations 不要四个方向都勾上
2. 请在 AppDelegate 中加入如下代码,允许所有旋转方向
```js
- (UIInterfaceOrientationMask)application:(UIApplication *)application
supportedInterfaceOrientationsForWindow:(UIWindow *)window {
return UIInterfaceOrientationMaskAll;
}
```
......@@ -10,4 +10,8 @@
return [super application:application didFinishLaunchingWithOptions:launchOptions];
}
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
return UIInterfaceOrientationMaskAll;
}
@end
......@@ -2,6 +2,8 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CADisableMinimumFrameDurationOnPhone</key>
<true/>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleDisplayName</key>
......@@ -24,28 +26,26 @@
<string>$(FLUTTER_BUILD_NUMBER)</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UIApplicationSupportsIndirectInputEvents</key>
<true/>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
<string>Main</string>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
<string>UIInterfaceOrientationPortrait</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
</array>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
<key>CADisableMinimumFrameDurationOnPhone</key>
<true/>
<key>UIApplicationSupportsIndirectInputEvents</key>
<true/>
</dict>
</plist>
//
// OrientationPlugin+Hook.h
// Runner
//
// Created by 蒋俊 on 2023/9/13.
//
#import "OrientationPlugin.h"
NS_ASSUME_NONNULL_BEGIN
@interface OrientationPlugin (Hook)
@end
NS_ASSUME_NONNULL_END
//
// OrientationPlugin+Hook.m
// Runner
//
// Created by 蒋俊 on 2023/9/13.
//
#import "OrientationPlugin+Hook.h"
#import <objc/runtime.h>
@implementation OrientationPlugin (Hook)
+ (void)load
{
Method originalMethod = class_getInstanceMethod(self, NSSelectorFromString(@"forceOrientation:"));
Method swizzledMethod = class_getInstanceMethod(self, @selector(hook_forceOrientation:));
method_exchangeImplementations(originalMethod, swizzledMethod);
}
- (void)hook_forceOrientation:(NSString *)orientation {
// 若想在 iOS 16 上强制旋转生效
// 1.请在 AppDelegate 中加入如下代码,允许所有旋转方向
// 2.info.plist 表里 Supported interface orientations 不要四个方向都勾上
// - (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
//
// return UIInterfaceOrientationMaskAll;
// }
if (@available(iOS 16.0, *)) {
UIWindowSceneGeometryPreferencesIOS *perference = [[UIWindowSceneGeometryPreferencesIOS alloc] init];
if ([orientation isEqualToString:@"DeviceOrientation.portraitUp"]) {
perference.interfaceOrientations = UIInterfaceOrientationMaskPortrait;
[self requestGeometryUpdateWithPreferences: perference];
} else if ([orientation isEqualToString:@"DeviceOrientation.portraitDown"]) {
perference.interfaceOrientations = UIInterfaceOrientationMaskPortraitUpsideDown;
[self requestGeometryUpdateWithPreferences: perference];
} else if ([orientation isEqualToString:@"DeviceOrientation.landscapeLeft"]) {
perference.interfaceOrientations = UIInterfaceOrientationMaskLandscapeLeft;
[self requestGeometryUpdateWithPreferences: perference];
} else if ([orientation isEqualToString:@"DeviceOrientation.landscapeRight"]) {
perference.interfaceOrientations = UIInterfaceOrientationMaskLandscapeRight;
[self requestGeometryUpdateWithPreferences: perference];
} else {
perference.interfaceOrientations = UIInterfaceOrientationMaskPortrait;
[self requestGeometryUpdateWithPreferences: perference];
}
} else {
if ([orientation isEqualToString:@"DeviceOrientation.portraitUp"]) {
[[UIDevice currentDevice] setValue:@(UIInterfaceOrientationPortrait) forKey:@"orientation"];
} else if ([orientation isEqualToString:@"DeviceOrientation.portraitDown"]) {
[[UIDevice currentDevice] setValue:@(UIInterfaceOrientationPortraitUpsideDown) forKey:@"orientation"];
} else if ([orientation isEqualToString:@"DeviceOrientation.landscapeLeft"]) {
[[UIDevice currentDevice] setValue:@(UIInterfaceOrientationLandscapeLeft) forKey:@"orientation"];
} else if ([orientation isEqualToString:@"DeviceOrientation.landscapeRight"]) {
[[UIDevice currentDevice] setValue:@(UIInterfaceOrientationLandscapeRight) forKey:@"orientation"];
} else {
[[UIDevice currentDevice] setValue:@(UIInterfaceOrientationUnknown) forKey:@"orientation"];
}
}
}
- (void)requestGeometryUpdateWithPreferences:(UIWindowSceneGeometryPreferences *)geometryPreferences API_AVAILABLE(ios(16.0)){
UIWindowScene *windowScene = (UIWindowScene *)[[[UIApplication sharedApplication] connectedScenes] allObjects].firstObject;
[UIViewController attemptRotationToDeviceOrientation];
[windowScene requestGeometryUpdateWithPreferences: geometryPreferences
errorHandler: ^( NSError * _Nonnull error) {
NSLog(@"error--%@", error);
}];
UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
if (keyWindow != NULL) {
[keyWindow.rootViewController setNeedsUpdateOfSupportedInterfaceOrientations];
}
}
@end
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论