(Flutter) Deep links not working on iOS when app is killed
Asked Answered
S

2

6

My deep links on iOS aren't working correctly (I followed the official Flutter guide on how to implement deep links). On Android, everything works perfectly fine, but on iOS (when the app is killed), the app just launches from the link but stays at the home page (doesn't navigate to the correct route).

I use auto_route for routing.

This is my routerDelegate in MaterialApp.router:

          routerDelegate: _appRouter.delegate(
            deepLinkBuilder: (deepLink) {
              if (RegExp(r'\/oferta\/[^\/]+\/[^\/]+.*$')
                  .hasMatch(deepLink.path)) {
                return deepLink;
              } else {
                return DeepLink.defaultPath;
              }
            },
          ),

// Edit:

I’ve tried to implement routing with go_router instead of auto_route hoping the problem would solve and was occurring due to using auto_route. Also go_router doesn’t use routerDelegate or deepLinkBuilder, because it displays the configured screen based on the URL path. Unfortunatelly it did not solve my problem. When the app is killed, deeplinks open the app at homePage, they don't navigate to the desired screen on iOS. Android still works perfectly fine.

Satire answered 5/10, 2023 at 10:17 Comment(3)
Have you checked if the regex matches correctly?Decompound
@AbhinavMathur it's definitely not the problem of my regex (since it works on iOS when the app was in background as expected). The issue is occuring only when the app was killed.Satire
@Satire are you able to fix the issue ? I am also facing same issue in my app.Catchascatchcan
G
1

First, when dealing with deep links on iOS, it's essential to make sure you've set up the necessary configurations in your Xcode project. Here are some steps to follow:

URL Types in Info.plist: Ensure that your app's Info.plist file has the URL Types configured correctly. You need to specify the URL scheme for your app, which should match the one you use in your deep links.

  • Open the Info.plist file.
  • Add a new "URL Types" key if it doesn't already exist.
  • Add an item under "URL Types" and set your URL scheme.

Associated Domains: If your deep links involve domains (like https://example.com/deeplink), you need to set up Associated Domains in Xcode. This step is important for Universal Links, which are a more reliable way of handling deep links.

Example:

import 'package:flutter/material.dart';
import 'package:uni_links/uni_links.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  void initState() {
    super.initState();
    initUniLinks();
  }

  void initUniLinks() async {
    try {
      final initialLink = await getInitialLink();
      handleDeepLink(initialLink);
    } on Exception {
      // Handle any exceptions
    }

    linkStream.listen((String? link) {
      if (link != null) {
        handleDeepLink(link);
      }
    });
  }

  void handleDeepLink(String link) {
    // Handle your deep link here
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // Your app's content
    );
  }
}

Gurgle answered 19/10, 2023 at 4:53 Comment(3)
I have done all the things, but the issue persisted still...Satire
@Md Rasheduzzaman Could you solve this issue?Yaroslavl
Yes, Did you fix this issue ? Or still stack in that position.Gurgle
S
-1
  1. If it is working in android devices, you need to check permission in the 'info.plist' file.
  2. deep-linking often does not work with virtual devices(simulators), so please try to use real devices.
Saturated answered 13/10, 2023 at 12:11 Comment(3)
I tried on physical devices as well as on simulators. It just doesn't work on iOS when the app was killed before launching the deep link. Everything works as expected when the app was just in the background. What permissions should I have declared in my Info.plist? So far I only added associated-domains to my Runner.entitlements.Satire
Do you have the key <key>FlutterDeepLinkingEnabled</key>Autumn
I do have this key set to true.Satire

© 2022 - 2025 — McMap. All rights reserved.