【问题标题】:How to setup react-native-navigation with react-native-siri-shortcut (rootView issue)如何使用 react-native-siri-shortcut 设置 react-native-navigation(rootView 问题)
【发布时间】:2019-01-17 17:38:53
【问题描述】:

需要一些关于如何让RNNreact-native-siri-shortcut 合作的指导。 如果我的帖子过于冗长,请多多包涵,因为我是 xCodeobjective C 的菜鸟,所以不要错过任何内容。

所以我在尝试让这 2 个库一起工作时遇到了几个问题/问题:

1。使用RCTRootView 设置initialProperties

react-native-siri-shortcutsAppDelegate.m 中设置应用程序如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
 // Check if the app launched with any shortcuts
    BOOL launchedFromShortcut = [launchOptions objectForKey:@"UIApplicationLaunchOptionsUserActivityDictionaryKey"] != nil;
    //Add a boolean to the initialProperties to let the app know you got the initial shortcut
    NSDictionary *initialProperties = @{ @"launchedFromShortcut":@(launchedFromShortcut) };

    RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
                                                        moduleName:@"doesthismatter"
                                                 initialProperties:initialProperties // Add the initial properties here
                                                     launchOptions:launchOptions];

...

}

在 RNN 的 V2 中,不再使用 rootView,而是直接调用:

[ReactNativeNavigation bootstrap:jsCodeLocation launchOptions:launchOptions];

因此我们无法将initialProperties 传递给rootView

2。在代码的其他部分访问rootView

继续设置react-native-siri-shortcut

// This method checks for shortcuts issued to the app
- (BOOL)application:(UIApplication *)application
continueUserActivity:(NSUserActivity *)userActivity
 restorationHandler:(void (^)(NSArray<id<UIUserActivityRestoring>> *restorableObjects))restorationHandler
{
  UIViewController *viewController = [self.window rootViewController];
  RCTRootView *rootView = (RCTRootView*) [viewController view];

  // If the initial properties say the app launched from a shortcut (see above), tell the library about it.
  if ([[rootView.appProperties objectForKey:@"launchedFromShortcut"] boolValue]) {
    ShortcutsModule.initialUserActivity = userActivity;

    rootView.appProperties = @{ @"launchedFromShortcut":@NO };
  }

  [ShortcutsModule onShortcutReceivedWithUserActivity:userActivity];

  return YES;
}

由于RCTRootView 没有被用来注册我们的应用程序,这部分代码可以工作吗?

我已经在 repo 中搜索了与 initialPropsreactView 相关的问题,并且我确实发现了一些问题,但从未得到答复并由于不活动而被关闭。

任何帮助将不胜感激。

【问题讨论】:

    标签: react-native-navigation


    【解决方案1】:

    实际上,在github 上搜索launchedFromShortcut 属性时,我意识到它并没有在Javascript 端使用。因此,它的唯一用途是作为 flag 来判断应用程序是否从快捷方式启动并将其传递给 RNN 的 RNNReactRootViewCreator 似乎没有必要。

    消除了集成变得非常简单的必要性,我们只需将launchedFromShortcut = [launchOptions objectForKey:UIApplicationLaunchOptionsUserActivityDictionaryKey] != nil; 添加到didFinishLaunchingWithOptions 并在continueUserActivity 上检查它的值。

    AppDelegate.m 如下所示:

    
    #import "AppDelegate.h"
    #import <React/RCTBridge.h>
    #import <React/RCTBundleURLProvider.h>
    #import <ReactNativeNavigation/ReactNativeNavigation.h>
    #import <RNSiriShortcuts/RNSiriShortcuts-Swift.h>
    
    @implementation AppDelegate
    BOOL launchedFromShortcut;
    
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    
      launchedFromShortcut = [launchOptions objectForKey:UIApplicationLaunchOptionsUserActivityDictionaryKey] != nil;
    [ReactNativeNavigation bootstrap:jsCodeLocation launchOptions:launchOptions];
    
        //    
        // Regular RNN bootstrap code omitted for brevity sake
        //    
    
      return YES;
    }
    
    
    - (BOOL)application:(UIApplication *)application
    continueUserActivity:(NSUserActivity *)userActivity
     restorationHandler:(void (^)(NSArray<id<UIUserActivityRestoring>> *restorableObjects))restorationHandler
    {
    
        if (launchedFromShortcut) {
            ShortcutsModule.initialUserActivity = userActivity;
            launchedFromShortcut = NO;
        }
    
        [ShortcutsModule onShortcutReceivedWithUserActivity:userActivity];
        return YES;
    }
    
    @end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-18
      • 1970-01-01
      • 2020-11-25
      相关资源
      最近更新 更多