Skip to content

Commit af9f097

Browse files
committed
Merge pull request #546 from tjwudi/patch-2
Add guide for using React Native in existing iOS App
2 parents 75f6d2b + 35bea5f commit af9f097

File tree

4 files changed

+142
-1
lines changed

4 files changed

+142
-1
lines changed

docs/EmbeddedApp.md

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
---
2+
id: embedded-app
3+
title: Use React Native in Existing iOS App
4+
layout: docs
5+
category: Guides
6+
permalink: docs/embeded-app.html
7+
next: activityindicatorios
8+
---
9+
10+
## Install React Native Using Cocoapods
11+
12+
[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).
13+
14+
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.
15+
16+
```
17+
pod 'React'
18+
pod 'React/RCTText'
19+
# Add any subspecs you want to use in your project
20+
```
21+
22+
Remember to install all subspecs you need. The `<Text>` element cannot be used without `pod 'React/RCTText'`.
23+
24+
Then install pods via shell
25+
26+
```
27+
$ pod install --verbose
28+
```
29+
30+
The installation process also requires [Node.js](http://nodejs.org).
31+
32+
## Create Your React Native App
33+
34+
First, enter React Native's pod root directory and create **index.ios.js** inside a directory `ReactComponent`.
35+
36+
```
37+
$ cd Pods/React
38+
$ mkdir ReactComponent
39+
$ touch index.ios.js
40+
```
41+
42+
Copy & paste following starter code for **index.ios.js**.
43+
44+
```
45+
var React = require('react-native');
46+
47+
var {
48+
Text,
49+
View
50+
} = React;
51+
52+
var styles = React.StyleSheet.create({
53+
container: {
54+
flex: 1,
55+
backgroundColor: 'red'
56+
}
57+
});
58+
59+
class SimpleApp extends React.Component {
60+
render() {
61+
return <View style={styles.container}>
62+
<Text>This is a simple application.</Text>
63+
</View>;
64+
}
65+
}
66+
67+
React.AppRegistry.registerComponent('SimpleApp', () => SimpleApp);
68+
```
69+
70+
`SimpleApp` will be your **module name**, which will be used later on.
71+
72+
## Add Container View To Your App
73+
74+
You should now add container view for React Native component. It can be any **UIView** in your app.
75+
76+
![Container view example](/react-native/img/EmbeddedAppContainerViewExample.png)
77+
78+
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 :)).
79+
80+
```
81+
// ReactView.h
82+
83+
#import <UIKit/UIKit.h>
84+
@interface ReactView : UIView
85+
@end
86+
```
87+
88+
Don't forget to add an outlet for it.
89+
90+
```
91+
// ViewController.m
92+
93+
@interface ViewController ()
94+
@property (weak, nonatomic) IBOutlet ReactView *reactView;
95+
@end
96+
```
97+
98+
Here I disabled **AutoLayout** for simplicity. In real production world, you should turn on AutoLayout and setup constraints by yourself.
99+
100+
## Add RCTRootView To Container View
101+
102+
Ready for the most interesting part? Now we shall create the **RCTRootView**, where your React Native app lives in.
103+
104+
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.
105+
106+
```
107+
NSString *urlString = @"http://localhost:8081/index.ios.bundle";
108+
NSURL *jsCodeLocation = [NSURL URLWithString:urlString];
109+
RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
110+
moduleName: @"SimpleApp"
111+
launchOptions:nil];
112+
```
113+
114+
Then add it as a subview of the **ReactView**.
115+
116+
```
117+
[self addSubview:rootView];
118+
rootView.frame = self.bounds;
119+
```
120+
121+
## Start Development Server
122+
123+
In root directory, we need to start React Native development server.
124+
125+
```
126+
$ ./Pods/React/packager/packager.sh --root ./ReactComponents
127+
```
128+
129+
`--root` indicates the root of your React Native apps. Here we just have one **index.ios.js**. React Native development server will use packager to create a **index.ios.bundle**. Which can be access via `http://localhost:8081/index.ios.bundle`.
130+
131+
## Compile And Run
132+
133+
Now compile and run your app. You shall now see your React Native app running inside of the **ReactView**.
134+
135+
![Example](/react-native/img/EmbeddedAppExample.png)
136+
137+
## Conclusion
138+
139+
So under the hood, when **RCTRootView** is initialized, it will try to download, parse and run the bundle file from React Native development server. All you need to do is to implement your own container view, add **RCTRootView** as its subclass. And then serve the bundle using React Native development server. Then, bravo!
140+
141+
You can checkout full source code of sample application [here](https://github.com/tjwudi/EmbededReactNativeExample).

docs/Testing.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: Testing
44
layout: docs
55
category: Guides
66
permalink: docs/testing.html
7-
next: activityindicatorios
7+
next: embedded-app
88
---
99

1010
## Running Tests and Contributing
12.4 KB
Loading
14.3 KB
Loading

0 commit comments

Comments
 (0)