Login with the Auto-Saved Credentials

In this approach, your application performs the re-login with the access token and credentials that are automatically stored by the SDK. The access token and credentials are auto-saved by the SDK when a user login or refresh is executed.

Kii SDK will store the following user credentials in the Keychain (not in iCloud Keychain) each time when you successfully log in or refresh user.

  • User ID
  • Username
  • Email address
  • Phone number
  • Display name
  • Country
  • Access Token
  • Expiration period of the access token
  • Refresh token

The information is deleted only when the logout is executed.

Kii SDK will store the user credentials in the Keychain as an item with kSecClassKey for kSecClass and "KiiSDK/user_credentials" for kSecAttrApplicationLabel. If your application uses the Keychain to store information, please make sure not to conflict with the credentials stored by Kii SDK. Please read Keychain Services Programming Guide from Apple for the details.

The following sample code shows how to login with these credentials.

Swift:

  • let user : KiiUser?
    
    do{
      // Authenticate a user with the stored credentials.
      user = try KiiUser.authenticateWithStoredCredentialsSynchronous()
    
      // Refresh the user.
      try user?.refreshSynchronous()
    }catch let error as NSError {
      // Handle the error.
      return
    }
  • // Authenticate a user with the stored credentials.
    KiiUser.authenticate(storedCredentials: { (user :KiiUser?, error : Error?) -> Void in
    
      if error != nil {/* Handle the error. */ return} // you can remove this line if you don't handle this error.
    
      // Refresh the user.
      user?.refresh {(user :KiiUser?, error )->Void in
        if (error != nil) {
          // Handle the error.
          return
        }
      }
    })

Objective-C:

  • NSError *error = nil;
    
    // Authenticate a user with the stored credentials.
    KiiUser *user = [KiiUser authenticateWithStoredCredentialsSynchronous:&error];
    if (error != nil) {
      // Handle the error.
      return;
    }
    
    // Refresh the user.
    [user refreshSynchronous:&error];
    if (error != nil) {
      // Handle the error.
      return
    }
  • // Authenticate a user with the stored credentials.
    [KiiUser authenticateWithStoredCredentials:^(KiiUser *user, NSError *error) {
      if (error != nil) {
        // Handle the error.
        return;
      }
    
      // Refresh the user.
      [user refreshWithBlock:^(KiiUser *user, NSError *error) {
        if (error != nil) {
          // Handle the error.
          return;
        }
      }];
    }];

You can login by executing the authenticate(storedCredentials:_:) method if the credentials are stored in the Keychain. This method just restores the login state from the Keychain and does not access the server. To get the latest user information from the server, you need to execute the refresh(_:) method.

The stored credentials are deleted from the Keychain when you execute the logOut() method. Executing the authenticate(storedCredentials:_:) method after the logout will throw an exception.

An error will be thrown if reading the stored credentials from the Keychain fails for some reasons. Please consider handling such a case by letting users logs in with their password.

Whether the user logs in again or not, the credentials including the access token are saved every time when the user logs in or gets refreshed.

You can also let pseudo users log in with the authenticate(storedCredentials:_:) method by calling the refresh(_:) method of the KiiUser class just after each pseudo user is created.

When you design your mobile app, make sure to implement serialization and screen transition logics. See Title screen in Kii Balance tutorial for some references.