Skip to content

Commit a11fb75

Browse files
committed
Update Podspec directions to use use_react_native!
Also, increase consistency between the Objective-C and Swift versions. Fixes #2951.
1 parent 7ab3a75 commit a11fb75

File tree

2 files changed

+110
-133
lines changed

2 files changed

+110
-133
lines changed

docs/_integration-with-exisiting-apps-objc.md

Lines changed: 26 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ yarn add react-native
5656
</TabItem>
5757
</Tabs>
5858

59-
This will print a message similar to the following (scroll up in the yarn output to see it):
59+
This will print a message similar to the following (scroll up in the output to see it):
6060

6161
> warning "[email protected]" has unmet peer dependency "[email protected]".
6262
@@ -79,7 +79,7 @@ yarn add react@version_printed_above
7979
</TabItem>
8080
</Tabs>
8181

82-
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.
8383

8484
Add `node_modules/` to your `.gitignore` file.
8585

@@ -97,7 +97,7 @@ brew install cocoapods
9797
9898
## Adding React Native to your app
9999

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.
101101

102102
![Before RN Integration](/docs/assets/react-native-existing-app-integration-ios-before.png)
103103

@@ -109,66 +109,33 @@ Install the Command Line Tools. Choose "Preferences..." in the Xcode menu. Go to
109109

110110
### Configuring CocoaPods dependencies
111111

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:
117113

118114
```shell
119115
pod init
120116
```
121117

122118
The `Podfile` will contain a boilerplate setup that you will tweak for your integration purposes.
123119

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:
125121

