如何将 iOS React Native 模板转换为 swift? [英] How to convert iOS react native template to swift?
问题描述
我一直在阅读有关 react native 并决定尝试一下,我在 Swift 方面相当有经验,但从未尝试过 Objective c,所以我想将模板项目转换为使用 AppDelegate.swift
I have been reading about react native and decided to give it a try, i am fairly experienced in Swift but never tried out objective c so i would like to convert the template project to use AppDelegate.swift
我遵循了这个已接受答案中的解决方案:React Native app purely in Swift,但是当我运行项目时,我只有黑屏
I followed the solution in this accepted answer: React Native app purely in Swift, but all i have is a black screen when i run the project
有谁知道如何将模板转换为 Swift?我可以直接从 React Native 调用 swift 代码而不需要任何桥头吗?
Does anyone knows how can i convert the template to Swift? Can i call swift code directly from React Native without any bridge header?
顺便说一下,我正在运行 React Native 0.52
I am running React Native 0.52 btw
推荐答案
所以经过多次尝试后,我发现了如何让它与 React Native 0.52 一起工作
So after several tries i found out how to make it work with React Native 0.52
1 - 删除 AppDelegate.m、AppDelegate.h 和 main.m
1 - Delete AppDelegate.m, AppDelegate.h and main.m
2 - 创建一个新的 AppDelegate.swift,当它询问您是否要创建一个 Objective-C 桥头时,只需说是"
2 - Create a new AppDelegate.swift and when it asks if you would like to create an Objective-C bridge header just say "Yes"
3 - 将以下内容复制到 Bridging-Header.h 文件中:
3 - Copy the following to your Bridging-Header.h file:
#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>
4 - 在 AppDelegate.swift 文件中使用以下代码并编辑项目名称:
4 - Use the following code in your AppDelegate.swift file and edit the project name:
@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
}
}
5 - 不要忘记用您的实际项目名称替换YOUR-PROJECT-NAME"
5 - Don´t forget to replace "YOUR-PROJECT-NAME" by your actual project name
6 - 奔跑吧!快乐编码:)
6 - Run et voilà! Happy coding :)
这篇关于如何将 iOS React Native 模板转换为 swift?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!