Learn how to implement user authentication to access a secure ArcGIS service with OAuth credentials.
Prerequisites
Before starting this tutorial:
-
You need an ArcGIS Location Platform or ArcGIS Online account.
-
Your system meets the system requirements.
Steps
-
Go to the Create OAuth credentials for user authentication tutorial to obtain a
ClientIDandRedirectURLto use in step 3. -
Add the ArcGIS Toolkit package to this swift project by following these instructions.
-
In the Project Navigator, click MainApp.swift.
- In the Editor, declare and initialize an
Authenticatortoolkit member in theMainAppstruct. - Create an initializer in the
MainAppstruct. - Create a new instance of the
Authenticatorusing anOAuthUserConfigurationthat contains your OAuth credentials from step 1. - Ensure the
ArcGISEnvironment.authenticationManageruses theauthenticatiorto handle any ArcGIS challenges.
MainApp.swift.@mainstruct MainApp: App {@ObservedObject var authenticator: Authenticatorinit() {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)} - In the Editor, declare and initialize an
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.