Learn how to implement user authentication to access a secure ArcGIS service with OAuth credentials.

Prerequisites

Before starting this tutorial:

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

  2. Your system meets the system requirements.

Steps

  1. Go to the Create OAuth credentials for user authentication tutorial to obtain a ClientID and RedirectURL to use in step 3.

  2. Add the ArcGIS Toolkit package to this swift project by following these instructions.

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

    • In the Editor, declare and initialize an Authenticator toolkit member in the MainApp struct.
    • Create an initializer in the MainApp struct.
    • Create a new instance of the Authenticator using an OAuthUserConfiguration that contains your OAuth credentials from step 1.
    • Ensure the ArcGISEnvironment.authenticationManager uses the authenticatior to handle any ArcGIS challenges.
    MainApp.swift.
    @main
    struct MainApp: App {
    @ObservedObject var authenticator: Authenticator
    init() {
    authenticator = Authenticator(oAuthUserConfigurations: [
    OAuthUserConfiguration(
    portalURL: URL(string: "<#YOUR-PORTAL-URL#>")!,
    clientID: ""<#YOUR-CLIENT-ID#>"",
    redirectURL: URL(string: "<#YOUR-REDIRECT-URL#>")!
    )
    ])
    ArcGISEnvironment.authenticationManager.handleChallenges(using: authenticator)
    }

For more information, see the User authentication.

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.