I'm trying to add the necessary code to support deep links in my React Native 0.78.1 app (not using Expo). 0.78.1 created an AppDelegate.swift, not an AppDelegate.m like older versions of RN. However, looking at the RN Deep Links documentation (located here: RN Deep Links), those instructions suggest the code below, but only for AppDelegate.m, not AppDelegate.swift:
I don't know either language so I'm not sure how to proceed. Could someone provide the correct implementation for AppDelegate.swift, or point me to documentation on the swift implementation elsewhere that I might have missed?
// Add the header at the top of the file:
#import <React/RCTLinkingManager.h>
// Add this inside `@implementation AppDelegate` above `@end`:
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
return [RCTLinkingManager application:application openURL:url options:options];
}
// Add this inside `@implementation AppDelegate` above `@end`:
- (BOOL)application:(UIApplication *)application continueUserActivity:(nonnull NSUserActivity *)userActivity
restorationHandler:(nonnull void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler
{
return [RCTLinkingManager application:application
continueUserActivity:userActivity
restorationHandler:restorationHandler];
}.
I'm trying to add the necessary code to support deep links in my React Native 0.78.1 app (not using Expo). 0.78.1 created an AppDelegate.swift, not an AppDelegate.m like older versions of RN. However, looking at the RN Deep Links documentation (located here: RN Deep Links), those instructions suggest the code below, but only for AppDelegate.m, not AppDelegate.swift:
I don't know either language so I'm not sure how to proceed. Could someone provide the correct implementation for AppDelegate.swift, or point me to documentation on the swift implementation elsewhere that I might have missed?
// Add the header at the top of the file:
#import <React/RCTLinkingManager.h>
// Add this inside `@implementation AppDelegate` above `@end`:
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
return [RCTLinkingManager application:application openURL:url options:options];
}
// Add this inside `@implementation AppDelegate` above `@end`:
- (BOOL)application:(UIApplication *)application continueUserActivity:(nonnull NSUserActivity *)userActivity
restorationHandler:(nonnull void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler
{
return [RCTLinkingManager application:application
continueUserActivity:userActivity
restorationHandler:restorationHandler];
}.
Share
Improve this question
asked Mar 30 at 0:11
RJMRJM
1,1869 silver badges21 bronze badges
2
|
1 Answer
Reset to default 0To handle custom URL schemes in your React Native iOS app, you need to make sure that AppDelegate.swift is properly configured. Follow these steps:
Open your AppDelegate.swift file: Navigate to the AppDelegate.swift file in your Xcode project. This file is typically located in the ios directory under YourProjectName/ios/YourProjectName/AppDelegate.swift.
Add the provided code to the end of AppDelegate.swift: Add the following method at the end of your AppDelegate.swift file:
override func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
return RCTLinkingManager.application(app, open: url, options: options)
}
This method allows your app to handle URLs passed into it, such as appauth://yourpage.
- Configure URL scheme in your app: Make sure that your app's Info.plist file is set up to handle the custom URL scheme. Add the following to your Info.plist:
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>appauth</string> <!-- Replace with your app's custom URL scheme -->
</array>
</dict>
</array>
Replace "appauth" with your actual custom URL scheme.
Build your project: Once the code is added and the configuration is done, build and run your project in Xcode.
Test the custom URL scheme: To test the URL scheme, you can use the following command in your terminal:
bash Copy xcrun simctl openurl booted appauth://yourpage Make sure to replace appauth with your custom scheme, and yourpage with your specific URL path.
- Verify: Check if the app opens correctly when using the custom URL scheme. If it works, your URL handling is set up correctly.
application:openURL:options:
in doc developer.apple/documentation/uikit/uiapplicationdelegate/… switch to Swift and you'll see the declaration. Same for the other one. Then call the RCTLinkingManager version. – Larme Commented Mar 30 at 0:43