Skip to content

Commit 0db3a32

Browse files
authored
codepush setup updates (#3442)
* codepush additions * additional setup * typo fix * link fix * link fix * link fix
1 parent edbe126 commit 0db3a32

3 files changed

Lines changed: 181 additions & 5 deletions

File tree

content/_index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ How to get started with Codemagic
1616

1717
{{</links-group>}}
1818

19-
{{<links-group title="Codepush guides">}}
20-
OTA for React Native
19+
{{<links-group title="CodePush guides">}}
20+
OTA updates for React Native
2121
- [Concepts](../rn-codepush/concepts/)
2222
- [Setup](../rn-codepush/setup/)
2323
- [Troubleshooting](../rn-codepush/debugging-and-common-issues/)

content/rn-codepush/security-and-access.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,30 @@ openssl rsa -in codepush_private.key -pubout -out codepush_public.key
105105
- The **public key** is embedded in the mobile app
106106
- Used to verify that updates were signed by a trusted source
107107
- Can be safely distributed with the application
108-
108+
- **iOS:** add `CodePushPublicKey` in `Info.plist` and string value of public key content. Example:
109+
{{< highlight bash "style=paraiso-dark">}}
110+
<plist version="1.0">
111+
<dict>
112+
<!-- ...other configs... -->
113+
114+
<key>CodePushPublicKey</key>
115+
<string>-----BEGIN PUBLIC KEY-----
116+
MFwwDQYJKoZIhvcNAQEy.....==
117+
-----END PUBLIC KEY-----</string>
118+
119+
<!-- ...other configs... -->
120+
</dict>
121+
</plist>
122+
{{< /highlight >}}
123+
- **Android:** add `CodePushPublicKey` string item to `strings.xml`. Example:
124+
{{< highlight bash "style=paraiso-dark">}}
125+
<resources>
126+
<string name="app_name">my_app</string>
127+
<string name="CodePushPublicKey">-----BEGIN PUBLIC KEY-----
128+
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtPSR9lkGzZ4FR0lxF+ZA.......
129+
-----END PUBLIC KEY-----</string>
130+
</resources>
131+
{{< /highlight >}}
109132

110133
### Sign update packages
111134

content/rn-codepush/setup.md

Lines changed: 155 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ This section prepares a project to use CodePush with Codemagic. After completing
1616

1717
Codemagic hosts the CodePush server and developers interact with it using [access tokens](#get-an-access-token) and the CodePush CLI. If you want to learn about how OTA updates work, check out the [concepts page](https://docs.codemagic.io/rn-codepush/concepts/).
1818

19-
These instructions are for React Native New Architecture projects. If your app is already configured, skip to setting up [deployment keys](#configure-deployment-keys) and CI sections to verify configuration.
19+
These instructions are for React Native New Architecture projects. If your app is already configured, skip to setting up [deployment keys](#add-codepush-to-a-react-native-app) and CI sections to verify configuration.
2020

2121
The same Codemagic server can be used for all of your apps.
2222

@@ -178,7 +178,160 @@ export default codePush(App);
178178

179179
This enables the SDK to automatically check for updates on app start (or based on your chosen update strategy).
180180

181-
6. **Run a test OTA release**
181+
### CodePush iOS Setup (React Native)
182+
183+
This guide covers the iOS-specific configuration required to enable CodePush OTA updates in your React Native app.
184+
185+
**1. Install iOS Dependencies**
186+
187+
Navigate to the ios directory and install CocoaPods:
188+
189+
{{< highlight bash "style=paraiso-dark">}}
190+
cd ios && pod install && cd ..
191+
{{< /highlight>}}
192+
193+
**2. Update AppDelegate**
194+
195+
You need to configure your app to load the JavaScript bundle from CodePush instead of the embedded bundle.
196+
197+
**Objective-C**: If your project is `Objective-C` based (scroll down for `Swift` based configuration), then open the `AppDelegate.m` file and add an import statement for the CodePush headers at the top:
198+
199+
{{< highlight text "style=paraiso-dark">}}
200+
#import <CodePush/CodePush.h>
201+
{{< /highlight>}}
202+
203+
**3. Update JS Bundle Location**
204+
205+
Find the following existing bundle reference in the `AppDelegate.m` file:
206+
207+
{{< highlight bash "style=paraiso-dark">}}
208+
jsCodeLocation = [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
209+
{{< /highlight >}}
210+
211+
And replace it with:
212+
213+
{{< highlight bash "style=paraiso-dark">}}
214+
jsCodeLocation = [CodePush bundleURL];
215+
{{< /highlight >}}
216+
217+
At the end, your `sourceURLForBridge` method should look like this:
218+
219+
{{< highlight bash "style=paraiso-dark">}}
220+
- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
221+
{
222+
#if DEBUG
223+
return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index"];
224+
#else
225+
return [CodePush bundleURL];
226+
#endif
227+
}
228+
{{< /highlight >}}
229+
230+
**Swift** :If your project is using `Swift` based configuration, then follow the steps below:
231+
232+
1. Navigate to your iOS project and open the `AppDelegate.swift` file
233+
2. Add the following import at the top of the file:
234+
235+
{{< highlight bash "style=paraiso-dark">}}
236+
import CodePush
237+
{{< /highlight >}}
238+
239+
3. Find the following line of code in the `AppDelegate.swift` file:
240+
241+
{{< highlight bash "style=paraiso-dark">}}
242+
Bundle.main.url(forResource: "main", withExtension: "jsbundle")
243+
{{< /highlight >}}
244+
245+
And replace it with:
246+
247+
{{< highlight bash "style=paraiso-dark">}}
248+
CodePush.bundleURL()
249+
{{< /highlight>}}
250+
251+
Your `bundleUrl` method should look like this:
252+
253+
{{< highlight bash "style=paraiso-dark">}}
254+
override func bundleURL() -> URL? {
255+
#if DEBUG
256+
RCTBundleURLProvider.sharedSettings().jsBundleURL(forBundleRoot: "index")
257+
#else
258+
CodePush.bundleURL()
259+
#endif
260+
}
261+
{{< /highlight >}}
262+
263+
### CodePush Android Setup (React Native)
264+
265+
{{<notebox>}}
266+
Plugin installation and configuration for React Native 0.76 version and above
267+
{{</notebox>}}
268+
269+
**1. Ensure Native Linking**
270+
271+
In `android/app/build.gradle`, add `codepush.gradle` as an additional build task definition to the end of the file:
272+
273+
{{< highlight bash "style=paraiso-dark">}}
274+
apply from: "../../node_modules/@code-push-next/react-native-code-push/android/codepush.gradle"
275+
{{< /highlight >}}
276+
277+
**2. Update MainApplication**
278+
279+
Open the `MainApplication` file and update it as follows:
280+
281+
{{< highlight shell "style=paraiso-dark">}}
282+
...
283+
// 1. Import the plugin class.
284+
import com.microsoft.codepush.react.CodePush
285+
286+
class MainApplication : Application(), ReactApplication {
287+
override val reactNativeHost: ReactNativeHost =
288+
object : DefaultReactNativeHost(this) {
289+
override fun getPackages(): List<ReactPackage> = PackageList(this).packages.apply {
290+
// Packages that cannot be autolinked yet can be added manually here, for example:
291+
// add(MyReactNativePackage())
292+
}
293+
294+
// 2. Override the getJSBundleFile method in order to let
295+
// the CodePush runtime determine where to get the JS
296+
// bundle location from on each app start
297+
override fun getJSBundleFile(): String {
298+
return CodePush.getJSBundleFile()
299+
}
300+
};
301+
}
302+
303+
{{< /highlight>}}
304+
305+
For React Native **0.82** and above, make the following changes to `MainApplication.kt`:
306+
307+
{{< highlight shell "style=paraiso-dark">}}
308+
...
309+
// 1. Import the plugin class.
310+
import com.microsoft.codepush.react.CodePush
311+
312+
class MainApplication : Application(), ReactApplication {
313+
314+
override val reactHost: ReactHost by lazy {
315+
getDefaultReactHost(
316+
context = applicationContext,
317+
packageList =
318+
PackageList(this).packages.apply {
319+
// Packages that cannot be autolinked yet can be added manually here, for example:
320+
// add(MyReactNativePackage())
321+
},
322+
// 2. RN 0.82+ uses ReactHost config instead of overriding getJSBundleFile().
323+
// Set jsBundleFilePath to CodePush so CodePush resolves the JS bundle path
324+
// at startup (OTA update if available, fallback to bundled JS otherwise).
325+
jsBundleFilePath = CodePush.getJSBundleFile(),
326+
)
327+
}
328+
}
329+
{{< /highlight>}}
330+
331+
332+
333+
334+
### Run a test OTA release
182335

183336
To verify that CodePush is working end-to-end, you can publish a quick test release:
184337
{{< highlight bash "style=paraiso-dark">}}

0 commit comments

Comments
 (0)