Skip to content

Commit

Permalink
Merge the Objective-C and Swift iOS existing-app guides
Browse files Browse the repository at this point in the history
Use `<Tabs>` to display the appropriate-language content.
  • Loading branch information
mhjacobson committed Feb 9, 2022
1 parent 3817751 commit 2a3659e
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 369 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -244,14 +244,61 @@ When you build a React Native application, you use the [Metro bundler][metro] to

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.

<Tabs groupId="language" defaultValue="objective-c" values={[ {label: 'Objective-C', value: 'objective-c'}, {label: 'Swift', value: 'swift'}, ]}>
<TabItem value="objective-c">

First `import` the `RCTRootView` header.

```objectivec
#import <React/RCTRootView.h>
```

</TabItem>
<TabItem value="swift">

First `import` the `React` module.

```swift
import React
```

</TabItem>
</Tabs>

> 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.
<Tabs groupId="language" defaultValue="objective-c" values={[ {label: 'Objective-C', value: 'objective-c'}, {label: 'Swift', value: 'swift'}, ]}>
<TabItem value="objective-c">

```objectivec
- (IBAction)highScoreButtonPressed:(id)sender {
NSURL *jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.bundle?platform=ios"];
NSDictionary *mockData = @{
@"scores" : @[
@{ @"name" : @"Alex", @"value": @"42" },
@{ @"name" : @"Joel", @"value": @"10" },
],
};

RCTRootView *rootView =
[[RCTRootView alloc] initWithBundleURL:jsCodeLocation
moduleName:@"RNHighScores"
initialProperties:mockData
launchOptions:nil];

UIViewController *vc = [[UIViewController alloc] init];
vc.view = rootView;
[self presentViewController:vc animated:YES completion:nil];
}
```

> 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:...]`.
> 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.
</TabItem>
<TabItem value="swift">

```swift
@IBAction func highScoreButtonTapped(sender: UIButton) {
let jsCodeLocation = URL(string: "http://localhost:8081/index.bundle?platform=ios")
Expand Down Expand Up @@ -279,6 +326,9 @@ import React
> 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.
</TabItem>
</Tabs>

##### 3. Wire Up

Wire up the new link in the main menu to the newly added event handler method.
Expand Down
Loading

0 comments on commit 2a3659e

Please sign in to comment.