Defining an Owner

To operate a thing from a mobile application, you need to specify a Kii Cloud user as the thing owner. See here for the overview.

You can create a user with the User Management Feature of the Kii Cloud SDK. After you create a user, you will pass the user ID and access token of this user to the initialization API.

See Logging in and Using an Access Token to learn more about the access token.

There are several ways for creating a user. We recommend using the pseudo user if your scenario is simple (e.g., just sending commands and browsing state).

By using the pseudo user, you will create a user without any name and keep using their access token thereafter. Since it does not require any username or password, you can leverage the user without any explicit login.

Should you want to develop more complex applications by leveraging Kii Cloud SDK features, you can create a user or group and set them as the thing owner. See Implementation tips at the bottom of this page for more discussion.

Creating a pseudo user

The following sample code shows an example of creating a pseudo user. In this example, a new pseudo user is created and their user ID and access token are stored in the device storage (Note that the implementation of the data storing method is omitted in the sample code).

The access token and user ID are used when initializing the Thing-IF SDK. If you lose the access token, you will no longer be able to access to this user because there will be no way to distinguish the user.

// Set predefined and custom fields.
let userFields = KiiUserFields()
...

// Register a pseudo user.
KiiUser.registerAsPseudoUserWithUserFields(userFields) { (pseudoUser:KiiUser?, error: NSError?) -> Void in
  if error != nil {
    // Handle the error.
    return
  }

  // Get the access token of the preudo user.
  let accessToken = pseudoUser!.accessToken!

  // Get the user ID.
  let userID = pseudoUser!.userID!

  // Store the access token and the user ID with your own function.
  storeUser(accessToken, andUserID: userID)
}

The basic steps are as follows.

  1. Execute the registerAsPseudoUserWithUserFields method to create a pseudo user.
  2. After the user creation and login is executed, get the access token and the user ID from the accessToken and userID, respectively. Then, save them in the device.

Next, execute the following code. The owner you get in this code is to be passed to the ThingIFAPIBuilder of the initialization API.

// Create a typed ID from an ID type and the user ID.
let typedUserID = TypedID(type: "user", id: userID)

// Create an owner from the typed ID and the access token.
let owner = Owner(typedID: typedUserID, accessToken: accessToken)

This is what is happening in the sample code:

  1. Create a TypedID with the user ID and the string user that specify the type of the ID. If you are adding a group as an owner, you will pass the group ID with the string group.
  2. Create an Owner object from the TypeID and access token.

By using this "pseudo user as a thing owner" implementation on multiple devices, you will be able to share a thing among users who own the devices. This is done by binding the thing to the multiple users (i.e., pseudo users). This approach is OK as the Thing-IF SDK supports sharing a thing by multiple owners.

If you are planning to expand your application with the Kii Cloud SDK features (e.g., use the data management to store extra data), this simple relationship between the thing and users might not be sufficient (e.g., the user scope data are created per device, and they cannot be shared among users). Refer to the next section "Implementation tips".

Implementation tips

User type of the owner

We've presented a method to add a pseudo user as the thing owner. Alternatively, you can use the following approaches. All of them leverage the Kii Cloud SDK features.

  • Create a user with the username (or email address/phone number) and password, and let the user logs in with them (ref. Managing Users).
  • Create a group and set the group as the thing owner to share the thing among multiple group members (ref. Managing Groups).

Getting a command result

To get a command result, you need to receive its command ID as a push notification. However, such a push notification is sent only to the devices associated with the command sender. Design your mobile app with this point in mind.

If the owner is a pseudo user

The command ID is notified only to the device on which the command is executed because a pseudo user is unique to each device. You do not receive notifications for commands issued on other devices which share the thing.

If the owner is a normal user

The command ID is notified to all the devices associated with the owner by installing devices .