You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Installation process has created a new `/node_modules` folder. This folder stores all the JavaScript dependencies required to build your project.
82
+
The installation process has created a new `/node_modules` folder. This folder stores all the JavaScript dependencies required to build your project.
83
83
84
84
Add `node_modules/` to your `.gitignore` file.
85
85
@@ -97,7 +97,7 @@ brew install cocoapods
97
97
98
98
## Adding React Native to your app
99
99
100
-
Assume the [app for integration](https://github.com/JoelMarcey/iOS-2048) is a [2048](https://en.wikipedia.org/wiki/2048_%28video_game%29) game. Here is what the main menu of the native application looks like without React Native.
100
+
Assume the app is a [2048](https://en.wikipedia.org/wiki/2048_%28video_game%29) game. Here is what the main menu of the native application looks like without React Native.
@@ -109,66 +109,33 @@ Install the Command Line Tools. Choose "Preferences..." in the Xcode menu. Go to
109
109
110
110
### Configuring CocoaPods dependencies
111
111
112
-
Before you integrate React Native into your application, you will want to decide what parts of the React Native framework you would like to integrate. We will use CocoaPods to specify which of these "subspecs" your app will depend on.
113
-
114
-
The list of supported `subspec`s is available in [`/node_modules/react-native/React.podspec`](https://github.com/facebook/react-native/blob/master/React.podspec). They are generally named by functionality. For example, you will generally always want the `Core``subspec`. That will get you the `AppRegistry`, `StyleSheet`, `View` and other core React Native libraries. If you want to add the React Native `Text` library (e.g., for `<Text>` elements), then you will need the `RCTText``subspec`. If you want the `Image` library (e.g., for `<Image>` elements), then you will need the `RCTImage``subspec`.
115
-
116
-
You can specify which `subspec`s your app will depend on in a `Podfile` file. The easiest way to create a `Podfile` is by running the CocoaPods `init` command in the `/ios` subfolder of your project:
112
+
Add React Native targets to your project by editing your CocoaPods `Podfile`. If you don't have a `Podfile`, the easiest way to create one is by running the CocoaPods `init` command in the `/ios` subfolder of your project:
117
113
118
114
```shell
119
115
pod init
120
116
```
121
117
122
118
The `Podfile` will contain a boilerplate setup that you will tweak for your integration purposes.
123
119
124
-
> The `Podfile` version changes depending on your version of `react-native`. Refer to https://react-native-community.github.io/upgrade-helper/ for the specific version of `Podfile` you should be using.
120
+
Inside the `target` section of your `Podfile`, call `use_react_native!`, like this:
125
121
126
-
Ultimately, your `Podfile` should look something similar to this:
> Note that `RCTRootView initWithURL` starts up a new JSC VM. To save resources and simplify the communication between RN views in different parts of your native app, you can have multiple views powered by React Native that are associated with a single JS runtime. To do that, instead of using `[RCTRootView alloc] initWithURL`, use [`RCTBridge initWithBundleURL`](https://github.com/facebook/react-native/blob/master/React/Base/RCTBridge.h#L93) to create a bridge and then use `RCTRootView initWithBridge`.
275
+
> Note that `-[RCTRootView initWithBundleURL:...]` starts up a new JavaScript VM. To save resources and simplify the communication between React Native views in different parts of your native app, you can have multiple views powered by React Native that are associated with a single JS runtime. To do that, instead of using `-[RCTRootView initWithBundleURL:...]`, use [`-[RCTBridge initWithBundleURL:...]`](https://github.com/facebook/react-native/blob/master/React/Base/RCTBridge.h#L93) to create a bridge, and then use `-[RCTRootView initWithBridge:...]`.
315
276
316
277
> When moving your app to production, the `NSURL` can point to a pre-bundled file on disk via something like `[[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];`. You can use the `react-native-xcode.sh` script in `node_modules/react-native/scripts/` to generate that pre-bundled file.
Copy file name to clipboardExpand all lines: docs/_integration-with-exisiting-apps-swift.md
+84-68Lines changed: 84 additions & 68 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,3 +1,5 @@
1
+
import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem'; import constants from '@site/core/TabsConstants';
2
+
1
3
## Key Concepts
2
4
3
5
The keys to integrating React Native components into your iOS application are to:
@@ -37,21 +39,47 @@ Next, make sure you have [installed the yarn package manager](https://yarnpkg.co
37
39
38
40
Install the `react` and `react-native` packages. Open a terminal or command prompt, then navigate to the directory with your `package.json` file and run:
Yarn has created a new `/node_modules` folder. This folder stores all the JavaScript dependencies required to build your project.
72
+
</TabItem>
73
+
<TabItemvalue="yarn">
74
+
75
+
```shell
76
+
yarn add react@version_printed_above
77
+
```
78
+
79
+
</TabItem>
80
+
</Tabs>
81
+
82
+
The installation process has created a new `/node_modules` folder. This folder stores all the JavaScript dependencies required to build your project.
55
83
56
84
Add `node_modules/` to your `.gitignore` file.
57
85
@@ -62,14 +90,14 @@ Add `node_modules/` to your `.gitignore` file.
62
90
We recommend installing CocoaPods using [Homebrew](http://brew.sh/).
63
91
64
92
```shell
65
-
$ brew install cocoapods
93
+
brew install cocoapods
66
94
```
67
95
68
96
> It is technically possible not to use CocoaPods, but that would require manual library and linker additions that would overly complicate this process.
69
97
70
98
## Adding React Native to your app
71
99
72
-
Assume the [app for integration](https://github.com/JoelMarcey/swift-2048) is a [2048](https://en.wikipedia.org/wiki/2048_%28video_game%29) game. Here is what the main menu of the native application looks like without React Native.
100
+
Assume the app is a [2048](https://en.wikipedia.org/wiki/2048_%28video_game%29) game. Here is what the main menu of the native application looks like without React Native.
@@ -81,58 +109,33 @@ Install the Command Line Tools. Choose "Preferences..." in the Xcode menu. Go to
81
109
82
110
### Configuring CocoaPods dependencies
83
111
84
-
Before you integrate React Native into your application, you will want to decide what parts of the React Native framework you would like to integrate. We will use CocoaPods to specify which of these "subspecs" your app will depend on.
85
-
86
-
The list of supported `subspec`s is available in [`/node_modules/react-native/React.podspec`](https://github.com/facebook/react-native/blob/master/React.podspec). They are generally named by functionality. For example, you will generally always want the `Core``subspec`. That will get you the `AppRegistry`, `StyleSheet`, `View` and other core React Native libraries. If you want to add the React Native `Text` library (e.g., for `<Text>` elements), then you will need the `RCTText``subspec`. If you want the `Image` library (e.g., for `<Image>` elements), then you will need the `RCTImage``subspec`.
87
-
88
-
You can specify which `subspec`s your app will depend on in a `Podfile` file. The easiest way to create a `Podfile` is by running the CocoaPods `init` command in the `/ios` subfolder of your project:
112
+
Add React Native targets to your project by editing your CocoaPods `Podfile`. If you don't have a `Podfile`, the easiest way to create one is by running the CocoaPods `init` command in the `/ios` subfolder of your project:
89
113
90
114
```shell
91
-
$ pod init
115
+
pod init
92
116
```
93
117
94
118
The `Podfile` will contain a boilerplate setup that you will tweak for your integration purposes.
95
119
96
-
> The `Podfile` version changes depending on your version of `react-native`. Refer to https://react-native-community.github.io/upgrade-helper/ for the specific version of `Podfile` you should be using.
120
+
Inside the `target` section of your `Podfile`, call `use_react_native!`, like this:
97
121
98
-
Ultimately, your `Podfile` should look something similar to this:
# The target name is usually the name of your application.
126
+
target 'NumberTileGame'do
110
127
# Your 'node_modules' directory is probably in the root of your project,
111
-
# but if not, adjust the `:path` accordingly
112
-
pod 'React', :path => '../node_modules/react-native', :subspecs => [
113
-
'Core',
114
-
'CxxBridge', # Include this for RN >= 0.47
115
-
'DevSupport', # Include this to enable In-App Devmenu if RN >= 0.43
116
-
'RCTText',
117
-
'RCTNetwork',
118
-
'RCTWebSocket', # needed for debugging
119
-
# Add any other subspecs you want to use in your project
120
-
]
121
-
# Explicitly include Yoga if you are using RN >= 0.42.0
122
-
pod "Yoga", :path => "../node_modules/react-native/ReactCommon/yoga"
123
-
124
-
# Third party deps podspec link
125
-
pod 'DoubleConversion', :podspec => '../node_modules/react-native/third-party-podspecs/DoubleConversion.podspec'
126
-
pod 'glog', :podspec => '../node_modules/react-native/third-party-podspecs/glog.podspec'
127
-
pod 'Folly', :podspec => '../node_modules/react-native/third-party-podspecs/Folly.podspec'
128
-
128
+
# but if not, adjust the `:path` argument accordingly.
129
+
use_react_native!(
130
+
:path => "../node_modules/react-native"
131
+
)
129
132
end
130
133
```
131
134
132
-
After you have created your `Podfile`, you are ready to install the React Native pod.
135
+
After you have created your `Podfile`, you are ready to install the React Native pods.
133
136
134
137
```shell
135
-
$ pod install
138
+
pod install
136
139
```
137
140
138
141
You should see output such as:
@@ -241,40 +244,40 @@ When you build a React Native application, you use the [Metro bundler][metro] to
241
244
242
245
We will, for debugging purposes, log that the event handler was invoked. Then, we will create a string with the location of our React Native code that exists inside the `index.bundle`. Finally, we will create the main `RCTRootView`. Notice how we provide `RNHighScores` as the `moduleName` that we created [above](#the-react-native-component) when writing the code for our React Native component.
243
246
244
-
First `import` the `React`library.
247
+
First `import` the `React`module.
245
248
246
-
```jsx
249
+
```swift
247
250
importReact
248
251
```
249
252
250
253
> The `initialProperties` are here for illustration purposes so we have some data for our high score screen. In our React Native component, we will use `this.props` to get access to that data.
let jsCodeLocation =URL(string: "http://localhost:8081/index.bundle?platform=ios")
258
+
let mockData = [
259
+
"scores": [
260
+
["name":"Alex", "value":"42"],
261
+
["name":"Joel", "value":"10"],
262
+
]
263
+
]
264
+
265
+
let rootView =RCTRootView(
266
+
bundleURL: jsCodeLocation,
267
+
moduleName: "RNHighScores",
268
+
initialProperties: mockData,
269
+
launchOptions: nil
270
+
)
271
+
272
+
let vc =UIViewController()
273
+
vc.view= rootView
274
+
self.present(vc, animated: true)
272
275
}
273
276
```
274
277
275
-
> Note that `RCTRootViewbundleURL` starts up a new JSC VM. To save resources and simplify the communication between RN views in different parts of your native app, you can have multiple views powered by React Native that are associated with a single JS runtime. To do that, instead of using `RCTRootViewbundleURL`, use [`RCTBridge initWithBundleURL`](https://github.com/facebook/react-native/blob/master/React/Base/RCTBridge.h#L89) to create a bridge and then use `RCTRootView initWithBridge`.
278
+
> Note that `RCTRootView(bundleURL:...)` starts up a new JavaScript VM. To save resources and simplify the communication between React Native views in different parts of your native app, you can have multiple views powered by React Native that are associated with a single JS runtime. To do that, instead of using `RCTRootView(bundleURL:...)`, use [`RCTBridge(bundleURL:...)`](https://github.com/facebook/react-native/blob/master/React/Base/RCTBridge.h#L89) to create a bridge, and then use `RCTRootView(bridge:...)`.
276
279
277
-
> When moving your app to production, the `NSURL` can point to a pre-bundled file on disk via something like `let mainBundle = NSBundle(URLForResource: "main" withExtension:"jsbundle")`. You can use the `react-native-xcode.sh` script in `node_modules/react-native/scripts/` to generate that pre-bundled file.
280
+
> When moving your app to production, the `URL` can point to a pre-bundled file on disk via something like `Bundle.main.url(forResource: "main", withExtension:"jsbundle")`. You can use the `react-native-xcode.sh` script in `node_modules/react-native/scripts/` to generate that pre-bundled file.
278
281
279
282
##### 3. Wire Up
280
283
@@ -312,10 +315,23 @@ Apple has blocked implicit cleartext HTTP resource loading. So we need to add th
312
315
313
316
To run your app, you need to first start the development server. To do this, run the following command in the root directory of your React Native project:
If you are using Xcode or your favorite editor, build and run your native iOS application as normal. Alternatively, you can run the app from the command line using:
0 commit comments