Seamless: Using the Apigee SDK in Swift
With the announcement of Apple’s new programming language Swift, there's a huge need for companies like ours to ensure compatibility for our SDK.
The good news is that Apple has made the integration process pretty seamless. Using Swift, we wrote a sample application for the Apigee iOS SDK called EventManager. This sample app is a catch-all for both the app services and the app monitoring that the SDK provides. While writing the EventManager sample app, there were a few key unique features worth exploring when integrating the Apigee iOS SDK using Swift.
To expose a set of Objective-C files and non native frameworks for use in your Swift code, you need to use an Objective-C bridging header.
Importing objective-C code into the bridging header file
As per Apple’s documentation:
Within the bridging header file, you need to use imports like you would in any normal Objective-C code. To import the Apigee SDK for use in your Swift code, set up your bridging header as follows.
After you have created this file, make sure the Objective-C bridging header build setting has the path to your created bridging header, as follows. At this point, you will be able to use the Apigee SDK within any Swift code.
Import the frameworks
A Swift-based application does not currently contain a frameworks folder like an Objective-C based app would, so you'll need to import the frameworks required for the Apigee SDK.
The biggest challenge when creating an app is getting familiar with Swift language. For instance, the initialization of the ApigeeClient class, while trivial, takes some getting used to.
Initializing the ApigeeClient class in Objective-C
Initializing the ApigeeClient in Swift
One of the more amazing parts of Swift is the use of tuples. As shown in the next example, pulled from our EventManager sample app, the use of tuples and the unique features Swift provides can come in handy.
The method getEvents returns a tuple containing multiple values. In this case, getEvents returns the result of two separate methods, which are used to get the private and public events.
Calling this method and using the returned tuple is just as easy; you can pull the various values out of the tuple with ease and it will look and feel great in your code.
Aside from the project integration, using Swift with the Apigee SDK is just as seamless as it is when using Apple’s native frameworks.
For more about Swift, read our recent post Apple's Swift and CloudKit: Mainstreaming BaaS.
landing page image: Ron Knight/Flickr