- Create a project in XCode with the default settings
- iOS > Application > Single View Application
- Language: Swift
- Under project General settings, add ReactKit to Linked Framework and Libraries
- + > Add Other... and choose /path/to/react-native/ReactKit/ReactKit.xcodeproj
- Now ReactKit would have been imported. Link it by choosing it from the list.
- + > lib.ReactKit.a
- Under project Build Settings,
- Linking > Other Linker Flags : Add
-all_load
- Search Paths > Header Search Paths:
- Add
/path/to/react-native
- Enable
recursive
- Add
- Swift Compiler - Code Generation > Objective-C Bridging Header
- Set to
ProjectName/ProjectName-Bridging-Header.h
- We will create this file in later part
- Set to
- Linking > Other Linker Flags : Add
- Under project Info settings,
- Remove property Main storyboard file base name
- Delete file Main.storyboard from XCode and Confirm Remove Reference
Use this as the function body for didFinishLaunchingWithOptions
replacing
<AppFileName>.js
- Your JS entry point where you'll register the Application.<AppName>
- The name you use in<AppFileName>
inAppRegistry.registerComponent
to register the application.
// initialize the rootView to fetch JS from the dev server
let rootView = RCTRootView()
rootView.scriptURL = NSURL(string: "http://localhost:8081/<AppFileName>.includeRequire.runModule.bundle")
rootView.moduleName = "<AppName>"
// Initialize a Controller to use view as React View
let rootViewController = ViewController()
rootViewController.view = rootView
// Set window to use rootViewController
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.rootViewController = rootViewController
self.window?.makeKeyAndVisible()
return true
Add this override to ViewController if you don't want the top time-bar of ios to be visible in your Application.
override func prefersStatusBarHidden() -> Bool {
return true
}
This file imports react-native(Obj-C) classes to be used in your swift application. Create a header file named ProjectName-Bridging-Header.h
(in the same directory as AppDelegate.swift) and paste the contents generated by the following script
find /path/to/react-native/ReactKit -name "*.h" | awk -F'/' '{print "#import \""$NF"\""}'
The bare minimum stuff
var React = require('react-native');
var App = module.exports = React.createClass({ render() { /* ... */ });
React.AppRegistry.registerComponent('<AppName>', () => App);
APPDIR = Directory which contains '<AppFileName>.js'
NODE_MODULES_DIR = usually '$APPDIR/node_modules'
if you use other libraries in your app
cd /path/to/react-native
./packager/packager.sh --root $APPDIR --root $NODE_MODULES_DIR
- Note: DONOT use
$NODE_MODULES_DIR
if you don't have any node_modules
- Note: DONOT use
- Build the App from XCode and everything should work just fine.