Menu
Amazon API Gateway
Developer Guide

Use an iOS SDK generated by API Gateway in a Swift App

Before beginning the following procedure, you must complete the steps in Generate SDKs for an API Using the API Gateway Console for iOS in Swift and download the .zip file of the generated SDK.

Install AWS Mobile SDK and API Gateway-Generated SDK in a Swift Project

The following procedure describes how to install the SDK.

To install and use an iOS SDK generated by API Gateway in Swift

  1. Extract the contents of the API Gateway-generated .zip file you downloaded earlier. Using the SimpleCalc API, you may want to rename the unzipped SDK folder to something like sdk_swift_simple_calc. In this SDK folder there is a README.md file and a Podfile file. The README.md file contains the instructions to install and use the SDK. This tutorial provides details about these instructions. The installation leverages CocoaPods to import required AWS Mobile SDK components. You must update the Podfile to import the SDKs into your Swift app's Xcode project. The unarchived SDK folder also contains a generated-src folder that contains the source code of the generated SDK of your API.

  2. Launch Xcode and create a new iOS Swift project. Make a note of the project's target. You will need to set it in the Podfile.

  3. To import the required AWS Mobile SDK components into the Xcode project by using CocoaPods, do the following:

    1. If it is not installed, install CocoaPods by running the following command in a terminal window:

      Copy
      sudo gem install cocoapods pod setup
    2. Copy the Podfile file from the extracted SDK folder into the same directory containing your Xcode project file. Replace the following block:

      Copy
      target '<YourXcodeTarget>' do pod 'AWSAPIGateway', '~> 2.4.7' end

      with your project's target name as shown:

      Copy
      target 'app_swift_simple_calc' do pod 'AWSAPIGateway', '~> 2.4.7' end

      If your Xcode project already contains a Podfile with the correct target, you can simply add the following line of code to the do ... end loop:

      Copy
      pod 'AWSAPIGateway', '~> 2.4.7'
    3. Open a terminal window and run the following command in the app directory:

      Copy
      pod install

      This installs the API Gateway component and any dependent AWS Mobile SDK components into the app's project.

    4. Close the Xcode project and then open the *.xcworkspace file to relaunch Xcode.

    5. Add all of the SDK's header files (.h) and Swift source code files (.swift) from the extracted generated-src directory to your Xcode project.

    6. To enable calling the Objective-C libraries of the AWS Mobile SDK from your Swift code project, set the Bridging_Header.h file path on the Objective-C Bridging Header property under the Swift Compiler - General setting of your Xcode project configuration:

      Tip

      You can type bridging in the search box of Xcode to locate the Objective-C Bridging Header property.

    7. Build the Xcode project to verify that it is properly configured before proceeding further. If your Xcode uses a more recent version of Swift than the one supported for the AWS Mobile SDK, you will get Swift compiler errors. In this case, set the Use Legacy Swift Language Version property to Yes under the Swift Compiler - Version setting:

    To import the AWS Mobile SDK for iOS in Swift into your project by explicitly downloading the AWS Mobile SDK or using Carthage, follow the instructions in the README.md file that comes with the SDK package. Be sure to use only one of these options to import the AWS Mobile SDK.

Call API methods through the iOS SDK generated by API Gateway in a Swift Project

When you generated the SDK with the prefix of SIMPLE_CALC for this SimpleCalc API with two models to describe the input (Input) and output (Result) of the API's requests and responses, in the SDK, the resulting API client class becomes SIMPLE_CALCSimpleCalcClient and the corresponding data classes are SIMPLE_CALCInput and SIMPLE_CALCResult, respectively. The API requests and responses are mapped to the SDK methods as follows:

  • The API request of

    Copy
    GET /?a=...&b=...&op=...

    becomes the SDK method of

    Copy
    public func rootGet(op: String?, a: String?, b: String?) -> AWSTask

    The AWSTask.result property is of the SIMPLE_CALCResult type if the Result model was added to the method response. Otherwise, it is of the NSDictionary type.

  • This API request of

    Copy
    POST / { "a": "Number", "b": "Number", "op": "String" }

    becomes the SDK method of

    Copy
    public func rootPost(body: SIMPLE_CALCInput) -> AWSTask
  • The API request of

    Copy
    GET /{a}/{b}/{op}

    becomes the SDK method of

    Copy
    public func aBOpGet(a: String, b: String, op: String) -> AWSTask

The following procedure describes how to call the API methods in Swift app source code; for example, as part of the viewDidLoad() delegate in a ViewController.m file.

To call the API through the iOS SDK generated by API Gateway

  1. Instantiate the API client class:

    Copy
    let client = SIMPLE_CALCSimpleCalcClient.defaultClient()

    To use Amazon Cognito with the API, set a default AWS service configuration (shown following) before getting the defaultClient method (shown previously):

    Copy
    let credentialsProvider = AWSCognitoCredentialsProvider(regionType: AWSRegionType.USEast1, identityPoolId: "my_pool_id") let configuration = AWSServiceConfiguration(region: AWSRegionType.USWest2, credentialsProvider: credentialsProvider) AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration
  2. Call the GET /?a=1&b=2&op=+ method to perform 1+2:

    Copy
    client.rootGet("+", a: "1", b:"2").continueWithBlock {(task: AWSTask) -> AnyObject? in self.showResult(task) return nil }

    where the helper function self.showResult(task) prints the result or error to the console; for example:

    Copy
    func showResult(task: AWSTask) { if let error = task.error { print("Error: \(error)") } else if let result = task.result { if result is SIMPLE_CALCResult { let res = result as! SIMPLE_CALCResult print(String(format:"%@ %@ %@ = %@", res.input!.a!, res.input!.op!, res.input!.b!, res.output!.c!)) } else if result is NSDictionary { let res = result as! NSDictionary print("NSDictionary: \(res)") } } }

    In a production app, you can display the result or error in a text field. The resulting display is 1 + 2 = 3.

  3. Call the POST / with a payload to perform 1-2:

    Copy
    let body = SIMPLE_CALCInput() body.a=1 body.b=2 body.op="-" client.rootPost(body).continueWithBlock {(task: AWSTask) -> AnyObject? in self.showResult(task) return nil }

    The resultant display is 1 - 2 = -1.

  4. Call the GET /{a}/{b}/{op} to perform 1/2:

    Copy
    client.aBOpGet("1", b:"2", op:"div").continueWithBlock {(task: AWSTask) -> AnyObject? in self.showResult(task) return nil }

    The resulting display is 1 div 2 = 0.5. Here, div is used in place of / because the simple Lambda function in the back end does not handle URL encoded path variables.