2015-03-31 14:49:52 +00:00
---
2015-03-31 15:20:26 +00:00
id: embedded-app
2015-03-31 16:46:45 +00:00
title: Integration with Existing App
2015-03-31 14:49:52 +00:00
layout: docs
category: Guides
permalink: docs/embeded-app.html
next: activityindicatorios
---
2015-03-31 21:40:59 +00:00
Since React makes no assumptions about the rest of your technology stack – it’ s commonly noted as simply the `V` in `MVC` – it’ s easily embeddable within an existing non-React Native app. In fact, it integrates with other best practice community tools like [CocoaPods ](http://cocoapods.org/ ).
2015-03-31 20:19:45 +00:00
## Requirements
2015-03-31 21:40:59 +00:00
- [CocoaPods ](http://cocoapods.org/ ) – `gem install cocoapods`
2015-03-31 20:19:45 +00:00
- [Node.js ](http://nodejs.org ) – `brew install node`
2015-03-31 21:40:59 +00:00
## Install React Native Using CocoaPods
2015-03-31 14:49:52 +00:00
2015-03-31 20:14:45 +00:00
[CocoaPods ](http://cocoapods.org/ ) is a package management tool for iOS/Mac development. We need to use it to download React Native. If you haven't install CocoaPods yet, checkout [this tutorial ](http://guides.cocoapods.org/using/getting-started.html ).
2015-03-31 14:49:52 +00:00
2015-03-31 20:14:45 +00:00
When you are ready to work with CocoaPods, add the following line to `Podfile` . If you don't have one, then create it under the root directory of your project.
2015-03-31 14:49:52 +00:00
2015-03-31 15:01:14 +00:00
```
pod 'React'
2015-03-31 16:46:45 +00:00
pod 'React/RCTText'
2015-03-31 15:01:14 +00:00
# Add any subspecs you want to use in your project
```
2015-03-31 14:49:52 +00:00
Remember to install all subspecs you need. The `<Text>` element cannot be used without `pod 'React/RCTText'` .
2015-03-31 16:46:45 +00:00
2015-03-31 20:20:42 +00:00
Then install your pods:
2015-03-31 14:49:52 +00:00
2015-03-31 15:01:14 +00:00
```
2015-03-31 20:20:42 +00:00
$ pod install
2015-03-31 15:01:14 +00:00
```
2015-03-31 16:46:45 +00:00
2015-03-31 14:49:52 +00:00
## Create Your React Native App
2015-03-31 20:25:08 +00:00
There are two pieces you’ ll need to set up:
1. The root JavaScript file that will contain your actual React Native app and other components
- Wrapper Objective-C code that will load up your script and create a `RCTRootView` to display and manage your React Native components
2015-03-31 21:08:37 +00:00
First, create a directory for your app’ s React code and create a simple `index.ios.js` file:
2015-03-31 14:49:52 +00:00
2015-03-31 15:01:14 +00:00
```
$ mkdir ReactComponent
$ touch index.ios.js
```
2015-03-31 16:46:45 +00:00
2015-03-31 21:09:19 +00:00
Copy & paste following starter code for `index.ios.js` – it’ s a barebones React Native app:
2015-03-31 14:49:52 +00:00
2015-03-31 15:01:14 +00:00
```
2015-03-31 20:40:30 +00:00
'use strict';
2015-03-31 15:01:14 +00:00
2015-03-31 20:40:30 +00:00
var React = require('react-native');
2015-03-31 15:01:14 +00:00
var {
Text,
View
} = React;
var styles = React.StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'red'
}
});
class SimpleApp extends React.Component {
render() {
2015-03-31 21:09:19 +00:00
return (
< View style = {styles.container} >
2015-03-31 15:01:14 +00:00
< Text > This is a simple application.< / Text >
2015-03-31 21:09:19 +00:00
< / View >
)
2015-03-31 15:01:14 +00:00
}
}
React.AppRegistry.registerComponent('SimpleApp', () => SimpleApp);
```
2015-03-31 16:46:45 +00:00
2015-03-31 14:49:52 +00:00
`SimpleApp` will be your **module name** , which will be used later on.
## Add Container View To Your App
2015-03-31 21:41:59 +00:00
You should now add a container view for the React Native component. It can be any `UIView` in your app.
2015-03-31 14:49:52 +00:00
2015-04-03 01:09:07 +00:00
![Container view example ](/react-native/img/EmbeddedAppContainerViewExample.png )
2015-03-31 14:49:52 +00:00
2015-03-31 21:11:26 +00:00
However, let's subclass `UIView` for the sake of clean code. Let's name it `ReactView` . Open up `Yourproject.xcworkspace` and create a new class `ReactView` (You can name it whatever you like :)).
2015-03-31 14:49:52 +00:00
2015-03-31 15:01:14 +00:00
```
// ReactView.h
2015-03-31 14:49:52 +00:00
2015-03-31 15:01:14 +00:00
#import <UIKit/UIKit.h>
@interface ReactView : UIView
@end
```
2015-03-31 14:49:52 +00:00
2015-03-31 21:17:44 +00:00
In a view controller that wants to manage this view, go ahead and add an outlet and wire it up:
2015-03-31 16:46:45 +00:00
2015-03-31 15:01:14 +00:00
```
// ViewController.m
@interface ViewController ()
@property (weak, nonatomic) IBOutlet ReactView *reactView;
@end
```
2015-03-31 16:46:45 +00:00
2015-03-31 14:49:52 +00:00
Here I disabled **AutoLayout** for simplicity. In real production world, you should turn on AutoLayout and setup constraints by yourself.
## Add RCTRootView To Container View
2015-03-31 21:11:26 +00:00
Ready for the most interesting part? Now we shall create the `RCTRootView` , where your React Native app lives in.
2015-03-31 14:49:52 +00:00
2015-03-31 21:11:26 +00:00
In `ReactView.m` , we need to first initiate `RCTRootView` with the URI of your `index.ios.bundle` . `index.ios.bundle` will be created by packager and served by React Native server, which will be discussed later on.
2015-03-31 14:49:52 +00:00
2015-03-31 15:01:14 +00:00
```
2015-03-31 21:21:54 +00:00
NSURL *jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.ios.bundle"];
// For production use, this `NSURL` could instead point to a pre-bundled file on disk:
//
// NSURL *jsCodeLocation = [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
//
// To generate that file, run the curl command and add the output to your main Xcode build target:
//
// curl http://localhost:8081/index.ios.bundle -o main.jsbundle
2015-03-31 15:01:14 +00:00
RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
moduleName: @"SimpleApp"
launchOptions:nil];
```
2015-03-31 21:11:26 +00:00
Then add it as a subview of the `ReactView` .
2015-03-31 14:49:52 +00:00
2015-03-31 15:01:14 +00:00
```
[self addSubview:rootView];
rootView.frame = self.bounds;
```
2015-03-31 14:49:52 +00:00
## Start Development Server
2015-03-31 16:46:45 +00:00
In root directory, we need to start React Native development server.
2015-03-31 14:49:52 +00:00
2015-03-31 15:01:14 +00:00
```
2015-03-31 21:08:37 +00:00
(JS_DIR=`pwd`/ReactComponent; cd Pods/React; npm run start -- --root $JS_DIR)
2015-03-31 15:01:14 +00:00
```
2015-03-31 21:08:37 +00:00
This command will start up a React Native development server within our CocoaPods dependency to build our bundled script. The `--root` option indicates the root of your React Native apps – this will be our `ReactComponents` directory containing the single `index.ios.js` file. This running server will package up the `index.ios.bundle` file accessible via `http://localhost:8081/index.ios.bundle` .
2015-03-31 14:49:52 +00:00
## Compile And Run
2015-03-31 21:11:26 +00:00
Now compile and run your app. You shall now see your React Native app running inside of the `ReactView` .
2015-03-31 14:49:52 +00:00
2015-04-03 01:09:07 +00:00
![Example ](/react-native/img/EmbeddedAppExample.png )
2015-03-31 14:49:52 +00:00
2015-03-31 21:11:58 +00:00
Live reload works from the simulator, too! You’ ve got a simple React component totally encapsulated behind an Objective-C `UIView` subclass.
2015-03-31 14:49:52 +00:00
## Conclusion
2015-03-31 21:24:26 +00:00
So under the hood, when `RCTRootView` is initialized, it will try to download, parse and run the bundle file from React Native development server. This means all you need to do is to implement your own container view or view controller for the `RCTRootView` – the `RCTRootView` ingests your bundled JS and renders your React components. Bravo!
2015-03-31 14:49:52 +00:00
2015-03-31 21:24:26 +00:00
You can checkout full source code of a sample application [here ](https://github.com/tjwudi/EmbededReactNativeExample ).