iosswift

API authentication with swift


I want to make a GET request to an API. I have an API key but don't know how to implement it with my swift app. The API documentation says this:

"API requests must be made over HTTPS and be accompanied by an API Key sent via the X-IFL-API-Key header."


Solution

  • Use NSMutableURLRequest and simply add the header:

    request.setValue(apiKey, forHTTPHeaderField: "X-IFL-API-Key")
    

    You can find the documentation for NSMutableURLRequest here. Once you have created and configured your URL request, you can "invoke" it by using the various methods of NSURLSession.