From e7f1e084c5ae6bd1ffb04c0df161dc3fc2eb145a Mon Sep 17 00:00:00 2001 From: daniel-shuy Date: Tue, 4 Oct 2022 22:56:48 +0800 Subject: [PATCH] [share_plus] Remove usage of deprecated UIApplication.keyWindow in iOS 13+ --- packages/share_plus/share_plus/CHANGELOG.md | 4 +++ .../ios/Classes/FLTSharePlusPlugin.m | 35 +++++++++++++++++-- packages/share_plus/share_plus/pubspec.yaml | 2 +- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/packages/share_plus/share_plus/CHANGELOG.md b/packages/share_plus/share_plus/CHANGELOG.md index b72761504f..7338ec258d 100644 --- a/packages/share_plus/share_plus/CHANGELOG.md +++ b/packages/share_plus/share_plus/CHANGELOG.md @@ -1,3 +1,7 @@ +## 4.5.0 + +- iOS: Remove usage of deprecated UIApplication.keyWindow in iOS 13+ + ## 4.4.0 - Reverted changes in 4.2.0 due to crash issues. See #1081 diff --git a/packages/share_plus/share_plus/ios/Classes/FLTSharePlusPlugin.m b/packages/share_plus/share_plus/ios/Classes/FLTSharePlusPlugin.m index cacd119b32..67cfa2f4ec 100644 --- a/packages/share_plus/share_plus/ios/Classes/FLTSharePlusPlugin.m +++ b/packages/share_plus/share_plus/ios/Classes/FLTSharePlusPlugin.m @@ -9,7 +9,22 @@ static NSString *const PLATFORM_CHANNEL = @"dev.fluttercommunity.plus/share"; static UIViewController *RootViewController() { - return [UIApplication sharedApplication].keyWindow.rootViewController; + if (@available(iOS 13, *)) { // UIApplication.keyWindow is deprecated + NSSet *scenes = [[UIApplication sharedApplication] connectedScenes]; + for (UIScene *scene in scenes) { + if ([scene isKindOfClass:[UIWindowScene class]]) { + NSArray *windows = ((UIWindowScene *)scene).windows; + for (UIWindow *window in windows) { + if (window.isKeyWindow) { + return window.rootViewController; + } + } + } + } + return nil; + } else { + return [UIApplication sharedApplication].keyWindow.rootViewController; + } } static UIViewController * @@ -259,8 +274,15 @@ + (void)registerWithRegistrar:(NSObject *)registrar { return; } + UIViewController *rootViewController = RootViewController(); + if (!rootViewController) { + result([FlutterError errorWithCode:@"error" + message:@"No root view controller found" + details:nil]); + return; + } UIViewController *topViewController = - TopViewControllerForViewController(RootViewController()); + TopViewControllerForViewController(rootViewController); [self shareText:shareText subject:shareSubject @@ -293,8 +315,15 @@ + (void)registerWithRegistrar:(NSObject *)registrar { } } + UIViewController *rootViewController = RootViewController(); + if (!rootViewController) { + result([FlutterError errorWithCode:@"error" + message:@"No root view controller found" + details:nil]); + return; + } UIViewController *topViewController = - TopViewControllerForViewController(RootViewController()); + TopViewControllerForViewController(rootViewController); [self shareFiles:paths withMimeType:mimeTypes withSubject:subject diff --git a/packages/share_plus/share_plus/pubspec.yaml b/packages/share_plus/share_plus/pubspec.yaml index a1f1601245..79df4f9ccc 100644 --- a/packages/share_plus/share_plus/pubspec.yaml +++ b/packages/share_plus/share_plus/pubspec.yaml @@ -1,6 +1,6 @@ name: share_plus description: Flutter plugin for sharing content via the platform share UI, using the ACTION_SEND intent on Android and UIActivityViewController on iOS. -version: 4.4.0 +version: 4.5.0 homepage: https://plus.fluttercommunity.dev/ repository: https://github.com/fluttercommunity/plus_plugins/tree/main/packages/ issue_tracker: https://github.com/fluttercommunity/plus_plugins/labels/share_plus