DZone
Mobile Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Mobile Zone > Swift Modules With React Native

Swift Modules With React Native

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

Cray Styris user avatar by
Cray Styris
·
Jul. 22, 16 · Mobile Zone · Tutorial
Like (5)
Save
Tweet
7.74K Views

Join the DZone community and get the full member experience.

Join For Free

React Native retains the native properties of the platform and hence it is a robust choice for cross-platform app 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.

React Native React (JavaScript library) Swift (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 8 Must-Have Project Reports You Can Use Today
  • How to Build a Simple CLI With Oclif
  • Monitoring Spring Boot Application With Prometheus and Grafana
  • What Emerging Technologies Make Data Centers More Energy Efficient?

Comments

Mobile Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo