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
Client
andID Redirect
to use in step 3.URL -
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
Authenticator
toolkit member in theMain
struct.App - Create an initializer in the
Main
struct.App - Create a new instance of the
Authenticator
using anO
that contains your OAuth credentials from step 1.Auth User Configuration - Ensure the
ArcGIS
uses theEnvironment.authentication Manager 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 25Add 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. @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) }
- 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. In a production environment we do not recommend that you store it directly in source code.