Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/url_launcher/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@

## 5.1.3

* Always launch url from the top most UIViewController in iOS.

## 5.1.2

* Update AGP and gradle.
Expand Down
55 changes: 35 additions & 20 deletions packages/url_launcher/ios/Classes/UrlLauncherPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -61,33 +61,17 @@ @interface FLTUrlLauncherPlugin ()

@end

@interface FLTUrlLauncherPlugin ()

@property(strong, nonatomic) UIViewController *viewController;

@end

@implementation FLTUrlLauncherPlugin

+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
FlutterMethodChannel *channel =
[FlutterMethodChannel methodChannelWithName:@"plugins.flutter.io/url_launcher"
binaryMessenger:registrar.messenger];
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]) {
Expand Down Expand Up @@ -153,9 +137,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)) {
Expand All @@ -165,4 +149,35 @@ - (void)closeWebViewWithResult:(FlutterResult)result API_AVAILABLE(ios(9.0)) {
result(nil);
}

- (UIViewController *)topViewController {
return [self topViewControllerFromViewController:[UIApplication sharedApplication].keyWindow.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
Comment thread
vitor-gyant marked this conversation as resolved.
Outdated
{
if ([rootViewController isKindOfClass:[UINavigationController class]]) {
UINavigationController *navigationController = (UINavigationController *)rootViewController;
return [self topViewControllerFromViewController:[navigationController.viewControllers lastObject]];
}
if ([rootViewController isKindOfClass:[UITabBarController class]]) {
UITabBarController *tabController = (UITabBarController *)rootViewController;
return [self topViewControllerFromViewController:tabController.selectedViewController];
}
if (rootViewController.presentedViewController) {
return [self topViewControllerFromViewController:rootViewController.presentedViewController];
}
return rootViewController;
}
@end
2 changes: 1 addition & 1 deletion packages/url_launcher/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <flutter-dev@googlegroups.com>
homepage: https://github.com/flutter/plugins/tree/master/packages/url_launcher
version: 5.1.2
version: 5.1.3

flutter:
plugin:
Expand Down