Implement user authentication

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.
    Use dark colors for code blocks
    7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
    Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    @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. In a production environment we do not recommend that you store it directly in source code.

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.