Implement API key authentication

Prerequisites

Before starting this tutorial:

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

  2. Your system meets the system requirements.

Steps

You need an access token to use the location services used in this tutorial.

  1. Go to the Create an API key tutorial to obtain a new API key access token.

    • Ensure that the following privilege is enabled: Location services > Basemaps > Basemap styles service.

    • Copy the access token as it will be used in the next step.

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

    • In the Editor, add an import statement to reference the API and implement an initializer in the MainApp struct.

    • Set the ArcGISEnvironment.apiKey property with your API key access token.

    MainApp.swift
    Use dark colors for code blocks
    2 3 4 5 6 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.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    
    import SwiftUI
    
    import ArcGIS
    
    @main
    struct MainApp: App {
    
        init() {
    
            ArcGISEnvironment.apiKey = APIKey("<#YOUR-ACCESS-TOKEN#>")
    
        }
    
        var body: some SwiftUI.Scene {
            WindowGroup {
                ContentView()
    
                    .ignoresSafeArea()
    
            }
        }
    
    }

For more information, see API key authentication.

Best Practice: The access token is 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.