Skip to content

Instantly share code, notes, and snippets.

@barrault01
Last active December 31, 2018 13:37
Show Gist options
  • Select an option

  • Save barrault01/883b1532098f7f55ffda3594d785789a to your computer and use it in GitHub Desktop.

Select an option

Save barrault01/883b1532098f7f55ffda3594d785789a to your computer and use it in GitHub Desktop.
A UIViewController that present React Native application
import UIKit
import React
@objc(ReactViewController)
public class ReactViewController: UIViewController {
var moduleName: String = ""
var jsFileName: String = ""
var isDebug: Bool = false
var showNavigationBar: Bool = true
var bundle: Bundle = Bundle.main
var oldNavigationBarVisibility: Bool = false
var props: [String: Any] = [:]
static var callback: (() -> Void)?
override public func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
oldNavigationBarVisibility = self.navigationController?.isNavigationBarHidden ?? false
self.navigationController?.setNavigationBarHidden(!showNavigationBar, animated: animated)
}
override public func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationController?.setNavigationBarHidden(oldNavigationBarVisibility, animated: animated)
}
public convenience init(moduleName: String?,
jsFileName: String?,
bundle: Bundle?,
isDebug: Bool = false,
showNavigationBar: Bool?,
props: [String: Any]?,
callback: (() -> Void)? = nil) {
self.init()
if let name = moduleName {
self.moduleName = name
}
if let bundle = bundle {
self.bundle = bundle
}
if let name = jsFileName {
self.jsFileName = name
}
if let props = props {
self.props = props
}
if let showNavigationBar = showNavigationBar {
self.showNavigationBar = showNavigationBar
}
self.isDebug = isDebug
ReactViewController.callback = callback
}
@objc func dismissReactView() {
DispatchQueue.main.async {
ReactViewController.callback?()
}
}
}
extension ReactViewController: RCTBridgeDelegate {
public override func loadView() {
if (!isDebug) {
let bridge = RCTBridge.init(delegate: self, launchOptions: [:])
self.view = RCTRootView(bridge: bridge, moduleName: self.moduleName, initialProperties: self.props)
} else {
let url = RCTBundleURLProvider.sharedSettings().jsBundleURL(forBundleRoot: "index", fallbackResource: nil)
self.view = RCTRootView(bundleURL: url, moduleName: self.moduleName, initialProperties: self.props, launchOptions: nil)
}
}
public func sourceURL(for bridge: RCTBridge!) -> URL! {
return self.bundle.url(forResource: self.jsFileName, withExtension: "jsbundle")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment