Learn how to implement user authentication User authentication is a type of authentication that allows users with an ArcGIS account to sign into an application and allow it to access ArcGIS content, services, and resources on their behalf. The typical authorization protocol used is OAuth2.0. Learn more to access a secure ArcGIS service A service, also known as an ArcGIS service, is software that supports an ArcGIS REST API and provides geospatial functionality or data. A service can be hosted by Esri or in ArcGIS Enterprise. Learn more with OAuth credentials OAuth credentials are an item that contains parameters required to implement user authentication or app authentication, including a client_id, client_secret, and redirect URIs. They are a type of developer credential. Learn more .

Figure : Overview of how to access a secure ArcGIS service with OAuth credentials.

You can use different types of authentication to access secured ArcGIS services A service, also known as an ArcGIS service, is software that supports an ArcGIS REST API and provides geospatial functionality or data. A service can be hosted by Esri or in ArcGIS Enterprise. Learn more . To implement OAuth credentials for user authentication User authentication is a type of authentication that allows users with an ArcGIS account to sign into an application and allow it to access ArcGIS content, services, and resources on their behalf. The typical authorization protocol used is OAuth2.0. Learn more , you can use your ArcGIS account An ArcGIS account is an identity with a user type and set of privileges that can access specific ArcGIS products, tools, APIs, services, and resources. The main account types that can be used for development are an ArcGIS Location Platform account, ArcGIS Online account, and ArcGIS Enterprise account. ArcGIS Location Platform and ArcGIS Online accounts are also associated with a subscription. Learn more to register an app with your portal ArcGIS portal, also known as a portal, is a website with applications and tools that can be used to create, manage, access, and share geospatial content and data. It supports security and authentication, developer credentials, content and data service management, user and group management, and site administration. A portal can be hosted in Esri's infrastructure or your own infrastructure. Learn more and get a Client ID A Client ID is an identifier associated with an application that assists with client / server OAuth 2.0 authentication for ArcGIS client APIs. Learn more , and then configure your app to redirect users to login with their credentials when the service or content is accessed. This is known as user authentication. If the app uses premium ArcGIS Online ArcGIS Online is a GIS mapping, analytics, data hosting, and content management software as a service (SaaS) product. It includes applications, tools, APIs, and location services for users and developers. It is subscription-based and requires an ArcGIS Online account. Learn more services that consume credits Credits are the currency used by ArcGIS Online Organization accounts to account for data storage and location service consumption. Credits are consumed for specific transactions, such as accessing location services, and types of storage, such as storing features, performing analytics, and using premium content. Learn more , for example, the app user’s account will be charged.

In this tutorial, you will build an app that implements user authentication using OAuth credentials so users can sign in and be authenticated through ArcGIS Online to access the ArcGIS World Traffic service.

Prerequisites

Before starting this tutorial:

  1. You need an ArcGIS Location Platform or ArcGIS Online account.

  2. Your system meets the system requirements.

Set up authentication

To access the secure ArcGIS location services ArcGIS Location Services, also referred to as Location Services, are services hosted by Esri that provide geospatial functionality for developing mapping applications. They include the ArcGIS Basemap Styles service, ArcGIS Static Basemap Tiles service, ArcGIS Places service, ArcGIS Geocoding service, ArcGIS Routing service, ArcGIS GeoEnrichment service, and ArcGIS Elevation service. An ArcGIS Location Platform or ArcGIS Online account is required to use the services. Learn more used in this tutorial, you must implement user authentication User authentication is a type of authentication that allows users with an ArcGIS account to sign into an application and allow it to access ArcGIS content, services, and resources on their behalf. The typical authorization protocol used is OAuth2.0. Learn more using an ArcGIS Location Platform An ArcGIS Location Platform account, formerly known as an ArcGIS Developer account, is an identity associated with an ArcGIS Location Platform subscription. Learn more or an ArcGIS Online An ArcGIS Online account, also known as an ArcGIS Organization account, is an identity associated with an ArcGIS Online subscription. It can be used to access ArcGIS tools and develop applications with ArcGIS location services for an organization. Learn more account.

Create a new OAuth credential to access the secured resources used in this tutorial.

  1. Complete the Create OAuth credentials for user authentication tutorial.

  2. Copy and paste the ClientID and RedirectURL into a safe location. They will be used in a later step.

All users that access this application need account privileges Privileges are a set of permissions assigned to ArcGIS accounts, developer credentials, and applications that grant access to secure resources and functionality in ArcGIS. Learn more to access the basemap styles service The ArcGIS Basemap Styles service, also referred to as the Basemap Styles service, is a location service that provides basemap styles and data for the world. It returns styles as Mapbox styles and web maps, and data as vector tiles and/or map tiles. It supports all of the styles in the ArcGIS Basemap style and Open Basemap style family. An ArcGIS Location Platform or ArcGIS Online account is required to use the service. Learn more .

Develop or Download

You have two options for completing this tutorial:

  1. Option 1: Develop the code or
  2. Option 2: Download the completed solution

Option 1: Develop the code

Create a new app

To get started, use Xcode to create an iOS app and configure it to reference the API.

  1. Open Xcode. In the menu bar, click File > New > Project.

    • In the Choose a template for your new project: window, set the following properties:
      • Multiplatform iOS
      • Application App
    • Click Next.
    • In the Choose options for your new project: window, set the following properties:
      • Product Name: <your app name>
      • Organization Identifier: <your organization>
      • Interface: SwiftUI
      • Language: Swift
    • Uncheck all other options.
    • Click Next.
    • Choose a location to store your project. Click Create.
  2. In the Project Navigator, click <your app name>App. In the Editor, right click on the struct name, <your app name>App. Select Refactor > Rename… to rename the struct to MainApp. Click the Rename button in the top right to confirm the new name. This will rename the struct and file in all affected areas.

  3. Add a reference to the API using Swift Package Manager.

  4. In the MainApp.swift file, some errors may appear after importing ArcGIS. Resolve the errors by distinguishing the Scene protocol from Scene. To do so, add the SwiftUI prefix to Scene.

    MainApp.swift
    var body: some SwiftUI.Scene {
    WindowGroup {
    ContentView()
    }
    }

Add a protected layer to map

Add the World Traffic layer to the map.

  1. In the Project Navigator, click ContentView.swift.

  2. In the Editor, add an import statement to reference the API.

    ContentView.swift
    import SwiftUI
    import ArcGIS
  3. Add a Map with the @State property wrapper with a default value. Create a Map with an arcGISTopographic basemap style and an inital viewpoint and return it.

    ContentView.swift
    struct ContentView: View {
    @State private var map = {
    let map = Map(basemapStyle: .arcGISTopographic)
    map.initialViewpoint = Viewpoint(latitude: 34.02700, longitude: -118.80500, scale: 72_000)
    return map
    }()
  4. Add a secured traffic layer to the map’s operational layers collection.

    ContentView.swift
    @State private var map = {
    let map = Map(basemapStyle: .arcGISTopographic)
    map.initialViewpoint = Viewpoint(latitude: 34.02700, longitude: -118.80500, scale: 72_000)
    let trafficLayerURL = URL(string: "http://www.arcgis.com/home/item.html?id=ff11eb5b930b4fabba15c47feb130de4")!
    let trafficLayer = ArcGISMapImageLayer(url: trafficLayerURL)
    map.addOperationalLayer(trafficLayer)
    return map
    }()
  5. Replace the default code in the body to display the map. Create a MapView using the previously created map.

    ContentView.swift
    var body: some View {
    MapView(map: map)
    }
  6. Open the MainApp.swift file. Add the .ignoreSafeArea modifier to the ContentView. This modifier ensures that the map view is displayed beyond the safe area to all edges.

    MainApp.swift
    var body: some SwiftUI.Scene {
    WindowGroup {
    ContentView()
    .ignoresSafeArea()
    }
    }

Integrate OAuth credentials into your app

Use the ArcGIS Maps SDK for Swift Toolkit component to create an Authenticator object that handles authentication every time a secured ArcGIS resource is accessed.

  1. The Toolkit is required to access the Authenticator component. Follow the installation instructions to add Toolkit to the MainApp.swift file.

    MainApp.swift
    import SwiftUI
    import ArcGIS
    import ArcGISToolkit
  2. Create an Authenticator object with the Observed Object property wrapper.

    MainApp.swift
    struct MainApp: App {
    // Setup an `Authenticator` with OAuth configuration.
    @ObservedObject var authenticator = Authenticator(
    oAuthUserConfigurations: [
    ]
    )
  3. Initialize the Authenticator with an OAuthConfiguration object. Use your ClientID and RedirectURL credentials that you created in the Set up authentication step.

    MainApp.swift
    // Setup an `Authenticator` with OAuth configuration.
    @ObservedObject var authenticator = Authenticator(
    oAuthUserConfigurations: [
    OAuthUserConfiguration(
    // Enter OAuth credentials for user authentication.
    portalURL: URL(string: "https://www.arcgis.com")!,
    clientID: "<#YOUR-CLIENT-ID#>",
    redirectURL: URL(string: "<#YOUR-REDIRECT-URL#>")!
    )
    ]
    )
  4. Create an initializer function and assign the authenticator to the ArcGISAuthenticationChallengeHandler. This points the challenge to the authenticator which will handle the request.

    MainApp.swift
    struct MainApp: App {
    // Setup an `Authenticator` with OAuth configuration.
    @ObservedObject var authenticator = Authenticator(
    oAuthUserConfigurations: [
    OAuthUserConfiguration(
    // Enter OAuth credentials for user authentication.
    portalURL: URL(string: "https://www.arcgis.com")!,
    clientID: "<#YOUR-CLIENT-ID#>",
    redirectURL: URL(string: "<#YOUR-REDIRECT-URL#>")!
    )
    ]
    )
    init() {
    ArcGISEnvironment.authenticationManager.arcGISAuthenticationChallengeHandler = authenticator
    }
  5. Lastly, display the OAuth interface. Add the authenticator view modifier, passing in the authenticator object.

    MainApp.swift
    var body: some SwiftUI.Scene {
    WindowGroup {
    ContentView()
    .authenticator(authenticator)
    .ignoresSafeArea()
    }
    }
  6. Press Command+R to run the app.

Upon app launch, you will be prompted to log in with your ArcGIS Location Platform or ArcGIS Online credentials. Once you authenticate successfully, the basemap and traffic layer will appear in the map.

Manage data persistence

In real-world applications, you can create and persist a credential store in the keychain so that the user does not have to sign in again when the app is re-launched.

  1. Create an asynchronous function called setupPersistentCredentialStorage that sets up new credential stores that will be persisted to the keychain. Use the authenticationManager.setupPersistentCredentialStorage method to create the credential store and set the keychain access to whenUnlocked. This will allow the item to be accessible only when device is unlocked.

    MainApp.swift
    init() {
    ArcGISEnvironment.authenticationManager.arcGISAuthenticationChallengeHandler = authenticator
    }
    private func setupPersistentCredentialStorage() async {
    Task {
    try await ArcGISEnvironment.authenticationManager.setupPersistentCredentialStorage(access: .whenUnlocked)
    }
    }
  2. Set up the credential stores upon app launch. Add a task function to ContentView. In the closure, call the setupPersistentCredentialStorage function.

    MainApp.swift
    var body: some SwiftUI.Scene {
    WindowGroup {
    ContentView()
    .authenticator(authenticator)
    .ignoresSafeArea()
    .task {
    await setupPersistentCredentialStorage()
    }
    }
    }
  3. Press Command + R to run the app.

Upon app launch, you will not be prompted to log in again.

Alternatively, you can download the tutorial solution, as follows.

Option 2: Download the solution

  1. Click the Download solution link under Solution and unzip the file to a location on your machine.

  2. Open the .xcodeproj file in Xcode.

Since the downloaded solution does not contain authentication credentials, you must add the developer credentials that you created in the Set up authentication section.

Set developer credentials in the solution

  1. In the Project Navigator, click MainApp.swift.

  2. Enter your clientID and redirectURL values that you created earlier.

    MainApp.swift
    24 collapsed lines
    // Copyright 2025 Esri
    //
    // Licensed under the Apache License, Version 2.0 (the "License");
    // you may not use this file except in compliance with the License.
    // You may obtain a copy of the License at
    //
    // https://www.apache.org/licenses/LICENSE-2.0
    //
    // Unless required by applicable law or agreed to in writing, software
    // distributed under the License is distributed on an "AS IS" BASIS,
    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    // See the License for the specific language governing permissions and
    // limitations under the License.
    import SwiftUI
    import ArcGIS
    import ArcGISToolkit
    @main
    struct MainApp: App {
    // Setup an `Authenticator` with OAuth configuration.
    @ObservedObject var authenticator = Authenticator(
    oAuthUserConfigurations: [
    OAuthUserConfiguration(
    // Enter OAuth credentials for user authentication.
    portalURL: URL(string: "https://www.arcgis.com")!,
    clientID: "<#YOUR-CLIENT-ID#>",
    redirectURL: URL(string: "<#YOUR-REDIRECT-URL#>")!
    )
    ]
    )
    28 collapsed lines
    init() {
    ArcGISEnvironment.authenticationManager.arcGISAuthenticationChallengeHandler = authenticator
    }
    private func setupPersistentCredentialStorage() async {
    Task {
    try await ArcGISEnvironment.authenticationManager.setupPersistentCredentialStorage(access: .whenUnlocked)
    }
    }
    var body: some SwiftUI.Scene {
    WindowGroup {
    ContentView()
    .authenticator(authenticator)
    .ignoresSafeArea()
    .task {
    await setupPersistentCredentialStorage()
    }
    }
    }
    }

Best Practice: The OAuth credentials are stored directly in the code as a convenience for this tutorial. Do not store credentials directly in source code in a production environment.

Run the solution

Press Command + R to run the app.

Upon app launch, you will be prompted to log in with your ArcGIS Location Platform or ArcGIS Online credentials. Once you authenticate successfully, the basemap and traffic layer will appear in the map.

What’s next?

Learn how to use additional API features, ArcGIS location services, and ArcGIS tools in these tutorials: