React Native应用程序纯粹在Swift中 [英] React Native app purely in Swift
本文介绍了React Native应用程序纯粹在Swift中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用以下命令创建了我的第一个本机应用程序。
I created my first react native app using the following command.
react-native init PropertyFinder
这在 Objective-C 中为ios创建了一个项目。问题是我不知道Objective-C但是我在Swift上工作。
This created a project for ios in Objective-C. The problem is I don't know Objective-C but I work on Swift.
这要点展示了如何将反应组件链接到快速项目。
但有没有办法用 Swift语言直接创建项目?
This gist shows how to link react-components to a swift project. But is there a way to directly create the project in Swift language?
推荐答案
这个优秀的帖子帮助我解决了这个问题。
This excellent post helped me solving the problem.
解决方案
- 添加桥接标题
#import "RCTBridgeModule.h"
#import "RCTBridge.h"
#import "RCTEventDispatcher.h"
#import "RCTRootView.h"
#import "RCTUtils.h"
#import "RCTConvert.h"
- 添加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
}
-
删除
AppDelegate.h
,AppDelegate.m
和主文件。
清理和构建。
这篇关于React Native应用程序纯粹在Swift中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文