Kii BLOG

Part 2: Powering Your App

The second post in the “What can Kii do for me?” series will highlight some of the core functionality that makes Kii Cloud an excellent solution for a fast, easy-to-implement and reliable mobile backend. If you missed part 1 (a general overview of Kii Cloud), click here.

Our goal with Kii Cloud is to make developers’ lives easier. This includes taking commonly-used functionality on the server side and making it easily manageable from a native client SDK. This is all done on a carrier-grade backend that will automatically scale with your app. Two of the most critical components of any cloud-based app are User Management and Data Management. Kii Cloud makes these incredibly easy to use within your app, right out of the box.

 

User Management

The first thing you want to do as an app developer is create users who can maintain state across devices, platforms, and sessions. This provides an excellent personalized experience for your application; as an app developer it provides you with a clear understanding of your users and gives you insight to improve your application.

When building user management in your backend, registration and authentication are the first steps. However, more requirements for profile and account management will arise, as will password management, welcome emails and more. Rather than building these APIs yourself, Kii Cloud provides all of this functionality using native client methods that can be called directly from your application.

Here’s a quick example for creating and authenticating a new user with your application:

String username = “user_123456″;
String password = “123ABC”;
KiiUser user = KiiUser.createWithUsername(username);

try{
user.register(password);
} catch (IOException e) {
// Sign-up failed for some reasons
// Please check IOExecption to see what went wrong…
} catch (AppException e) {
// Sign-up failed for some reasons
// Please check AppException to see what went wrong…
}

NSString *username = @”user123456″;
NSString *password = @”123ABC”;

KiiUser *user = [KiiUser userWithUsername:username
andPassword:password];

// register the user
[user performRegistrationWithBlock:^(KiiUser *user, NSError *error) {
if(error == nil) {
NSLog(@"Registered user: %@", user);
}
}];

var username = “user123456″;
var password = “123ABC”;

// Create the KiiUser object
var user = KiiUser.userWithUsername(username, password);

// Register the user, defining callbacks for when the process completes
user.register({
// Called on successful registration
success: function(theUser) {
// Print some info to the log
console.log(“User registered!”);
console.log(theUser);
},

// Called on a failed registration
failure: function(theUser, errorString) {
// Print some info to the log
console.log(“Error registering: ” + errorString);
}
});

 

Data Management

Object Storage

This is the most commonly-used storage type, and includes JSON-style objects that can be saved to the backend, queried and even linked to users and/or other objects. Virtually every cloud-based application utilizes this type of storage.

If you’re a full-stack developer, the equivalent may be a JSON-responding API with an SQL storage as a backer. Rather than writing the APIs, making the database connections, creating a scalable & reliable system, and paying for the provider – you can get this out of the box with Kii Cloud via a single, easy-to-use API call.

These types of objects are stored in buckets which can belong to the entire application, a group of users or a single user. This scope determines the default permissions, but the access control can also be defined at an object-level if you wish.

Here’s an example of creating an object in an application-scoped bucket named “app_bucket”:

// Create an object
KiiObject obj = Kii.bucket(“app_bucket”).object();

// Set key-value pairs
obj.set(“myid”, 1);
obj.set(“name”, “John Doe”);
obj.set(“email”, “john@example.com”);

// Save the object
try {
obj.save();
} catch (IOException e) {
// Handle error
} catch (AppException e) {
// Handle error
}

KiiBucket *myBucket = [Kii bucketWithName:@"app_bucket"];

// Create an object with key/value pairs
KiiObject *obj = [myBucket createObject];
[obj setObject:[NSNumber numberWithInt:1] forKey:@”myid”];
[obj setObject:@"John Doe" forKey:@"name"];
[obj setObject:@"john@example.com" forKey:@"email"];

// Save the object
[obj saveWithBlock:^(KiiObject *myObject, NSError *error) {
if(error == nil) {
NSLog(@"myObject saved: %@", myObject);
}
}];

// Create an application scope bucket named “app_bucket”
var appBucket = Kii.bucketWithName(“app_bucket”);

// Create the object with key/value pairs in this bucket
var obj = appBucket.createObject();
obj.set(“myid”, 1);
obj.set(“name”, “John Doe”);
obj.set(“email”, “john@example.com”);

// Save the object
obj.save({
success: function(theObject) {
console.log(“Object saved!”);
console.log(theObject);
},
failure: function(theObject, errorString) {
console.log(“Error saving object: ” + errorString);
}
});

Easy, right? This is just a taste of what Kii Cloud is capable of. Our full-service SDKs and APIs give you the ability to update, query, and even grant permissions on buckets or single objects. If you’d like to learn more and get a full feature list, check out our guides here.

 

File Storage

As applications become more data-intensive and billions of pictures, videos, notes and files are uploaded from mobile devices each day, it is imperative for your application to have a scalable file storage system. Many options exist, but Kii Cloud also offers this out of the box, giving you carrier-grade storage that’s free to start.

Along with all other Kii Cloud functionality, the SDKs and APIs provide this via easy-to-use methods for your favorite platforms. Here’s a simple example for uploading a file to Kii Cloud:

// Local file reference.
File f = new File(“/mnt/sdcard/image.jpg”);

try {
// Store file in “MyStorage” bucket on Kii Cloud.
KiiFile kiiFile = Kii.fileBucket(“MyStorage”).file(f);// Upload the file to the Cloud
kiiFile.save();
} catch (IOException e) {
// Handle error
} catch (AppException e) {
// Handle error
}

// Create bucket
KiiFileBucket *bucket = [[KiiUser currentUser] fileBucketWithName:@”my_private”];

// Create a KiiFile using the local filepath
KiiFile *file = [bucket fileWithLocalPath:@"path/to/my/file"];

// Save the file metadata + body to the server
[file saveFileWithProgressBlock:^(KiiFile *file, double progress) {
NSLog(@"Progress: %lf for file: %@", progress, file);
}
andCompletionBlock:^(KiiFile *file, NSError *error) {
if(error == nil) {
NSLog(@"File saved: %@", file);
} else {
NSLog(@"Error saving file: %@ with error: %@", file, error);
}
}
];

That’s all it takes to upload any type of file to a reliable, scalable backend – and it only takes a few lines of code.

 

What’s next?

By now you should have a feel for how easy and useful Kii Cloud can be. Stay tuned for the next few blog posts; we will introduce some new functionality and discuss the next steps to your app’s business.

And of course, if you want to start coding away, check out http://developer.kii.com where you can sign up and get started for FREE. You’ll have your first cloud-based app up and running in a matter of minutes.

 

Associated articles

  1. Introducing Kii Cloud
  2. Powering Your App
  3. Analyzing Your App
  4. Grow Your Business
  5. Success Stories