Method guide
YandexLoginSDK class
The main class involved in all interactions with the SDK. This class is a singleton. Its only instance is a static variable shared.
To start authorization, use the method authorize(with:customValues:authorizationStrategy:). A parent View Controller has to be passed to it.
To get the authorization result, you need to register a SDK Yandex ID observer using the add(observer:) method. When you no longer need notifications from LoginSDK, you can unregister the observer using the remove(observer:) method.
To delete the saved tokens or re-authorize the user, use the logout() method.
Static variables
-
The only instance of the
YandexLoginSDKclass:static var shared: YandexLoginSDK { get }Make sure to use the static variable
sharedto get variable values and call methods. -
The current LoginSDK version:
static var version: String { get }It's formatted in accordance with Semantic Versioning (SemVer).
Methods
-
Activate SDK Yandex ID:
func activate(with: String, authorizationStrategy: AuthorizationStrategy) throwsPass the Client ID and your selected authorization strategy
authorizationStrategy(.defaultis the default value) to this method. The method will validate your app configuration before the SDK is activated. If the validation fails, the method call will result in an error. -
Obtain a token:
func handleUserActivity(NSUserActivity) throwsThis method handles the NSUserActivity argument to extract the URL passed to the app and then extract the tokens from the URL. If successful, the observers call the
didFinishLogin(with:)method with a.successargument and an associated value of the LoginResult type. -
Wrap the method
handleUserActivity(_:):func tryHandleUserActivity(NSUserActivity) –> BoolThe method
handleUserActivity(_:)may throw an error, while the methodtryHandleUserActivity(_:)calls it in a do-catch statement and returnsfalse, if an error occurs. -
Obtain the authorization code:
func handleOpenURL(URL) throwsThis method handles the URL passed to it to extract the authorization code and then uses that code to request tokens. If successful, the observers call the
didFinishLogin(with:)method with a.successargument and an associated value of the LoginResult type. -
Wrap the method
handleOpenURL(_:):func tryHandleOpenURL(URL) –> BoolThe method
handleOpenURL(_:)may throw an error, while the methodtryHandleOpenURL(_:)calls it in a do-catch statement and returnsfalseif an error occurs. -
Validate the URL:
func isURLRelatedToSDK(URL) –> BoolThis method checks whether the URL passed to it is related to SDK Yandex ID.
-
Add an observer:
func add(observer: any LoginSDKObserver)This method adds the object passed to it to the list of SDK Yandex ID observers. The observer will receive notifications about all the results of the SDK running, specifically about successful authorizations and errors.
-
Remove an observer:
func remove(observer: any LoginSDKObserver)This method removes the object passed to it from the list of SDK Yandex ID observers.
-
Start the authorization process:
func authorize(with: UIViewController, customValues: [String: String]?, authorizationStrategy: AuthorizationStrategy) throwsThe
parentViewControllerparameter is required even when the selected authorization strategy is to use Yandex apps. The reason is that there may be no such apps on the user's device. In this case, the SDK will switch to authorization via a web interface. ThecustomValuesparameter has a default value ofnil.
-
Delete the tokens from storage:
func logout() throwsUse this method when there's a need to re-authorize.
YandexLoginSDK.AuthorizationStrategy enumeration
The enumeration (enum) that determines the user authorization strategy for SDK Yandex ID. Depending on the value of the authorizationStrategy property set in YandexLoginSDK, SDK Yandex ID decides whether to authorize the user via Yandex apps or a web interface.
Values
-
Default strategy:
case defaultWhen this strategy is selected, SDK Yandex ID tries to open a Yandex app that supports authorization and authorize the user there. If there are no such apps, SDK Yandex ID tries to authorize the user via a web interface.
-
The web authorization strategy:
case webOnlyIf you need to authorize the user via a web interface, set this strategy when activating the app in the method
activate(with:authorizationStrategy:)of the YandexLoginSDK class instance. In this case, the activator won't require the app's Info.plist to have schemes for launching Yandex apps.
LoginResult structure
The LoginResult structure stores the tokens that were obtained during authorization. An instance of this structure is passed to SDK Yandex ID observers in the method didFinishLogin(with:) when the authorization is successful.
Variables
-
OAuth token:
var token: String { get }Used in requests to Yandex APIs.
-
JSON Web Token:
var jwt: String { get }Learn more about the JSON Web Token.
-
The structure represented as a dictionary:
var asDictionary: [String: String] { get }The dictionary's keys and values are strings. The key for the OAuth token is “token“, and the key for the JSON Web Token is “jwt“.
-
The structure represented as a string:
var asString: String { get }
LoginSDKObserver protocol
SDK Yandex ID uses the LoginSDKObserver protocol to notify observers that the authorization process has ended. Only classes can implement this protocol.
To subscribe to changes, use the addObserver(_:) method of the YandexLoginSDK class.
To unsubscribe from changes, use the removeObserver(_:) method of the YandexLoginSDK class.
Methods
-
Finish authorization:
func didFinishLogin(with: Result<LoginResult, any Error>)This method is called in two cases:
- SDK Yandex ID has successfully completed the authorization, obtaining an OAuth token and a JSON Web Token.
- SDK Yandex ID has encountered an error during the authorization.
An unsuccessful authorization attempt will have an error as an associated value, with a type conforming to the
Errorprotocol. Specifically, the error may correspond to theYandexLoginSDKErrorprotocol.
YandexLoginSDKError protocol
The YandexLoginSDKError protocol combines all the errors generated by SDK Yandex ID. You can use the message variable to get a string description for any such error.
Variables
-
A string with detailed information about an error:
var message: String { get }