最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Enabling DeepLinks in iOS AppDelegate.swift for the latest version of react native - Stack Overflow

programmeradmin1浏览0评论

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
  • Search the method 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
  • you don't have to write the code by your own, we can config through Xcode - add URL Types - add associated domain developer.apple/documentation/Xcode/… – Suppose Commented Apr 3 at 6:54
Add a comment  | 

1 Answer 1

Reset to default 0

To handle custom URL schemes in your React Native iOS app, you need to make sure that AppDelegate.swift is properly configured. Follow these steps:

  1. 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.

  2. 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.

  1. 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.

  1. Build your project: Once the code is added and the configuration is done, build and run your project in Xcode.

  2. 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.

  1. Verify: Check if the app opens correctly when using the custom URL scheme. If it works, your URL handling is set up correctly.
发布评论

评论列表(0)

  1. 暂无评论