Access services with OAuth 2.0
The following are required for this tutorial:
- An ArcGIS account to access your API keys. If you don't have an account, sign up for free.
- Your system meets the system requirements.
- The ArcGIS Runtime API for iOS is installed.
Steps
Configure OAuth 2.0 for your app
Register your app to generate a client ID and set a redirect URI to access the secure service:
- Sign in to your ArcGIS account. If you don't already have one, sign-up for free. You need to sign in so you can define an application and assign a client ID for authentication.
- At the top right of the main menu, click > New Application to create a new application.
- Fill in your application details and then select Register New Application. Take note of the Client ID value that is automatically assigned to your app.
- On the Authentication tab use the Redirect URIs section to add
my-app://auth
. Take note of the redirect URL you choose as you will need it later. Also note this URI is composed of two parts: a scheme (my-app
), followed by a path (auth
), separated with://
.
Set the app settings
Add a new swift file to your Xcode project named
AppConfiguration
. You will use this to define a structure to hold configuration constants required by your app.Add the following
struct
to AppConfiguration.swift. When you enter this code, change"YOUR-APP-CLIENT-ID"
to the Client ID you set in the application definition from the prior step. Update the URL scheme and path to match your Redirect URIs entry from the prior step.- Replace the
client_id
andredirect_url
values with the values shown on the authentication tab of your application definition.
- Replace the
Open ViewController.swift and update the existing
setupMap()
method to add the traffic image layer to the map.
At this point you can run and test your app. The basemap should load but the traffic layer will not load until you are able to use the AGSAuthenticationManager
to log in a user and get a valid access token.
Integrate OAuth 2.0 into your app
Configure a redirect URL scheme for your app. Right-click on info.plist file in the Project Navigator and then select Open As > / Source Code. Edit the file just after the opening top-level
<dict>
tag and add the following XML:Open AppDelegate.swift to setup the
AGSAuthenticationManager
in yourAppDelegate
. This is done here because iOS will delegate control back to your withUIApplication.OpenURLOptionsKey
once OAuth 2.0 completes. Import the ArcGIS library:Create a new function to setup the authentication manager. This code creates a configuration with the parameters you assigned to your app in
AppConfiguration
and then assigns that configuration to theAGSAuthenticationManager
. The credentials are also saved in the device's keychain..When OAuth 2.0 completes, iOS will return control back to your app using the Redirect URI. Here you ensure the inbound URL is your app's redirect URL. Use the
AGSApplicationDelegate
to open the inbound OAuth 2.0 redirect.Add a call to
setupOAuthManager()
from the application launch:Press Command-R to run the app in the iOS Simulator.
Congratulations, you're done!
Your app should open on your device then show a dialog from ArcGIS Online asking for OAuth 2.0 login. Once successfully logged in the map displays with the traffic layer added. Compare your solution with our completed solution project.