如何将iOS React Native模板转换为Swift?

Ste*_*eve 4 ios swift react-native

我一直在阅读有关react native的内容,并决定尝试一下,我在Swift中相当有经验,但从未尝试过使用目标c,因此我想将模板项目转换为使用AppDelegate.swift

我遵循了这个公认的答案中的解决方案:纯粹在Swift中使用React Native应用程序,但是当我运行项目时,我所拥有的只是一个黑屏

有谁知道我如何将模板转换为Swift?我可以直接从React Native调用快速代码而无需任何桥头吗?

我正在运行React Native 0.52 btw

Ste*_*eve 6

因此,经过几次尝试,我发现了如何使其与React Native 0.52一起使用

1-删除AppDelegate.m,AppDelegate.h和main.m

2-创建一个新的AppDelegate.swift,当它询问您是否要创建一个Objective-C桥头时,只需说“是”。

3-将以下内容复制到您的Bridging-Header.h文件中:

#import <React/RCTBridgeModule.h>
#import <React/RCTBridge.h>
#import <React/RCTEventDispatcher.h>
#import <React/RCTRootView.h>
#import <React/RCTUtils.h>
#import <React/RCTConvert.h>
#import <React/RCTBundleURLProvider.h>
Run Code Online (Sandbox Code Playgroud)

4-在您的AppDelegate.swift文件中使用以下代码并编辑项目名称:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
  var window: UIWindow?
  var bridge: RCTBridge!

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    let jsCodeLocation: URL

    jsCodeLocation = RCTBundleURLProvider.sharedSettings().jsBundleURL(forBundleRoot: "index", fallbackResource:nil)
    let rootView = RCTRootView(bundleURL: jsCodeLocation, moduleName: "YOUR-PROJECT-NAME", initialProperties: nil, launchOptions: launchOptions)
    let rootViewController = UIViewController()
    rootViewController.view = rootView

    self.window = UIWindow(frame: UIScreen.main.bounds)
    self.window?.rootViewController = rootViewController
    self.window?.makeKeyAndVisible()

    return true
  }
}
Run Code Online (Sandbox Code Playgroud)

5-不要忘记用您的实际项目名称替换“ YOUR-PROJECT-NAME”

6-Run etvoilà!快乐的编码:)