From 69358fb0adec84e426f380a93b536a554eaf1a8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vitor=20Magalh=C3=A3es?= Date: Tue, 3 Sep 2019 17:58:50 +0100 Subject: [PATCH 1/5] Fixed app2app scenario. --- packages/url_launcher/CHANGELOG.md | 5 +++ .../ios/Classes/UrlLauncherPlugin.m | 41 +++++++++++-------- packages/url_launcher/pubspec.yaml | 2 +- 3 files changed, 29 insertions(+), 19 deletions(-) diff --git a/packages/url_launcher/CHANGELOG.md b/packages/url_launcher/CHANGELOG.md index db2ace15b1e9..b14b10c5d929 100644 --- a/packages/url_launcher/CHANGELOG.md +++ b/packages/url_launcher/CHANGELOG.md @@ -1,3 +1,8 @@ + +## 5.1.3 + +* Fixed iOS app2app scenario. + ## 5.1.2 * Update AGP and gradle. diff --git a/packages/url_launcher/ios/Classes/UrlLauncherPlugin.m b/packages/url_launcher/ios/Classes/UrlLauncherPlugin.m index 56681dcd1ee3..6af7183c6fb3 100644 --- a/packages/url_launcher/ios/Classes/UrlLauncherPlugin.m +++ b/packages/url_launcher/ios/Classes/UrlLauncherPlugin.m @@ -61,12 +61,6 @@ @interface FLTUrlLauncherPlugin () @end -@interface FLTUrlLauncherPlugin () - -@property(strong, nonatomic) UIViewController *viewController; - -@end - @implementation FLTUrlLauncherPlugin + (void)registerWithRegistrar:(NSObject *)registrar { @@ -76,18 +70,10 @@ + (void)registerWithRegistrar:(NSObject *)registrar { UIViewController *viewController = [UIApplication sharedApplication].delegate.window.rootViewController; FLTUrlLauncherPlugin *plugin = - [[FLTUrlLauncherPlugin alloc] initWithViewController:viewController]; + [[FLTUrlLauncherPlugin alloc] init]; [registrar addMethodCallDelegate:plugin channel:channel]; } -- (instancetype)initWithViewController:(UIViewController *)viewController { - self = [super init]; - if (self) { - self.viewController = viewController; - } - return self; -} - - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result { NSString *url = call.arguments[@"url"]; if ([@"canLaunch" isEqualToString:call.method]) { @@ -153,9 +139,9 @@ - (void)launchURLInVC:(NSString *)urlString result:(FlutterResult)result API_AVA self.currentSession.didFinish = ^(void) { weakSelf.currentSession = nil; }; - [self.viewController presentViewController:self.currentSession.safari - animated:YES - completion:nil]; + [self.topViewController presentViewController:self.currentSession.safari + animated:YES + completion:nil]; } - (void)closeWebViewWithResult:(FlutterResult)result API_AVAILABLE(ios(9.0)) { @@ -165,4 +151,23 @@ - (void)closeWebViewWithResult:(FlutterResult)result API_AVAILABLE(ios(9.0)) { result(nil); } +- (UIViewController *)topViewController { + return [self topViewController:[UIApplication sharedApplication].keyWindow.rootViewController]; +} + +- (UIViewController *)topViewController:(UIViewController *)rootViewController +{ + if ([rootViewController isKindOfClass:[UINavigationController class]]) { + UINavigationController *navigationController = (UINavigationController *)rootViewController; + return [self topViewController:[navigationController.viewControllers lastObject]]; + } + if ([rootViewController isKindOfClass:[UITabBarController class]]) { + UITabBarController *tabController = (UITabBarController *)rootViewController; + return [self topViewController:tabController.selectedViewController]; + } + if (rootViewController.presentedViewController) { + return [self topViewController:rootViewController]; + } + return rootViewController; +} @end diff --git a/packages/url_launcher/pubspec.yaml b/packages/url_launcher/pubspec.yaml index f7b77db58818..34343639e9f2 100644 --- a/packages/url_launcher/pubspec.yaml +++ b/packages/url_launcher/pubspec.yaml @@ -3,7 +3,7 @@ description: Flutter plugin for launching a URL on Android and iOS. Supports web, phone, SMS, and email schemes. author: Flutter Team homepage: https://github.com/flutter/plugins/tree/master/packages/url_launcher -version: 5.1.2 +version: 5.1.3 flutter: plugin: From 066725b90defe3345edd139ead711a52e3a9859e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vitor=20Magalh=C3=A3es?= Date: Wed, 4 Sep 2019 10:33:25 +0100 Subject: [PATCH 2/5] Fix for infinite recursion. --- packages/url_launcher/ios/Classes/UrlLauncherPlugin.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/url_launcher/ios/Classes/UrlLauncherPlugin.m b/packages/url_launcher/ios/Classes/UrlLauncherPlugin.m index 6af7183c6fb3..fcc1fc4bddeb 100644 --- a/packages/url_launcher/ios/Classes/UrlLauncherPlugin.m +++ b/packages/url_launcher/ios/Classes/UrlLauncherPlugin.m @@ -166,7 +166,7 @@ - (UIViewController *)topViewController:(UIViewController *)rootViewController return [self topViewController:tabController.selectedViewController]; } if (rootViewController.presentedViewController) { - return [self topViewController:rootViewController]; + return [self topViewController:rootViewController.presentedViewController]; } return rootViewController; } From 92d51b2ae3e677b3926234f16d45c3a64c750d52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vitor=20Magalh=C3=A3es?= Date: Thu, 5 Sep 2019 10:43:31 +0100 Subject: [PATCH 3/5] Fixed PR discussions. --- packages/url_launcher/CHANGELOG.md | 2 +- .../ios/Classes/UrlLauncherPlugin.m | 24 +++++++++++++------ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/packages/url_launcher/CHANGELOG.md b/packages/url_launcher/CHANGELOG.md index b14b10c5d929..0ffbf290a5f7 100644 --- a/packages/url_launcher/CHANGELOG.md +++ b/packages/url_launcher/CHANGELOG.md @@ -1,7 +1,7 @@ ## 5.1.3 -* Fixed iOS app2app scenario. +* Always launch url from the top most UIViewController in iOS. ## 5.1.2 diff --git a/packages/url_launcher/ios/Classes/UrlLauncherPlugin.m b/packages/url_launcher/ios/Classes/UrlLauncherPlugin.m index fcc1fc4bddeb..55225a108d48 100644 --- a/packages/url_launcher/ios/Classes/UrlLauncherPlugin.m +++ b/packages/url_launcher/ios/Classes/UrlLauncherPlugin.m @@ -67,8 +67,6 @@ + (void)registerWithRegistrar:(NSObject *)registrar { FlutterMethodChannel *channel = [FlutterMethodChannel methodChannelWithName:@"plugins.flutter.io/url_launcher" binaryMessenger:registrar.messenger]; - UIViewController *viewController = - [UIApplication sharedApplication].delegate.window.rootViewController; FLTUrlLauncherPlugin *plugin = [[FLTUrlLauncherPlugin alloc] init]; [registrar addMethodCallDelegate:plugin channel:channel]; @@ -152,21 +150,33 @@ - (void)closeWebViewWithResult:(FlutterResult)result API_AVAILABLE(ios(9.0)) { } - (UIViewController *)topViewController { - return [self topViewController:[UIApplication sharedApplication].keyWindow.rootViewController]; + return [self topViewControllerFromViewController:[UIApplication sharedApplication].keyWindow.rootViewController]; } -- (UIViewController *)topViewController:(UIViewController *)rootViewController +/** + * This method recursively iterate through the view hierarchy + * to return the top most view controller. + * + * It supports the following scenarios: + * + * - The view controller is presenting another view. + * - The view controller is a UINavigationController. + * - The view controller is a UITabBarController. + * + * @return The top most view controller. + */ +- (UIViewController *)topViewControllerFromViewController:(UIViewController *)rootViewController { if ([rootViewController isKindOfClass:[UINavigationController class]]) { UINavigationController *navigationController = (UINavigationController *)rootViewController; - return [self topViewController:[navigationController.viewControllers lastObject]]; + return [self topViewControllerFromViewController:[navigationController.viewControllers lastObject]]; } if ([rootViewController isKindOfClass:[UITabBarController class]]) { UITabBarController *tabController = (UITabBarController *)rootViewController; - return [self topViewController:tabController.selectedViewController]; + return [self topViewControllerFromViewController:tabController.selectedViewController]; } if (rootViewController.presentedViewController) { - return [self topViewController:rootViewController.presentedViewController]; + return [self topViewControllerFromViewController:rootViewController.presentedViewController]; } return rootViewController; } From 8033b14b4155634ecc2adea34800f18860ea1337 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vitor=20Magalh=C3=A3es?= Date: Fri, 6 Sep 2019 15:34:44 +0100 Subject: [PATCH 4/5] Renamed parameter. Fixed format. --- .../ios/Classes/UrlLauncherPlugin.m | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/packages/url_launcher/ios/Classes/UrlLauncherPlugin.m b/packages/url_launcher/ios/Classes/UrlLauncherPlugin.m index 55225a108d48..ad30835a4f53 100644 --- a/packages/url_launcher/ios/Classes/UrlLauncherPlugin.m +++ b/packages/url_launcher/ios/Classes/UrlLauncherPlugin.m @@ -165,19 +165,18 @@ - (UIViewController *)topViewController { * * @return The top most view controller. */ -- (UIViewController *)topViewControllerFromViewController:(UIViewController *)rootViewController -{ - if ([rootViewController isKindOfClass:[UINavigationController class]]) { - UINavigationController *navigationController = (UINavigationController *)rootViewController; +- (UIViewController *)topViewControllerFromViewController:(UIViewController *)viewController { + if ([viewController isKindOfClass:[UINavigationController class]]) { + UINavigationController *navigationController = (UINavigationController *)viewController; return [self topViewControllerFromViewController:[navigationController.viewControllers lastObject]]; } - if ([rootViewController isKindOfClass:[UITabBarController class]]) { - UITabBarController *tabController = (UITabBarController *)rootViewController; + if ([viewController isKindOfClass:[UITabBarController class]]) { + UITabBarController *tabController = (UITabBarController *)viewController; return [self topViewControllerFromViewController:tabController.selectedViewController]; } - if (rootViewController.presentedViewController) { - return [self topViewControllerFromViewController:rootViewController.presentedViewController]; + if (viewController.presentedViewController) { + return [self topViewControllerFromViewController:viewController.presentedViewController]; } - return rootViewController; + return viewController; } @end From e61524198a98825c06c9e73885b7f1a5dea1c4b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vitor=20Magalh=C3=A3es?= Date: Fri, 6 Sep 2019 16:19:13 +0100 Subject: [PATCH 5/5] Fixed format. --- .../url_launcher/ios/Classes/UrlLauncherPlugin.m | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/packages/url_launcher/ios/Classes/UrlLauncherPlugin.m b/packages/url_launcher/ios/Classes/UrlLauncherPlugin.m index ad30835a4f53..b21829867a7e 100644 --- a/packages/url_launcher/ios/Classes/UrlLauncherPlugin.m +++ b/packages/url_launcher/ios/Classes/UrlLauncherPlugin.m @@ -67,8 +67,7 @@ + (void)registerWithRegistrar:(NSObject *)registrar { FlutterMethodChannel *channel = [FlutterMethodChannel methodChannelWithName:@"plugins.flutter.io/url_launcher" binaryMessenger:registrar.messenger]; - FLTUrlLauncherPlugin *plugin = - [[FLTUrlLauncherPlugin alloc] init]; + FLTUrlLauncherPlugin *plugin = [[FLTUrlLauncherPlugin alloc] init]; [registrar addMethodCallDelegate:plugin channel:channel]; } @@ -138,8 +137,8 @@ - (void)launchURLInVC:(NSString *)urlString result:(FlutterResult)result API_AVA weakSelf.currentSession = nil; }; [self.topViewController presentViewController:self.currentSession.safari - animated:YES - completion:nil]; + animated:YES + completion:nil]; } - (void)closeWebViewWithResult:(FlutterResult)result API_AVAILABLE(ios(9.0)) { @@ -150,10 +149,11 @@ - (void)closeWebViewWithResult:(FlutterResult)result API_AVAILABLE(ios(9.0)) { } - (UIViewController *)topViewController { - return [self topViewControllerFromViewController:[UIApplication sharedApplication].keyWindow.rootViewController]; + return [self topViewControllerFromViewController:[UIApplication sharedApplication] + .keyWindow.rootViewController]; } -/** +/** * This method recursively iterate through the view hierarchy * to return the top most view controller. * @@ -168,7 +168,8 @@ - (UIViewController *)topViewController { - (UIViewController *)topViewControllerFromViewController:(UIViewController *)viewController { if ([viewController isKindOfClass:[UINavigationController class]]) { UINavigationController *navigationController = (UINavigationController *)viewController; - return [self topViewControllerFromViewController:[navigationController.viewControllers lastObject]]; + return [self + topViewControllerFromViewController:[navigationController.viewControllers lastObject]]; } if ([viewController isKindOfClass:[UITabBarController class]]) { UITabBarController *tabController = (UITabBarController *)viewController;