5

If it's possible, is it an easy implementation or a tough one?

I had difficulty getting a clear idea in Flutter.io's documentation.

Deborah
  • 4,316
  • 8
  • 31
  • 45

2 Answers2

3

You can use platform channel for this. It shouldn't be tough. You need to add handlers in native code and redirect urls via channels to flutter code. Example for iOS:

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  [GeneratedPluginRegistrant registerWithRegistry:self];
  FlutterViewController *controller = (FlutterViewController*)self.window.rootViewController;

  self.urlChannel = [FlutterMethodChannel methodChannelWithName:@"com.myproject/url" binaryMessenger:controller];

  return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{

  [self.urlChannel invokeMethod:@"openURL"
                      arguments:@{@"url" : url.absoluteString}];

  return true;
}

@end

And basic flutter code:

class _MyHomePageState extends State<MyHomePage> {

  final MethodChannel channel = const MethodChannel("com.myproject/url");

  String _url;

  @override
  initState() {
    super.initState();

    channel.setMethodCallHandler((MethodCall call) async {
      debugPrint("setMethodCallHandler call = $call");

      if (call.method == "openURL") {
        setState(() => _url = call.arguments["url"]);
      }
    });
  }


  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(_url ?? "No URL"),
      ),
    );
  }
}
German Saprykin
  • 6,631
  • 2
  • 29
  • 26
  • 1
    error: property 'urlChannel' not found on object of type 'AppDelegate *' self.urlChannel = [FlutterMethodChannel methodChannelWithName:@"com.myproject/url" binaryMessenger:controller]; – bastimm Jan 31 '19 at 15:33
  • This is the only place I have been able to find simple code to use for this task!.Thank you. – davaus Sep 17 '19 at 23:12
1

For anyone need updated solution: you can use Google Dynamic Links for Firebase and another guideline on Medium

Thuong
  • 602
  • 6
  • 13