Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Warning
titlePro or Enterprise Subscription Required

This AMPLIFY Appcelerator Services feature requires a Pro or Enterprise Subscription. 

Table of Contents

Introduction

This guide walks through the setup of the AMPLIFY Appcelerator Services for iOS applications. The AMPLIFY Appcelerator Services SDK gives you access to the Appcelerator Analytics and Cloud services. To enable the Appcelerator Test for a project, run the appceletator-test utility against either the project or the IPA file.

Panel
titleNot developing a native iOS application with Objective-C?

See the following topics to use the AMPLIFY Appcelerator Services on other platforms:

Section
Column

For native Android applications built with Java, see Quick Start Guide for Android APS SDK.

Section
Column

For native iOS applications built with Swift, see Quick Start Guide for iOS APS SDK using Swift.

Section
Column

For Titanium Applications, see Quick Start.

Requirements

You should be familiar with developing native iOS applications using Xcode and Objective-C. The SDKs supports the following base SDK and target iOS versions:

Maximum SDK versionMinimum SDK versionMaximum target iOS versionMaximum target iOS version
9.0.x8.0.x9.0.x7.1.x

Setup

Before you can use Appcelerator Services in your application you need to:

  • Create an application in Dashboard
  • Download the SDK and appcelerator-test utility
  • Get the application keys

Multiexcerpt include
nopaneltrue
MultiExcerptNamecreatenativeapp
PageWithExcerptManaging Non-Titanium Client Applications in Dashboard

Dashboard displays the Platform Services tab for your application. In the tab, you can download the APS SDK and appcelerator-test utility, and get code snippets to copy and paste into your application.

For more information, refer to Managing Non-Titanium Client Applications in Dashboard.

Quick Tutorial

The following tutorial demonstrates the basic setup and usage of Analytics and Cloud libraries in an Xcode project. To complete the tutorial, you will need your APS application key for the Cloud and Analytics services. Download a complete version of the project.

To create a basic application using APS:

  1. In Xcode, create a new iOS Project (Single View Application).
  2. Unzip appcelerator-sdk-ios-<VERSION>.zip file and drag the Appcelerator.framework folder into the root project folder if you are using Xcode 6 and above, or the Frameworks folder if you are using Xcode 5 or below. 
  3. Select Copy items into destination… and click Finish.
  4. Select your project in the Project Navigator and click the Build Phases tab.
  5. Expand the Link Binary With Libraries section and add the libsqlite3.tbd and libz.tbd frameworks. Before Xcode 7, the framework extension is dylib rather than tbd.
  6. Click the Build Settings tab, then click the All button in the top-left corner of the tab.
  7. If you are using Xcode 7, you will need to disable bit code since some third-party libraries do not have bit code enabled. Expand the Build Options section and set Enable Bitcode to No.
  8. Expand the Linking section and add -ObjC  to Other Linker Flags.
  9. In your application delegate implementation file, import Appcelerator/Appcelerator.h.

    Code Block
    #import <Appcelerator/Appcelerator.h>
  10. Add the following initialization code to your application delegate's application:didFinishLaunchingWithOptions method, replacing the placeholder string with your APS application key:

    Code Block
    [[APSServiceManager sharedInstance] enableWithAppKey:@"APS_APP_KEY"];

    The iOS application is now ready to make method calls using the APS SDK APIs.

Warning

If the application attempts to make APS SDK API calls without first setting the application key, the application will throw an exception and crash.

Modify the Application

Customize the application's UI to display a picker, text field and button, and add some logic to respond to user interaction. The picker will display a list of available user accounts. The user can enter their password in the text field, then click the button to log in.

  1. Open your project's Main.storyboard file.
  2. Drag a Picker View, Text Field, and Button from the Object library to the view.
  3. Open the Assistant Editor. (From the menu, select View > Assistant Editor Show Assistant Editor or click the Assistant Editor button in the top-right corner.)
  4. In the right pane, display the ViewController.h file.
  5. Control-drag the Picker View to View Controller icon at the bottom of the storyboard. Select dataSource. Repeat for the delegate option.
  6. Drag the Picker View to the header file to create an IBOutlet. Name the outlet picker and click Connect. Repeat the same procedure for the Text Field and name it textField.
  7. In the ViewController.h file, declare the View Controller to implement the UIPickerViewDelegateUIPickerViewDataSource, and UITextFieldDelegate protocols. Add an NSArray property called usernames to keep track of the Picker View's data source and an NSString property called username to reference the current selection. The final header file should look like: 

    Code Block
    titleViewController.h
    #import <UIKit/UIKit.h>
    #import <CoreLocation/CoreLocation.h>
    @interface ViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate>
    
    @property (weak, nonatomic) IBOutlet UIPickerView *picker;
    @property (weak, nonatomic) IBOutlet UITextField *textField;
    @property (strong, nonatomic) NSArray *usernames;
    @property (strong, nonatomic) NSString *username;
    
    @end 
  8. In the ViewController.m file, create an IBAction for the button named doClick. In the following sections, you will add code to this handler that calls Cloud and Analytics services. The View Controller needs to implement the methods of UIPickerViewDelegateUIPickerViewDataSource and UITextFieldDelegate protocols. Add the following code to the file:

    Code Block
    titleViewController.m
    #import "ViewController.h"
    // Add this import statement
    #import <Appcelerator/Appcelerator.h>
     
    @implementation ViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
     
        // Add these statements to dismiss the keyboard 
        self.textField.delegate = self;
        [self.textField resignFirstResponder]; 
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
    }
     
    // Add these methods
    - (IBAction)onClick:(id)sender {
    
    }
     
    #pragma mark Picker DataSource/Delegate
    
    - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
        return 1;
    }
    
    - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
        return _usernames.count;
    }
    
    - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
        return [_usernames[row] objectForKey:@"username"];
    }
    
    - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
        _username = [_usernames[row] objectForKey:@"username"];
    }
     
    #pragma mark TextField Delegate
    
    - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
    {
        return YES;
    }
    
    - (BOOL)textFieldShouldReturn:(UITextField *)textField
    {
        [textField resignFirstResponder];
        return YES;
    }
     
    @end
  9. Control-drag the Button to the onClick:sender method to create an action connection.

