首页 > 解决方案 > 通过 ios 上的 Action 扩展加载 React 本机时,Javascript 似乎没有运行

问题描述

这是来自我的由 Action 扩展启动的视图控制器:

- (void)loadView {
  [super loadView];


  NSURL *jsCodeLocation;
      jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];

  BOOL found = NO;

  // Find the item containing the results from the JavaScript preprocessing.
  for (NSExtensionItem *item in self.extensionContext.inputItems) {
    for (NSItemProvider *itemProvider in item.attachments) {
      if ([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypePropertyList]) {
        [itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypePropertyList options:nil completionHandler:^(NSDictionary *dictionary, NSError *error) {
          [[NSOperationQueue mainQueue] addOperationWithBlock:^{

            NSDictionary *initialProps = @{@"isActionExtension": @YES,  @"preprocessingResults": dictionary[NSExtensionJavaScriptPreprocessingResultsKey]};

            RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
                                                                moduleName:@"QCheckoutRN"
                                                         initialProperties:initialProps
                                                             launchOptions:nil];

            rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];


            NSLog(@"High Score Button Pressed");
            UIViewController *vc = [[UIViewController alloc] init];
            vc.view = rootView;
            [self presentViewController:vc animated:YES completion:nil];


          }];
        }];
        found = YES;
      }
      break;
    }
    if (found) {
      break;
    }
  }

  if (!found) {
    // We did not find anything
    [self doneWithResults:nil];
  }

它加载了使用 RN 生成的视图,我看到了 jsx 文件中的按钮,但没有触发任何生命周期方法,无法执行 alert() 并且按钮不会触发操作。

不知道如何调试它,因为 JS 运行时似乎死了,这很奇怪,因为它做了初始渲染。我是不是加载错了?它看起来类似于https://facebook.github.io/react-native/docs/integration-with-existing-apps但他们正在使用一个动作来调用视图,同时我立即加载它。我是 iOS 开发和 Objective-c 的菜鸟,所以如果有人能指出这个问题,它会帮助它运行。

标签: objective-creact-native

解决方案


推荐阅读