Usage

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)
          }
       })
    }
    
  3. 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.

    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

  1. 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 the parentController 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:

    UserDefaults.standard.setValue(true, forKey:"blockBrowser")
    
    [[NSUserDefaults standardUserDefaults] setObject:@(YES) forKey:@"blockBrowser"];
    
  2. To get events with authorization results, add an observer:

    YXLSdk.shared.add(observer: self)
    
  3. 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
    }
    
  4. When an error occurs, this method is called:

    func loginDidFinishWithError(_ error: Error) {
       // process error
    }
    
    • error is an error with a kYXLErrorDomain domain and a YXLErrorCode 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()