Last active
January 17, 2022 11:01
-
-
Save kyo504/1c1942d2343f9b45f9265aef8c6dac0a to your computer and use it in GitHub Desktop.
Convert react-native iOS template to swift?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. Delete folling existing files | |
- AppDelegate.m | |
- AppDelegate.h | |
- main.m | |
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. Copy the following content to your Bridging-Header.h file | |
``` | |
#import <React/RCTRootView.h> | |
#import <React/RCTBundleURLProvider.h> | |
#import <FlipperKit/FlipperClient.h> | |
#import <FlipperKitLayoutPlugin/FlipperKitLayoutPlugin.h> | |
#import <FlipperKitUserDefaultsPlugin/FKUserDefaultsPlugin.h> | |
#import <FlipperKitNetworkPlugin/FlipperKitNetworkPlugin.h> | |
#import <SKIOSNetworkPlugin/SKIOSNetworkAdapter.h> | |
#import <FlipperKitReactPlugin/FlipperKitReactPlugin.h> | |
``` | |
4. Use the following code in your AppDelegate.swift file and edit the project name: | |
``` | |
import UIKit | |
#if DEBUG | |
import FlipperKit | |
#endif | |
@UIApplicationMain | |
class AppDelegate: UIResponder, UIApplicationDelegate { | |
var window: UIWindow? | |
private func initializeFlipper(with application: UIApplication) { | |
let client = FlipperClient.shared() | |
client?.add(FKUserDefaultsPlugin(suiteName: nil)) | |
client?.add(FlipperKitReactPlugin()) | |
client?.add(FlipperKitNetworkPlugin(networkAdapter: SKIOSNetworkAdapter())) | |
client?.start() | |
} | |
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool { | |
#if DEBUG | |
initializeFlipper(with: application) | |
#endif | |
let jsCodeLocation: URL | |
jsCodeLocation = RCTBundleURLProvider.sharedSettings().jsBundleURL(forBundleRoot: "index", fallbackResource:nil) | |
let rootView = RCTRootView(bundleURL: jsCodeLocation, moduleName: "Your-Project-Name", initialProperties: nil, launchOptions: launchOptions) | |
if #available(iOS 13.0, *) { | |
rootView.backgroundColor = UIColor.systemBackground | |
} else { | |
rootView.backgroundColor = UIColor.white | |
} | |
window = UIWindow(frame: UIScreen.main.bounds) | |
let rootViewController = UIViewController() | |
rootViewController.view = rootView | |
window?.rootViewController = rootViewController | |
window?.makeKeyAndVisible() | |
return true | |
} | |
func sourceURL(for bridge: RCTBridge?) -> URL? { | |
#if DEBUG | |
return RCTBundleURLProvider.sharedSettings().jsBundleURL(forBundleRoot: "index", fallbackResource: nil) | |
#else | |
return Bundle.main.url(forResource: "main", withExtension: "jsbundle") | |
#endif | |
} | |
} | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment