ios - 纯粹在 Swift 中 React Native 应用程序

标签 ios swift react-native

我使用以下命令创建了我的第一个 React native 应用。

react-native init PropertyFinder

这为 Objective-C 中的 ios 创建了一个项目。问题是我不了解 Objective-C,但我从事 Swift 方面的工作。

gist展示了如何将 react-components 链接到一个 swift 项目。 但是有没有办法直接用Swift语言创建项目呢?

最佳答案

excellent post帮助我解决了问题。

解决方案

  1. Add Bridging Header
#import "RCTBridgeModule.h"
#import "RCTBridge.h"
#import "RCTEventDispatcher.h"
#import "RCTRootView.h"
#import "RCTUtils.h"
#import "RCTConvert.h"
  1. Add AppDelegate.Swift
var bridge: RCTBridge!

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    /**
     * Loading JavaScript code - uncomment the one you want.
     *
     * OPTION 1
     * Load from development server. Start the server from the repository root:
     *
     * $ npm start
     *
     * To run on device, change `localhost` to the IP address of your computer
     * (you can get this by typing `ifconfig` into the terminal and selecting the
     * `inet` value under `en0:`) and make sure your computer and iOS device are
     * on the same Wi-Fi network.
     */

    let jsCodeLocation = NSURL(string: "http://localhost:8081/index.ios.bundle?platform=ios&dev=true")

    /**
     * OPTION 2
     * Load from pre-bundled file on disk. The static bundle is automatically
     * generated by "Bundle React Native code and images" build step.
     */

    // jsCodeLocation = NSBundle.mainBundle().URLForResource("main", withExtension: "jsbundle")

    let rootView = RCTRootView(bundleURL:jsCodeLocation as URL!, moduleName: "PropertyFinder", initialProperties: nil, launchOptions:launchOptions)

    self.bridge = rootView?.bridge

    self.window = UIWindow(frame: UIScreen.main.bounds)
    let rootViewController = UIViewController()

    rootViewController.view = rootView

    self.window!.rootViewController = rootViewController;
    self.window!.makeKeyAndVisible()

    return true
  }
  1. 删除 AppDelegate.hAppDelegate.m 和主文件。

  2. 清理并构建您的项目。

关于ios - 纯粹在 Swift 中 React Native 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40081012/

相关文章:

ios - 如何防止我的表格的单元格滚动到我的表格框架之外

ios - 如何在 Swift 中实现 didReceiveMemoryWarning?

json - Realm 和对象映射器 : Array of one item keeps stacking in my Realm database for every request

ios - 如何在 SWIFT 中将词典添加到 Plist

ios - 跨平台移动工具(Ionic、React-Native)上的 Twilio

javascript - React-Native 的 TextInput 组件中的 SetTimeout

ios - 在没有插入闪电电缆的情况下,如何在真实的 iOS 设备上查看实时控制台日志?

iphone - 如何对自动生成的 iOS 应用进行代码设计?

ios - 生成的PluginRegistrant.h :8:9: 'Flutter/Flutter.h' file not found

react-native - React Navigation v5 setOptions 不起作用