Getting the Current User

When a user logs in, the Kii Cloud SDK gets the user. While the user is logged in, you can get the user with an API.

When a user logs in, the SDK gets the user's access token from Kii Cloud. The SDK then stores the access token along with other user information in its static area for the details on the access token). In another word, the SDK remembers the "current user."

You can get the currently logged-in user by calling the currentUser method.

Swift:

  • // Get the currently logged-in user.
    let user = KiiUser.current()
    
    do {
      // Refresh the user to get the latest user info from Kii Cloud.
      try user?.refreshSynchronous()
    } catch let error as NSError {
      // Handle the error.
      return
    }
  • // Get the currently logged-in user.
    let user = KiiUser.current()
    
    // Refresh the user to get the latest user info from Kii Cloud.
    user?.refresh { (user :KiiUser?, error : Error?) -> Void in
      if error != nil {
        // Handle the error.
        return
      }
    }

Objective-C:

  • // Get the currently logged-in user.
    KiiUser *user = [KiiUser currentUser];
    
    NSError *error = nil;
    
    // Refresh the user to get the latest user info from Kii Cloud.
    [user refreshSynchronous:&error];
    if (error != nil) {
      // Handle the error.
      return;
    }
  • // Get the currently logged-in user.
    KiiUser *user = [KiiUser currentUser];
    
    // Refresh the user to get the latest user info from Kii Cloud.
    [user refreshWithBlock:^(KiiUser *user, NSError *error) {
      if (error != nil) {
        // Handle the error.
        return;
      }
    }];

Call the refreshSynchronous method to get the latest user information from Kii Cloud.

If the currentUser method returns nil, it means nobody is logged in.

The SDK can only handle one logged-in user at a time. If you want to handle multiple users, you need to login again as a different user. The login state of the previous user is lost by making this user switch.