126-
Ultimately, your `Podfile` should look something similar to this:
122+
```ruby
123+
require_relative '../node_modules/react-native/scripts/react_native_pods'
127124

128-
```
129-
# The target name is most likely the name of your project.
125+
# The target name is usually the name of your application.
130126
target 'NumberTileGame' do
131-
132127
# Your 'node_modules' directory is probably in the root of your project,
133-
# but if not, adjust the `:path` accordingly
134-
pod 'FBLazyVector', :path => "../node_modules/react-native/Libraries/FBLazyVector"
135-
pod 'FBReactNativeSpec', :path => "../node_modules/react-native/Libraries/FBReactNativeSpec"
136-
pod 'RCTRequired', :path => "../node_modules/react-native/Libraries/RCTRequired"
137-
pod 'RCTTypeSafety', :path => "../node_modules/react-native/Libraries/TypeSafety"
138-
pod 'React', :path => '../node_modules/react-native/'
139-
pod 'React-Core', :path => '../node_modules/react-native/'
140-
pod 'React-CoreModules', :path => '../node_modules/react-native/React/CoreModules'
141-
pod 'React-Core/DevSupport', :path => '../node_modules/react-native/'
142-
pod 'React-RCTActionSheet', :path => '../node_modules/react-native/Libraries/ActionSheetIOS'
143-
pod 'React-RCTAnimation', :path => '../node_modules/react-native/Libraries/NativeAnimation'
144-
pod 'React-RCTBlob', :path => '../node_modules/react-native/Libraries/Blob'
145-
pod 'React-RCTImage', :path => '../node_modules/react-native/Libraries/Image'
146-
pod 'React-RCTLinking', :path => '../node_modules/react-native/Libraries/LinkingIOS'
147-
pod 'React-RCTNetwork', :path => '../node_modules/react-native/Libraries/Network'
148-
pod 'React-RCTSettings', :path => '../node_modules/react-native/Libraries/Settings'
149-
pod 'React-RCTText', :path => '../node_modules/react-native/Libraries/Text'
150-
pod 'React-RCTVibration', :path => '../node_modules/react-native/Libraries/Vibration'
151-
pod 'React-Core/RCTWebSocket', :path => '../node_modules/react-native/'
152-
153-
pod 'React-cxxreact', :path => '../node_modules/react-native/ReactCommon/cxxreact'
154-
pod 'React-jsi', :path => '../node_modules/react-native/ReactCommon/jsi'
155-
pod 'React-jsiexecutor', :path => '../node_modules/react-native/ReactCommon/jsiexecutor'
156-
pod 'React-jsinspector', :path => '../node_modules/react-native/ReactCommon/jsinspector'
157-
pod 'ReactCommon/callinvoker', :path => "../node_modules/react-native/ReactCommon"
158-
pod 'ReactCommon/turbomodule/core', :path => "../node_modules/react-native/ReactCommon"
159-
pod 'Yoga', :path => '../node_modules/react-native/ReactCommon/yoga'
160-
161-
pod 'DoubleConversion', :podspec => '../node_modules/react-native/third-party-podspecs/DoubleConversion.podspec'
162-
pod 'glog', :podspec => '../node_modules/react-native/third-party-podspecs/glog.podspec'
163-
pod 'Folly', :podspec => '../node_modules/react-native/third-party-podspecs/Folly.podspec'
164-
128+
# but if not, adjust the `:path` argument accordingly.
129+
use_react_native!(
130+
:path => "../node_modules/react-native"
131+
)
165132
end
166133
```
167134

168-
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.
169136

170137
```shell
171-
$ pod install
138+
pod install
172139
```
173140

174141
You should see output such as:
@@ -285,33 +252,27 @@ First `import` the `RCTRootView` header.
285252
286253
```objectivec
287254
- (IBAction)highScoreButtonPressed:(id)sender {
288-
NSLog(@"High Score Button Pressed");
289255
NSURL *jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.bundle?platform=ios"];
256+
NSDictionary *mockData = @{
257+
@"scores" : @[
258+
@{ @"name" : @"Alex", @"value": @"42" },
259+
@{ @"name" : @"Joel", @"value": @"10" },
260+
],
261+
};
290262

291263
RCTRootView *rootView =
292-
[[RCTRootView alloc] initWithBundleURL: jsCodeLocation
293-
moduleName: @"RNHighScores"
294-
initialProperties:
295-
@{
296-
@"scores" : @[
297-
@{
298-
@"name" : @"Alex",
299-
@"value": @"42"
300-
},
301-
@{
302-
@"name" : @"Joel",
303-
@"value": @"10"
304-
}
305-
]
306-
}
307-
launchOptions: nil];
264+
[[RCTRootView alloc] initWithBundleURL:jsCodeLocation
265+
moduleName:@"RNHighScores"
266+
initialProperties:mockData
267+
launchOptions:nil];
268+
308269
UIViewController *vc = [[UIViewController alloc] init];
309270
vc.view = rootView;
310271
[self presentViewController:vc animated:YES completion:nil];
311272
}
312273
```
313274

314-
> 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:...]`.
315276
316277
> 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.
317278

docs/_integration-with-exisiting-apps-swift.md

Lines changed: 84 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem'; import constants from '@site/core/TabsConstants';
2+
13
## Key Concepts
24

35
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
3739

3840
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:
3941

42+
<Tabs groupId="package-manager" defaultValue={constants.defaultPackageManager} values={constants.packageManagers}>
43+
<TabItem value="npm">
44+
45+
```shell
46+
npm install react-native
47+
```
48+
49+
</TabItem>
50+
<TabItem value="yarn">
51+
4052
```shell
41-
$ yarn add react-native
53+
yarn add react-native
4254
```
4355

44-
This will print a message similar to the following (scroll up in the yarn output to see it):
56+
</TabItem>
57+
</Tabs>
58+
59+
This will print a message similar to the following (scroll up in the output to see it):
4560

4661
> warning "[email protected]" has unmet peer dependency "[email protected]".
4762
4863
This is OK, it means we also need to install React:
4964

65+
<Tabs groupId="package-manager" defaultValue={constants.defaultPackageManager} values={constants.packageManagers}>
66+
<TabItem value="npm">
67+
5068
```shell
51-
$ yarn add react@version_printed_above
69+
npm install react@version_printed_above
5270
```
5371

