How to get token in Arcgis iOS runtime and use it to access layers behind authentication

569
3
01-04-2023 05:56 AM
AxnMax
by
New Contributor II

Can someone let me know the correct procedure to to get token in Arcgis iOS runtime and how to use it to access layers behind authentication

 

0 Kudos
3 Replies
NimeshJarecha
Esri Regular Contributor

Hi AxnMax,

The iOS Runtime SDK supports loading a secured resources like layers, tables and portal. When you try to load any secured resource it detects the configured security on the server and prompts you to enter the credentials. If the service is secured using token authentication then provided username/password in the prompt will be used to create the `AGSCredential` object and token will be generated. Once, the object is loaded the created credential during the authentication process will be available as `credential` property of the object. Also, the credential will be stored in the `credential cache` at `AGSAuthenticationManager.shared().credentialCache`. 

The sample code is available in Token Authentication sample.

Regards,

Nimesh

 

0 Kudos
AxnMax
by
New Contributor II

Hi Nimesh,

Thank you for your reply. I have 2 questions:

1. Is there any way to suppress the prompt to enter the credentials, so we can supply the credentials through code.

2. Is there any way to use the token generated through the rest end point (/generateToken) and provide to the runtime for accessing the layers. 

 

0 Kudos
NimeshJarecha
Esri Regular Contributor

> 1. Is there any way to suppress the prompt to enter the credentials, so we can supply the credentials through code.

Yes,  you can create `AGSCredential` with `username` and `password` using the initializer and set it on the object you are trying to load like this,

let portal = AGSPortal.arcGISOnline(withLoginRequired: true)

let credential = AGSCredential(user: "<username>", password: "<password>")

portal.credential = credential

 

> 2. Is there any way to use the token generated through the rest end point (/generateToken) and provide to the runtime for accessing the layers. 

Yes, you can create `AGSCredential` with pre-generated token and referer using initializer. However, that the token not be automatically re-generated when expired. If expired, you'll see an authentication challenge and will have to provide a new credential. 

 

Regards,

Nimesh

0 Kudos