diff --git a/packages/share_plus/share_plus/CHANGELOG.md b/packages/share_plus/share_plus/CHANGELOG.md index 13e83627d2..b5520e9577 100644 --- a/packages/share_plus/share_plus/CHANGELOG.md +++ b/packages/share_plus/share_plus/CHANGELOG.md @@ -1,5 +1,6 @@ ## 4.5.0 +- iOS: Remove usage of deprecated UIApplication.keyWindow in iOS 13+ - Add `shareXFiles` implementations - Deprecate `shareFiles*` implementations - Enable `shareXFiles` implementations on Web 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