I’m trying to implement deep linking with go_router. Everything is working fine on Android, but on iOS opening the deep link URL opens the app to the correct screen only if the app is open and it is in the background. If the app is closed, the deep link URL only opens the login screen.
Here is my setup
Info.plist
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLSchemes</key>
<array>
<string>appscheme</string>
<string>${GOOGLE_REVERSED_CLIENT_ID}</string>
</array>
</dict>
</array>
app.dart
MaterialApp.router(
debugShowCheckedModeBanner: false,
routerConfig: GoRouter(
routes: [
GoRoute(
path: '/',
builder: (context, state) => const LoginPage( key: Key('loginPage',),),
),
GoRoute(
path: '/sign-up',
builder: (context, state) => const SignUpPage( key: Key('signUpPage'), ),
),
],
),
);
To invoke deeplink
xcrun simctl openurl booted "appscheme:/sign-up"
2
Answers
Let’s see how you can handle deeplink on iOS:
Swift Code in AppDelegate.swift:
Explanation for AppDelegate.swift:
AppDelegate.swift
, we set up deep link handling for a Flutter app running on iOS.MethodChannel
named "deeplink.flutter.dev/channel" to allow Dart to invoke methods in Swift.EventChannel
named "deeplink.flutter.dev/events" is created to listen for deep link events from Swift to Dart.GeneratedPluginRegistrant.register(with: self)
.LinkStreamHandler
class handles incoming deep links and sends them to the Flutter app.Now, let’s explain the Dart code in
main.dart
:Dart Code in main.dart:
Explanation for main.dart:
main.dart
, we set up deep link handlingfor a Flutter app.
EventChannel
named "deeplink.flutter.dev/events" to listen for deep link events from Swift to Dart.MethodChannel
named "deeplink.flutter.dev/channel" is created to invoke methods in Swift.startUri
is a function that attempts to invoke the "initialLink" method from Swift and handles exceptions.onRedirected
is a function that processes deep link URIs, pushing corresponding routes using the GoRouter package.router
object defines routes and pages for the GoRouter package.These code segments together enable seamless handling of deep links between Flutter and native iOS code, allowing control over deep link transitions within the Flutter app on iOS using Swift. While the provided Swift code works well, the uni_links package offers a more convenient and feature-rich solution for deep link handling in Flutter applications. You can find more information about the uni_links package here.
For more details, you can refer to the article here.
I think the issue is with the URL you’re trying to pass. Instead of passing the path after the scheme, add a host.
Here’s a link to the GitHub issue which demonstrates the same behaviour.