Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Swift Modules With React Native

DZone's Guide to

Swift Modules With React Native

The author takes us through migrating a React Native project from Objective-C to Swift.

· Mobile Zone
Free Resource

Discover how to focus on operators for Reactive Programming and how they are essential to react to data in your application.  Brought to you in partnership with Wakanda

React Native retains the native properties of the platform and hence it is a robust choice for cross-platform development with iOS as well. React is still not widely used with Swift, and on Github, there are few directions.

React Native is an Objective-C application framework that uses JavaScript language for running applications in JScore for iOS APIs. The underlying principle of React Native is that it uses native components of the platform to maintain look and feel and essential features. There are areas where React Native resources are inadequate for implementing certain functionalities

React Native APIs

React Native APIs send local notifications, however; it lacks API for processing. React Native project is written in Objective C, these can be written in Swift and exposed to the application. Swift looks more like JavaScript with fewer brackets and easier to write. There are few drawbacks, though, for instance, it can’t be converted to a .xcodeproj file with .a for easy linking. It can be converted in a .xcodeproj with .framework for bridging.

To Migrate React Native Project From Objective-C to Swift

Some tools that can be used to effectively translate the tools are Xcode, NodeJS, React Native 0.1.19 and React-Native 0.19. Install react-native-cli using $ npm install -g react-native-cli and create a React Native project using $ react-native init SwiftReactNative. Now, open file ios/SwiftReactNative.xcodeproj in Xcode and start the project. It will take a few minutes for the app to start, stop it and migrate it to Swift. Three files written in Objective-C will have to be replaced:

AppDelegate.h

AppDelegate.m

main.m

Create and name Swift file AppDelegate.swift. Once the Xcode file is added, it will offer us to create Objective-C with bridging header.

Click Create Bridging Header and select Swift language. Now, 2 new files should appear:

AppDelegat.swift

SwiftReactNative-Bridging-Header.h

Add this code to SwiftReactNative-Bridging-Header.h:

#import "RCTBridgeModule.h"

#import "RCTBridge.h"

#import "RCTEventDispatcher.h"

#import "RCTRootView.h"

#import "RCTUtils.h"

#import "RCTConvert.h"

Now edit the file AppDelegate.swift and delete the following files:

AppDelegate.h

AppDelegate.m

main.m

Migrating to iOS 9

In order to migrate to the latest iOS 9 version, in project settings go to General – Deployment Target and select version 9.0. Clean the project in Product- Clean and start it on the emulator by selecting Product- Run. When the application starts, it implies that the Swift migration has occurred successfully.

For verification, we can access arguments passed to the Swift method from JavaScript. To implement Swift method you have to implement it with a callback parameter and call it with a callback function from JavaScript.

With interface of Swift native code for JavaScript in React Native we can implement our application logic in either language.

Learn how divergent branches can appear in your repository and how to better understand why they are called “branches".  Brought to you in partnership with Wakanda

Topics:
react native ,ios swift ,swift 2 ,javascript

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}