54-
Yarn has created a new `/node_modules` folder. This folder stores all the JavaScript dependencies required to build your project.
72+
</TabItem>
73+
<TabItem value="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.
5583

5684
Add `node_modules/` to your `.gitignore` file.
5785

@@ -62,14 +90,14 @@ Add `node_modules/` to your `.gitignore` file.
6290
We recommend installing CocoaPods using [Homebrew](http://brew.sh/).
6391

6492
```shell
65-
$ brew install cocoapods
93+
brew install cocoapods
6694
```
6795

6896
> It is technically possible not to use CocoaPods, but that would require manual library and linker additions that would overly complicate this process.
6997
7098
## Adding React Native to your app
7199

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.
73101

74102
![Before RN Integration](/docs/assets/react-native-existing-app-integration-ios-before.png)
75103

@@ -81,58 +109,33 @@ Install the Command Line Tools. Choose "Preferences..." in the Xcode menu. Go to
81109

82110
### Configuring CocoaPods dependencies
83111

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:
89113

90114
```shell
91-
$ pod init
115+
pod init
92116
```
93117

94118
The `Podfile` will contain a boilerplate setup that you will tweak for your integration purposes.
95119

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:
97121

98-
Ultimately, your `Podfile` should look something similar to this:
99-
100-
```
101-
source 'https://github.com/CocoaPods/Specs.git'
102-
103-
# Required for Swift apps
104-
platform :ios, '8.0'
105-
use_frameworks!
106-
107-
# The target name is most likely the name of your project.
108-
target 'swift-2048' do
122+
```ruby
123+
require_relative '../node_modules/react-native/scripts/react_native_pods'
109124

125+
# The target name is usually the name of your application.
126+
target 'NumberTileGame' do
110127
# 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+
)
129132
end
130133
```
131134

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.
133136

134137
```shell
135-
$ pod install
138+
pod install
136139
```
137140

138141
You should see output such as:
@@ -241,40 +244,40 @@ When you build a React Native application, you use the [Metro bundler][metro] to
241244

242245
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.
243246

244-
First `import` the `React` library.
247+
First `import` the `React` module.
245248

246-
```jsx
249+
```swift
247250
import React
248251
```
249252

250253
> 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.
251254
252255
```swift
253-
@IBAction func highScoreButtonTapped(sender : UIButton) {
254-
NSLog("Hello")
255-
let jsCodeLocation = URL(string: "http://localhost:8081/index.bundle?platform=ios")
256-
let mockData:NSDictionary = ["scores":
257-
[
258-
["name":"Alex", "value":"42"],
259-
["name":"Joel", "value":"10"]
260-
]
261-
]
262-
263-
let rootView = RCTRootView(
264-
bundleURL: jsCodeLocation,
265-
moduleName: "RNHighScores",
266-
initialProperties: mockData as [NSObject : AnyObject],
267-
launchOptions: nil
268-
)
269-
let vc = UIViewController()
270-
vc.view = rootView
271-
self.present(vc, animated: true, completion: nil)
256+
@IBAction func highScoreButtonTapped(sender: UIButton) {
257+
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)
272275
}
273276
```
274277

275-
> Note that `RCTRootView bundleURL` 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 bundleURL`, 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:...)`.
276279
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.
278281
279282
##### 3. Wire Up
280283

@@ -312,10 +315,23 @@ Apple has blocked implicit cleartext HTTP resource loading. So we need to add th
312315

313316
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:
314317

318+
<Tabs groupId="package-manager" defaultValue={constants.defaultPackageManager} values={constants.packageManagers}>
319+
<TabItem value="npm">
320+
315321
```shell
316-
$ npm start
322+
npm start
317323
```
318324

325+
</TabItem>
326+
<TabItem value="yarn">
327+
328+
```shell
329+
yarn start
330+
```
331+
332+
</TabItem>
333+
</Tabs>
334+
319335
##### 3. Run the app
320336

321337
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

Comments
 (0)