我一直在阅读有关react native的内容,并决定尝试一下,我在Swift中相当有经验,但从未尝试过使用目标c,所以我想将模板项目转换为使用AppDelegate.swift
我遵循了这个公认的答案中的解决方案:纯粹在Swift中使用React Native应用程序,但是当我运行项目时,我所拥有的只是一个黑屏
有谁知道我如何将模板转换为Swift?我可以在没有任何桥头的情况下直接从React Native调用Swift代码吗?
我正在运行React Native 0.52 btw
因此,经过几次尝试,我发现了如何使其与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>
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
}
}
5-不要忘记用您的实际项目名称替换“ YOUR-PROJECT-NAME”
6-跑步,瞧!快乐的编码:)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句