The Mobile & Digital Assistant Blog covers the latest in mobile and conversational AI development and engagement

A little taste of Swift with Oracle Mobile Cloud Service

Chris Muir
Product Manager

Within the iOS development world arguably there is a trend to adopt Apple's Swift language over Objective-C for new application development. History has taught Oracle not to pick sides in such debates, being "client agnostic" is a common and frequent term thrown around internal to Oracle these days to ensure we embrace all development technologies.

Extending this discussion around Swift to Oracle Mobile Cloud Service (MCS), Oracle's contemporary MBaaS solution, a question we're getting asked frequently is does MCS support Swift? Currently MCS support an iOS Objective-C SDK, but what about Swift?

The thing about this question is it typically doesn't come from veteran iOS developers, but from people who are in the process of learning iOS programming for the first time, have read about the Swift trend and picked that as their iOS language to learn, but don't yet know enough about the interoperability between Objective-C code and Swift.

In promoting Swift but realizing there was a huge Objective-C code base out there for iOS apps, Apple included an interoperable set of features to allow Objective-C to call Swift code and vice versa. In order for Swift to make use of Objective-C libraries, you implement a "bridging header file" which lists the Objective-C libraries to import and then makes them available to Swift in its native syntax.

So returning to MCS, what exactly would you need to do to build an iOS application in Swift, using the Objective-C SDK provided by MCS?

First you create a Swift app in Xcode. Then you load the MCS iOS SDK into your Xcode project as shown in this video. Next you add the bridging header file following these steps:

  1. With the Xcode project selected, create a .h file MyAppName_Bridging_Header.h (ensuring the file is a child of the project node).
  2. In the header file import all the MCS iOS SDK header files from under the MCS/release-iphoneos/include subdirectories. An example bridging header file for MCS can be seen below.
  3. Return to the project 'Build Settings’ and search for 'Objective-C Bridging Header’. Set this to MyAppName_Bridging_Header.h.
  4. Run the project - this should complete successfully.

The example bridging header file:

#ifndef MyAppName_Bridging_Header_h

#define MyAppName_Bridging_Header_h

#import "OMCAppConfig.h"

#import "OMCAuthorization.h"

#import "OMCCore.h"

#import "OMCDiagnostics.h"

#import "OMCMobileBackend.h"

#import "OMCMobileBackendManager.h"

#import "OMCServiceProxy.h"

#import "OMCUser.h"

#import "OMCAnalytics.h"

#import "OMCAnalyticsApplicationDelegate.h"

#import "OMCAnalyticsDelegate.h"

#import "OMCMobileBackend+OMC_Analytics.h"

#import "OMCNotifications.h"

#import "OMCMobileBackend+OMC_Notifications.h"

#import "OMCStorage.h"

#import "OMCStorageCollection.h"

#import "OMCStorageConstants.h"

#import "OMCStorageObject.h"

#import "OMCMobileBackend+OMC_Storage.h"

#import "OMCFetchFileBuilder.h"

#import "OMCFetchObjectBuilder.h"

#import "OMCFetchObjectCollectionBuilder.h"

#import "OMCMobileEndpoint.h"

#import "OMCMobileFile.h"

#import "OMCMobileObject.h"

#import "OMCMobileObjectCollection.h"

#import "OMCMobileResource.h"

#import "OMCSyncGlobals.h"

#import "OMCSynchronization.h"

#import "OMCSyncPolicy.h"

#import "OMCSyncStoreState.h"

#import "OMCMobileBackend+OMC_Synchronization.h"


Once you're done this you've pretty much set to go to call the MCS SDK libraries in Swift. What can be a bit of challenge from there is mentally converting Objective-C code examples to Swift. Objective-C uses a C like notation with pointers and an abundance of square brackets, while Swift has a simpler syntax in my opinion to learn, but notably drops semicolons as line terminators which my typing brain just can't help adding after 20 years as a programmer!

To help you on your way, following is an Objective-C example for authenticating MCS mobile users via the iOS MCS SDK, then the equivalent in Swift using the same SDK but also with the bridging header file. Here's the Objective-C example:

#import "OMCAuthorization.h"

#import "OMCMobileBackend.h"

#import "OMCMobileBackendManager.h"

OMCMobileBackendManager *manager = [OMCMobileBackendManager sharedManager];

OMCMobileBackend *backend = manager.defaultMobileBackend;

OMCAuthorization *auth = [backend authorization];

NSError *err = [auth authenticate:@"username" password:@"password"];

The following is the equivalent in Swift using the iOS SDK:

// No imports necessary when bridging header file used

let manager: OMCMobileBackendManager = OMCMobileBackendManager.sharedManager()

let backend: OMCMobileBackend = manager.defaultMobileBackend

let auth: OMCAuthorization = backend.authorization

let err: NSError! = auth.authenticate("username", password: "password")

Hopefully this gives you a little taste of Swift with Oracle Mobile Cloud Service.

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.

Recent Content