Usage
To obtain tokens, follow these steps:
Step 1. Initialization
-
To initialize SDK Yandex ID, add this code to the AppDelegate:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // some code do { try YXLSdk.shared.activate(withAppId: "app ID_clientID") } catch { // process error } // some other code } @available(iOS 8.0, *) func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool { YXLSdk.shared.processUserActivity(userActivity) return true } func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool { return YXLSdk.shared.handleOpen(url, sourceApplication: sourceApplication) } @available(iOS 9.0, *) func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool { return YXLSdk.shared.handleOpen(url, sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String) }
-
If you use a UIScene, the URL-opening methods work via the UISceneDelegate protocol.
To pass URLs to
YXLSdk
, use the correspondingSceneDelegate
method:func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) { URLContexts.forEach({ context in if YXLSdk.shared.isUrlRelated(toSdk: context.url) { YXLSdk.shared.handleOpen(context.url, sourceApplication: nil) } }) }
-
You can pass scopes (permissions for the app) within
YandexLoginSDK
. To do that, set permission parameters for theYXLSdk
object:scopes
are required.optionalScopes
are optional.
You should only request permissions from the list defined at the time of app registration. To see the allowed permissions, open the link https://oauth.yandex.com/client/<client_id>/info, replacing <client_id> with your app's ID.
If additional permissions are requested when the user is already logged in, a screen asking for those new permissions is displayed.
Step 2. Authorization
-
To start user authorization, use this method:
YXLSdk.shared.authorize()
By default, the user is redirected to the browser (external app) for authorization.
In iOS 13 or higher, you can use
ASWebAuthenticationSession
. In this case, pass theparentController
parameter containing the authorization controller:YXLSdk.shared.authorize(withUid: 0, login: nil, phone: nil, firstName: nil, lastName: nil, customValues: nil, parentController: viewController)
In order to log in via the application, set a special flag before logging in:
SwiftObjective-CUserDefaults.standard.setValue(true, forKey:"blockBrowser")
[[NSUserDefaults standardUserDefaults] setObject:@(YES) forKey:@"blockBrowser"];
-
To get events with authorization results, add an observer:
YXLSdk.shared.add(observer: self)
-
When the result of a successful authorization is received, the method is called with a
result
parameter containing tokens:func loginDidFinish(with result: YXLLoginResult) { // use result.token and result.jwt }
-
result.token
is an OAuth token used in requests to Yandex APIs. -
result.jwt
is a JSON Web Token.
-
-
When an error occurs, this method is called:
func loginDidFinishWithError(_ error: Error) { // process error }
error
is an error with akYXLErrorDomain
domain and aYXLErrorCode
code.
Step 3. Getting user information
You can exchange the obtained token for user information.
Step 4. Logout
To delete locally stored tokens, use this method:
YXLSdk.shared.logout()