Using the SDK

To obtain tokens, follow these steps:

Step 1. Initialization

  1. 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)
    }
    
  2. If you use a UIScene, the URL-opening methods work via the UISceneDelegate protocol.

    To pass URLs to YXLSdk, use the corresponding SceneDelegate 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)
}
})
}


1. You can pass scopes (permissions for the app) within `YandexLoginSDK`. To do that, set permission parameters for the `YXLSdk` object: 
* `scopes` are required.
* `optionalScopes` are optional. 

Rights must be requested from the list defined when [registering the app](../../../../register-client.md#access). To see the allowed permissions, open the link [https://oauth.yandex.com/client/<client_id>/info](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 {#authorization}

1. To start user authorization, use this method:

```swift
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 the parentController parameter containing the authorization controller:

YXLSdk.shared.authorize(withUid: 0, login: nil, phone: nil, firstName: nil, lastName: nil, customValues: nil, parentController: viewController)

To run the authorization flow inside the app, set the required flag before starting authorization:

UserDefaults.standard.setValue(true, forKey:"blockBrowser")
[[NSUserDefaults standardUserDefaults] setObject:@(YES) forKey:@"blockBrowser"];
  1. To receive events with authorization results, you need to add yourself an observer:

    YXLSdk.shared.add(observer: self)
    
  2. Upon receiving a successful authorization result, the method with the result parameter containing the tokens listed below will be called:

    func loginDidFinish(with result: YXLLoginResult) {
       // use result.token and result.jwt
    }
    
  3. If an error occurs, the following will be called:

    func loginDidFinishWithError(_ error: Error) {
       // process error
    }
    
    • error: Error with the kYXLErrorDomain domain and YXLErrorCode code.

Step 3. Getting information about the user

You can exchange the obtained token for user information.

Step 4. Logout

To delete locally stored tokens, use the following method:

YXLSdk.shared.logout()