DZone
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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
11 Monitoring and Observability Tools for 2023
Learn more
  1. DZone
  2. Coding
  3. Frameworks
  4. 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 · Tutorial
Like (5)
Save
Tweet
Share
7.90K 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

  • 7 Most Sought-After Front-End Frameworks for Web Developers
  • Build an Automated Testing Pipeline With GitLab CI/CD and Selenium Grid
  • Reliability Is Slowing You Down
  • gRPC on the Client Side

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • 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: