Cloud-enabling Your Apps

0
77
6 min read

(For more resources related to this topic, see here.)

Which cloud services can you use with Titanium?

Here is a comparison of the services offered by three cloud-based providers who have been proven to work with Titanium:


Appcelerator Cloud Services

Parse

StackMob

Customizable storage

Yes

Yes

Yes

Push notifications

Yes

Yes

Yes

E-mail

Yes

No

No

Photos

Yes

Yes

Yes

Link with Facebook/Twitter account

Yes

Yes

Yes

User accounts

Yes

Yes

Yes

The services offered by these three leading contenders are very similar. The main difference is the cost. Which is the best one for you? It depends on your requirements; you will have to do the cost/benefit analysis to work out the best solution for you.

Do you need more functionality than this? No problem, look around for other PaaS providers. The PaaS service offered by RedHat has been proven to integrate with Titanium and offers far more flexibility. There is an example of a Titanium app developed with RedHat Openshift at https://openshift.redhat.com/community/blogs/developing-mobile-appsfor-the-cloud-with-titanium-studio-and-the-openshift-paas

It doesn’t stop there; new providers are coming along almost every month with new and grand ideas for web and mobile integration. My advice would be to take the long view. Draw up a list of what you require initially for your app and what you realistically want in the next year. Check this list against the cloud providers. Can they satisfy all your needs at a workable cost? They should do; they should be flexible enough to cover your plans. You should not need to split your solution between providers.

Clouds are everywhere

Cloud-based services offer more than just storage.

Appcelerator Cloud Services

Appcelerator Cloud Services ( ACS) is well integrated into Titanium. The API includes commands for controlling ACS cloud objects.

In the first example in this article we are going to add commentary functionality to the simple forex app. Forex commentary is an ideal example of the benefits of cloud-based storage where your data is available across all devices. First, let’s cover some foreground to the requirements.

First, let’s cover some foreground to the requirements.

The currency markets are open 24 hours a day, 5 days a week and trading opportunities can present themselves at any point. You will not be in front of your computer all of the time so you will need to be able to access and add commentary when you are on your phone or at home on your PC. This is where the power of the cloud really starts to hit home. We already know that you can create apps for a variety of devices using Appcelerator. This is good; we can access our app from most phones, but now using the cloud we can also access our commentary from anywhere. So, comments written on the train about the EURUSD rate can be seen later when at home looking at the PC.

When we are creating forex commentary, we will store the following:

  • The currency pair (that is EURUSD)

  • ‹ The rate (the current exchange rate)

  • The commentary (what we think about the exchange rate)

We will also store the date and time of the commentary. This is done automatically by ACS. All objects include the date they were created.

ACS allows you to store key value pairs (which is the same as Ti.App.Properties), that is AllowUserToSendEmails: True, or custom objects. We have several attributes to our commentary post so a key value pair will not suffice. Instead we will be using a custom object.

We are going to add a screen that will be called when a user selects a currency. From this screen a user can enter commentary on the currency.

Time for action – creating ACS custom objects

Perform the following steps to create ACS custom objects:

  1. Enable ACS in your existing app. Go to tiapp.xml and click on the Enable… button on the Cloud Services section. Your project will gain a new Ti.Cloud module and the ACS authentication keys will be shown:

  2. Go to the cloud website, https://my.appcelerator.com/apps, find your app, and select Manage ACS. Select Development from the selection buttons at the top.

  3. You need to define a user so your app can log in to ACS. From the App Management tab select Users from the list on the right. If you have not already created a suitable user, do it now.

  4. We will split the functionality in this article over two files. The first file will be called forexCommentary.js and will contain the cloud functionality, and the second file called forexCommentaryView.js will contain the layout code. Create the two new files.

  5. Before we can do anything with ACS, we need to log in. Create an init function in forexCommentary.js which will log in the forex user created previously:

    function init(_args) { if (!Cloud.sessionId) { Cloud.Users.login({ login: 'forex', password: 'forex' }, function (e) { if (e.success) { _args.success({user : e.users[0]}); } else { _args.error({error: e.error}); } }); }

    This is not a secure login, it’s not important for this example. If you need greater security, use the Ti.Cloud.Users. secureLogin functionality.

  6. Create another function to create a new commentary object on ACS. The function will accept a parameter containing the attribute’s pair, rate, and commentary and create a new custom object from these. The first highlighted section shows how easy it is to define a custom object. The second highlighted section shows the custom object being passed to the success callback when the storage request completes:

    function addCommentary(_args) { // create a new currency commentary Cloud.Objects.create({ classname: className, fields: { pair: _args.pair, rate: _args.rate, comment: _args.commentary } }, function (e) { if (e.success) { _args.success(e.forexCommentary[0]); } else { _args.error({error: e.error}); } }); }

  7. Now to the layout. This will be a simple form with a text area where the commentary can be added. The exchange rate and currency pair will be provided from the app’s front screen. Create a TextArea object and add it to the window. Note keyboardType of Ti.UI.KEYBOARD_ASCII which will force a full ASCII layout keyboard to be displayed and returnKeyType of Ti.UI.RETURNKEY_DONE which will add a done key used in the next step:

    var commentary = Ti.UI.createTextArea({ borderWidth:2, borderColour:'blue', borderRadius:5, keyboardType: Ti.UI.KEYBOARD_ASCII, returnKeyType: Ti.UI.RETURNKEY_DONE, textAlign: 'left', hintText: 'Enter your thoughts on '+thePair, width: '90%', height : 150 }); mainVw.add(commentary);

  8. Now add an event listener which will listen for the done key being pressed and when triggered will call the function to store the commentary with ACS:

    commentary.addEventListener('return', function(e) {forex.addCommentary({ pair: thePair, rate: theRate, commentary: e.value}) });

  9. Finally add the call to log in the ACS user when the window is first opened:

    var forex = require('forexCommentary'); forex.init();

  10. Run the app and enter some commentary.

What just happened?

You created functions to send a custom defined object to the server. Commentary entered on the phone is almost immediately available for viewing on the Appcelerator console (https://my.appcelerator.com/apps) and therefore available to be viewed by all other devices and formats.

Uploading pictures

Suppose you want to upload a picture, or a screenshot? This next example will show how easy it is to upload a picture to ACS.

LEAVE A REPLY

Please enter your comment!
Please enter your name here