Appcelerator Platform Services SDK for iOS -- Appcelerator Cloud Services

The Appcelerator Platform Services (APS) SDK for iOS provides APIs for your native iOS application to access Appcelerator Cloud Services (ACS).

Getting the SDK

To download and start using the SDK, you first need to create new iOS application in Dashboard. See Managing Native Applications in Dashboard for details on creating a new native application. After you create an application, a service key is generated that associates your application with all the Platform services. Dashboard also provides full instructions for enabling all Platform Services in your application. This guide will deal specifically with enabling and using Appcelerator Cloud Services in an iOS application.

Running the APSCloudExample Application

The SDK ZIP file includes an iOS sample project that demonstrates basic use of each of the Cloud APIs. To run the sample you first need to create a new application in Dashboard to obtain the necessary service application. You will then copy this key into the imported sample project's main Activity and then run the application.

To create the APSCloudExample application in Dashboard:

  1. Login to Appcelerator Dashboard.
  2. From the Orgs menu, select the organization to associate with the application.
  3. Click the Apps menu and select Add a Native App.
  4. In the New Native Application dialog, do the following:
    • Enter APSCloudExampleApp (or other name) in the Name field.
    • Select iOS from the Platform menu.
    • Select any category from the Category menu.

  5. Click Next and then click the Overview tab.
  6. Click the Services tab, then click Show Key under Cloud / Performance / Analytics.
  7. Select Development from the Environment menu, then click the clipboard icon to copy the key to your clipboard.

Next, you'll import the APSCloudExample project into Xcode, copy the key from your clipboard into the application's main activity, and run the application.

To import the completed APSCloudExample project:

  1. In Xcode, open the appcelerator-sdk-ios-1.0/examples/APSCloudExample.xcodeproj.
  2. Open AppDelegate.m and locate the following line of code.
  3. Replace << YOUR APP KEY >> with the application key you copied to your clipboard previously. To learn where to find your application key, see View Application Information.

      String appKey = "<< YOUR APP KEY >>";
    
  4. Run the application on an iOS device or simulator.

Once the application is running, try the following:

  • Create a new user by selecting Users > Create User. Enter a username, password and password confirmation and then click Create. If the user is created successfully, the following dialog is shown:

  • View the newly created user in Dashboard:

    1. Open Dashboard and select your application from the Apps menu.
    2. Select Cloud > Manage Data, then click Users in the Manage Data Object table. You should see the user you created listed in the Users table.

Enabling Cloud services in a new Project

Once you've created an application in Dashboard, downloaded the SDK, and obtained your application service key, there are few steps to enable Cloud service in your iOS project. The steps are slightly different if you are using Xcode 6 or 5.

To enable the Cloud service in an existing Xcode project:

  1. Add the appcelerator-sdk-ios-1.0.0/Appcelerator.framework folder to your project:
    • For Xcode 5 projects, drag the Appcelerator.framework folder to your project's Frameworks folder, making sure Copy items into destination group's folder is checked, and click Finish.
    • For Xcode 6 projects, drag the Appcelerator.framework folder to the project's root folder, making sure Copy Items if Needed is checked, and click Finish.
  2. On your project's Build Phases tab, expand the Link Binary With Libraries section and add the libsqlite3.dylib and libz.dylib frameworks.
  3. On your project's Build Settings tab, click All in the top-left corner, then expand the Linking section.
  4. In the Other Linker Flags field, enter -ObjC.

  5. Import Appcelerator.h into your application delegate:

     #import <Appcelerator/Appcelerator.h>
    
  6. Lastly, initialize Platform services by calling replacing << YOUR APP KEY >> with the service key generated by Dashboard when you created your application. (See View Application Information for how to locate your application key.)

      [[APSServiceManager sharedInstance] enableWithAppKey:@"<<YOUR APP KEY>>"];
    

At this point, your application can begin making API calls.

Making API Calls and Handling Responses

The iOS framework includes a collection of classes whose methods map to individual REST API method endpoints. For example, the [APSUsers create] method corresponds to the /users/create.json method endpoint.

Alternatively, you can use the generic [APSCloud sendRequest] method to make REST calls directly against the Cloud APIs. For more information, see Making Generic REST API Calls.

Building Request Parameters

The first parameter of each Cloud API method is a NSDictionary object that contains the of parameters to send with the request. For example, the [APSPhotos show] method takes a photo_id parameter whose value is, naturally, the ID of the photo to show.

// Create dictionary of parameters to be passed with the request
NSDictionary *data = @{@"photo_id": self.photoId};
[APSPhotos show:data withBlock:^(APSResponse *e) {
    // Handle response
}];

Handling Responses

The second parameter of each method call is a block that is passed an APSResponse whose properties contain information about the response. For instance, the success property contains a boolean indicating if the method call was successful or not; the response property returns a JSON-encoded object with the results of the method call.

NSDictionary *data = @{@"photo_id": self.photoId};
[APSPhotos show:data withBlock:^(APSResponse *e) {
    // The block will be called on the thread the request was started on
    if (e.success) {
        // Remove the backslashes from URLs in the JSON
        self.textView.text = [e.responseString stringByReplacingOccurrencesOfString:@"\" withString:@""];
    } else {
        [Utils handleErrorInResponse:e];
    }
}];

Note that the block will be called on the thread that the request was started on.

Example: APSUsers Login Call with Response Handler

The following example logs in an existing ACS user by their username and password. After a successful login, the application displays an alert indicating a successful login.

// Create dictionary of parameters to be passed with the request
NSDictionary *data = @{
   @"login": self.usernameField.text,
   @"password": self.passwordField.text
};

[APSUsers login:data withBlock:^(APSResponse *e) {
    // The block will be called on the thread the request was started on
    if (e.success) {
        NSString *userId = [[[e.response objectForKey:@"users"] objectAtIndex:0] objectForKey:@"id"];
        NSString *msg = [NSString stringWithFormat:@"Logged in! You are now logged in as %@", userId];
        [[[UIAlertView alloc] initWithTitle:@"Success" message:msg delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
    } else {
        [Utils handleErrorInResponse:e];
    }
}];

Monitoring Request Progress

For Cloud API methods that involve uploading large files, such as [APSPhotos create] or [APSFiles create], there is an overloaded version that takes an additional progressBlock parameter. This parameter is a code block that is periodically invoked and passed a float value indicating the progress of the request, and a boolean indicating if the request is for an upload (YES) or download (NO).

Example: APSPhotos Create Call with Progress Handler

The following example creates a new Photo object from a binary photo attachment. The progressBlock code block sets the progress property on a ProgressBar object, displaying the status of the upload.

NSDictionary *data = @{
    @"photo": self.photoData,
    @"photo_sync_sizes[]": @"small_240"
};
[APSPhotos create:data withBlock:^(APSResponse *e) {
    // The block will be called on the thread the request was started on
    if (e.success) {
        [[[UIAlertView alloc] initWithTitle:@"Success" message:@"Uploaded!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
        self.photoData = nil;
    } else {
        [Utils handleErrorInResponse:e];
    }
    self.createButton.hidden = NO;
} progressBlock:^(float progress, BOOL upload) {
    // The block will be called on the thread the request was started on
    self.progressBar.progress = progress;
}];

Making Generic REST APIs Method Calls

The [APSCloud sendRequest] method lets you easily make REST API calls directly against ACS, rather than using the specialized classes. In general, you should use the specialized classes as they provide an easier API. However, if new REST methods are deployed to the APS Cloud backend, this approach lets you immediately start using those methods without waiting for an update to the SDK.

To make a generic request, you call [APSCloud sharedInstance] to get a reference to the shared APSCloud object, and then call its sendRequest method. For each call, you must specify the following:

  • REST API method endpoint relative to "api.cloud.appcelerator.com/v1". Method endpoints are listed in the corresponding entries in the REST API documentation.
  • The HTTP method to use.
  • Data to send with the request.

For example, to create a post, pass the sendRequest() method the following information:

  • REST API method endpoint: posts/create.json
  • The HTTP method to use: POST
  • Data to send with the request: at minimum, you must specify the content property.

The following example calls users/login.json REST method directly and logs the result to the console.

NSDictionary *data = [NSDictionary dictionaryWithObjectsAndKeys:
                                    @"jalter", @"login",
                                    @"pass", @"password",
                                    nil];
[[APSCloud sharedInstance] sendRequest:@"users/login.json" method:@"POST" data:datahandler:^(APSResponse *e) {
    NSLog(@"SUCCESS: %hhd", e.success);
    NSLog(@"ERROR: %hhd", e.error);
    NSLog(@"MESSAGE: %@", e.message);
}];

Working with Push Notifications

The APSPushNotifications class lets your application subscribe, send and receive push notifications. To use this class, you must first do the following:

Once push services have been configured, and you've obtained a device token by registering your application to receive remote notifications, you can start calling methods of the APSPushNotifications class. For example, in the following example the user is subscribed to receive notifications from the channel named "friend_channel".

// Create dictionary of parameters to be passed with the request
NSDictionary *subscribeData = @{
            @"device_token": @"<DEVICE_TOKEN_STRING",
            @"channel": @"friend_channel"
            };
[APSPushNotifications subscribeToken:subscribeData withBlock:^(APSResponse *e) {
    if (e.success) {
        // Subscribed to channel
    } else {
        // Error subscribing to channel
    }
}];