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, Cloud and Performance services. Note that the Appcelerator Test service does not currently work with Swift projects.
You should be familiar with developing native iOS applications using Xcode and Swift. The SDKs supports the following base SDK and target iOS versions:
|Maximum SDK version||Minimum SDK version||Maximum target iOS version||Minimum target iOS version|
Before you can use Appcelerator Services in your application you need to:
- Create an application in Dashboard
- Download the SDK
- Get the APS application key
To register an Android Java, or iOS Objective-C or Swift application in Dashboard:
- Log in to Appcelerator Dashboard.
- Click the Add menu (+) and select Register App for Services to open the Register App for Services form.
- In the Register App for Services form, enter an application Name, and select a Platform and Category. Optionally, you can provide a custom icon to display in Dashboard for your application, and a description. To add application team members from the organization, click the add (+) button in the Members form at right.
- Click OK.
Dashboard displays the Platform Services tab for your application. In the tab, you can download the APS SDK, and get code snippets to copy and paste into your application.
For more information, refer to Managing Non-Titanium Client Applications in Dashboard.
The following tutorial demonstrates basic setup and usage of Analytics, Cloud, and Performance libraries in a Swift Xcode project. To complete the tutorial you will need your APS application key for the Cloud, Analytics, and Performance services. Download a complete version of the project.
To create a basic application using APS:
- In Xcode, create a new iOS Project (Single View Application). When prompted for options, select Swift in the Language drop-down.
Create a Header File and name it
<PROJECT_NAME>-Bridging-Header.h. Make sure the main project's application target folder is selected, then from the menu, select File > New > File... or drag a Header File from the File Template library to the application target folder. The application target folder is a folder under the root project folder and will be named the same as the project if the name has not been changed.
appcelerator-sdk-ios-<VERSION>.zipfile and drag the
Appcelerator.frameworkfolder into the root project folder.
- Select Copy items into destination… and click Finish.
- Select your project in the Project navigator and click the Build Phases tab.
- Expand the Link Binary With Libraries section and add the
libz.tbdframeworks. Prior to Xcode 7, the framework extension is
- Click the Build Settings tab, then click the All button in the top-left corner of the tab.
- If you are using Xcode 7, you will need to disable bitcode since some third-party libraries do not have bitcode enabled. Expand the Build Options section and set Enable Bitcode to No.
- Expand the Linking section and add
-ObjCto Other Linker Flags .
Expand the Swift Compiler - Code Generation section (near the bottom) and add the bridging header file (with the relative path from the root folder) to Objective-C Bridging Header, for example, <
Open the bridging header file and add the following import statement:
Open the application delegate file (
AppDelegate.swift) and add the following initialization code to the
The iOS application is now ready to make method calls using the APS SDK APIs.
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.
- Open your project's
- Drag a Picker View, Text Field and Button from the Object library to the view.
- 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.)
- In the right pane, display the
- Control-drag the Picker View to the View Controller icon at the top of the view. Select dataSource. Repeat for the delegate option.
- Drag the Picker View to the header file to create an IBOutlet. Name the outlet
pickerand click Connect. Repeat the same procedure for the Text Field and name it
Declare the View Controller to implement the
Add a Dictionary array called
usernamesto keep track of the Picker View's data source and a String variable called
usernameto reference the current selection.
Create an IBAction callback for the button named
doClick. In the following sections, you will add code to this handler that calls Cloud, Analytics and Performance services.
Implement the methods of
UITextFieldDelegateprotocols. Add the following code to the file:
- Control-drag the Button to the
onClick()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. There are two types of custom events: feature events and navigation events.
doClick method, add a
APSAnalytics.sharedInstance().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.
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
APSUsers.query() method to retrieve a list of user accounts from inside the
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.
Login to a Cloud Account
To login 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
APSUsers.login() method. Modify the
doClick method to login to a Cloud user account.
Set a Username for Crash Logs
To help differentiate crash logs, use the u
sername property. When the application successfully logs in to the Cloud user account, the application sets the u
Testing the Tutorial Sample
Before testing the sample, you need to create user accounts for the application to query. To create Cloud user accounts:
- Log in to Appcelerator Dashboard.
- In Dashboard, select your application from the APIs menu in the upper-left corner.
- In the left navigation bar, click Manage Data.
- Click Users, then the Create User button.
- In the Username field enter the user's username.
- In the Password field enter the new user's password (four-character minimum).
- Click Save Changes.
After you have created a few Cloud user accounts, build the sample and launch it on either a device or emulator.
Once the application launches:
- Click on the Picker. You should see a list of all the Cloud user accounts you added.
- Select a user account from the Picker and enter that user's password. Click the Button. In the log output, you should see an info log that login command was successful or not.
- Go back to the Appcelerator Dashboard.
- In Dashboard, select your application from the Apps menu in the upper-left corner.
- In the left navbar, select Analytics.
- In the Real Time view, you should see at least one active session.
- In the left navbar, click Events. You should see the "sample.feature.login" feature event.
Next Steps for Appcelerator Analytics, Cloud and Performance
This tutorial only covers a small portion of what the Analytics, Cloud and Performance services can provide. For more in-depth information about these features, see the following topics. Note that these guides were written for Objective-C projects. You will need to adapt the API calls for Swift.