iOS Objective-C Calling APIs

Sample Project

Download a sample project specific to this tutorial configured with your Auth0 API Keys.

System Requirements
  • CocoaPods 1.2.1
  • Version 8.3.2 (8E2002)
  • iPhone 7 - iOS 10.3 (14E269)
Show requirements

Auth0 provides a set of tools for protecting your resources with end-to-end authentication in your application.

This tutorial shows you how to get an access token, attach it to a request with an authorization header and call an API. We recommend you use this method for the best security and compliance with RFC standards.

Get the User's Credentials

You need an access token for your API to check if the request is authenticated.

You can retrieve the token from an Credentials instance. Read the Login article for instructions on how to get credentials.

Then, present the login screen:

// HomeViewController.m

HybridAuth *auth = [[HybridAuth alloc] init];
[auth showLoginWithScope:@"openid profile" connection:nil callback:^(NSError * _Nullable error, A0Credentials * _Nullable credentials) {
    dispatch_async(dispatch_get_main_queue(), ^{
        if (error) {
            NSLog(@"Error: %@", error);
        } else if (credentials) {
          // Do something with credentials e.g.: save them.
          // Auth0 will dismiss itself automatically by default.
        }
    });
}];

Attach the Token

This example shows how to use the accessToken value.

Depending on the standards in your API, you configure the authorization header differently. The code below is just an example.

To attach an access token to a request:

// ProfileViewController.m

NSString* token = ... // The accessToken you stored after authentication
NSString *url = @"https://localhost/api"; // Change to your API
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
// Configure your request here (method, body, etc)

[request addValue:[NSString stringWithFormat:@"Bearer %@", token] forHTTPHeaderField:@"Authorization"];
[[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
    // Parse the response
}] resume];

Sample project configuration

When you are testing the sample project, configure your URL request in the ProfileViewController.swift file:

// ProfileViewController.m

NSString *url = @"https://localhost/api"; // Change to your API
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
// Configure your request here (method, body, etc)

After you send a request and receive a response from your API, you can check the request status code in an alert view.

Read more about authentication API on the server-side in the API documentation.

Previous Tutorial
3. User Sessions
Next Tutorial
5. Authorization
Was this article helpful?
Use Auth0 for FREECreate free Account