In my React Native app, I had to migrate its AppDelegate
from Objective-C to Swift. After the migration, deeplinks stopped working: when a link is used, the app opens but it does not navigate to the desired route, while in Android it works fine.
Following React Navigation docs, I had this code in my AppDelegate.m
:
#import <React/RCTLinkingManager.h>
// other imports
@implementation AppDelegate
// other stuff
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
if ([[FBSDKApplicationDelegate sharedInstance] application:application openURL:url options:options]) {
return YES;
}
if ([RCTLinkingManager application:application openURL:url options:options]) {
return YES;
}
return NO;
}
- (BOOL)application:(UIApplication *)application continueUserActivity:(nonnull NSUserActivity *)userActivity
restorationHandler:(nonnull void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler
{
return [RCTLinkingManager application:application
continueUserActivity:userActivity
restorationHandler:restorationHandler];
}
@end
which became the following in Swift:
// imports
@main
// other stuff
func application(_ application: UIApplication, open URL: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
if (ApplicationDelegate.shared.application(application, open: URL, options: options)) {
return true
}
if (RCTLinkingManager.application(application, open: URL, options: options)) {
return true;
}
return false
}
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
return RCTLinkingManager.application(application, continue: userActivity, restorationHandler: restorationHandler)
}
I also have a myApp-Bridging-Header.h
file:
// other imports
#import <React/RCTLinkingManager.h>
I don't know if I'm doing something wrong (as I'm pretty lost with Objective-C or Swift) or if deeplinks with an AppDelegate
written in Swift just don't work.
Update
The problem seems related with scenes. After adding this code to my PhoneScene, deepLinks work, but only if the app is open or in the background; if it's closed it opens, but in home screen.
func scene( _ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
guard let url = URLContexts.first?.url else {
return
}
if RCTLinkingManager.application(UIApplication.shared, open: url, sourceApplication: nil, annotation: [UIApplication.OpenURLOptionsKey.annotation]) {
return
}
}
func scene( _ scene: UIScene, continue userActivity: NSUserActivity) {
RCTLinkingManager.application(UIApplication.shared, continue: userActivity, restorationHandler: { _ in })
}