Send an Analytics Feature Event

The Analytics library automatically captures and sends application life-cycle events to the backend Analytics service. You can also send custom analytics events, as shown in this example. You can use feature events as one type of custom events.

In the doClick method, add a sendAppFeatureEvent: method call to send a feature event with the string "sample.feature.login". The optional payload parameter is set to nil for this example, but it lets you send additional data along with the event.

Code Block
- (IBAction)doClick:(id)sender {
	// Call Analytics method
	[[APSAnalytics sharedInstance] sendAppFeatureEvent:@"sample.feature.login" payload:nil];
}

Query Cloud Users

To use the APS Cloud component, most of the methods require a user to be logged in. This sample uses the Picker View to select a Cloud user account. To populate the Picker View values, the application needs to retrieve a list of users. Call the query:withBlock: method to retrieve a list of user accounts from inside the viewDidLoad method.

Every APS Cloud method includes a withBlock parameter that specifies the callback to handle the server response. The callback is passed an APSResponse object that contains response metadata (such as success or failure) and the response payload.

Code Block
titleViewController.m
- (void)viewDidLoad
{
    [super viewDidLoad];
 
    // Call the APSUsers query method
    [APSUsers query:nil withBlock:^(APSResponse *e){
        if (e.success) {
            _usernames = [e.response objectForKey:@"users"];
            [self.picker reloadAllComponents];
        } else {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                            message:@"Unable to retrieve user accounts!"
                                                           delegate:nil
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles: nil];
            [alert show];
        }
    }];
}

Log in to a Cloud Account

To log in to a Cloud account, you need the username and password. Since the application was modified to get all available user accounts and populate the Picker View, the application needs to get the current value of the picker and the text entered in the Text Field. These values are passed to the login:withBlock: method. Modify the doClick method to login to a Cloud user account.

Code Block
titleViewController.m
- (IBAction)doClick:(id)sender {
	[[APSAnalytics sharedInstance] sendAppFeatureEvent:@"sample.feature.login" payload:nil];


    // Construct the request parameters
    NSDictionary *params = [[NSDictionary alloc]
                                 initWithObjectsAndKeys:_username,@"login",
                                 [_textField text],@"password",
                                 nil];
 
    // Call the APSUsers login method
    [APSUsers login:params withBlock:^(APSResponse *e){
        if (e.success) {
            NSLog(@"Successfully logged in as %@", _username);

        } else {
            NSLog(@"ERROR: Failed to log in!");
        }
    }];
} 

Log a Handled Exception

The Performance library automatically logs application crashes (unhandled exceptions) and handled exceptions to the backend Performance service. You can also leave breadcrumbs in your application, which are developer-defined text strings (up to 140 characters) that are analogous to log messages.

For example, you can replace the Log calls in the catch statements with logHandledException calls. Instead, the application will generate a runtime exception, and then call the logHandledException method to log that exception to the Performance backend. To the doClick method, add the following new code: 

Code Block
- (IBAction)doClick:(id)sender {

    // Analytics call...
    // Cloud call...
    @try {
        [NSException raise:NSGenericException format:@"Something happened..."];
    } @catch (NSException *exception) {
        [[APSPerformance sharedInstance] logHandledException:exception];
    }
}

Set a Username for Crash Logs

To help differentiate crash logs, use the username property. When the application successfully logs in to the Cloud user account, the application sets the username property.

Code Block
titleViewController.m
- (IBAction)doClick:(id)sender {

    [APSUsers login:params withBlock:^(APSResponse *e){
        if (e.success) {
            NSLog(@"Successfully logged in as %@", _username);
            // Add this APS Performance method call
            [[APSPerformance sharedInstance] username] = _username;
        } else {
            NSLog(@"ERROR: Failed to log in!");
        }
    }];

} 

Multiexcerpt include
nopaneltrue
MultiExcerptNametesting
PageWithExcerptQuick Start Guide for Android APS SDK

Next Steps for Appcelerator Analytics and Cloud

This tutorial only covers a small portion of what the Analytics and Cloud services can provide. For more in-depth information about these features, see the following topics: