Loading...

Implementing iOS-Apps with iCloud Support

Bachelor Thesis 2013 39 Pages

Computer Science - Programming

Excerpt

Contents

1 Introduction
1.1 What is Cloud Computing?
1.2 iCloud Overview

2 iCloud
2.1 Users view of iCloud
2.2 Developers view of iCloud
2.2.1 Testing & Debugging

3 Comparison iCloud with other Cloud Services

4 Saving data on the device
4.1 Property Lists
4.1.1 NSUserDefaults
4.1.2 Settings Bundle
4.1.3 Archiving Objects
4.2 Documents
4.2.1 File System
4.2.2 UIDocument
4.3 Database
4.3.1 SQLite
4.3.2 Core Data

5 Saving data in the iCloud
5.1 Key-Value Storage
5.2 Document Storage
5.2.1 Update Notification
5.2.2 Conflict Handling
5.3 Core Data Storage
5.3.1 Update Notification
5.3.2 Conflict Handling

6 Summary

Bibliography

List of Figures

List of Tables

List of Listings

List of Abbreviations

1 Introduction

The increasing proliferation of mobile devices and personal computers and the need to have all the personal or business information up to date means that data synchronization has to be as simple as possible. All big operating system vendors offer their own cloud solution, for instance Microsoft (Azure [1], SkyDrive [2]), Google (Google Drive [3]), Ubuntu (Ubuntu One [4]), and Apple (iCloud [5]). Each of those solutions works best with their own operating system, but could have some disadvantages, for instance missing features, on the other OS.

This paper uses the Apple environment and shows how you can build applications for iPhone or iPad, which save data into Apple’s cloud solution iCloud. The start of this paper provides an overview of cloud computing and iCloud, afterwords you will see the iCloud from the user view and the developer view. Before we can write iCloud applications, we have to know how data can be saved on the device, starting from simple key/value pairs, like user settings, to documents and databases. With this knowledge the application can be extended to save data to the iCloud. In this paper I will develop a simple location based application to demonstrate all of those possibilities of data storage on the device or in the iCloud. The last chapter shows how data can be exchanged without using the iCloud.

1.1 What is Cloud Computing?

Cloud Computing isn’t a new idea, but nowadays everybody is talking about the cloud. The main reason for the customer to use the cloud is, it makes it easy to exchange and synchronize the data of applications to all devices. John McCarthy already had already 1961 the vision of the future of time-sharing technology.[6]

"Cloud computing is a model for enabling ubiquitous, convenient, on-demand network access to a shared pool of configurable computing resources".[7] Those resources can be for instance servers, applications or storage. The NIST (National Institute of Standards and Technology) standard also describes different Service Models: Software as Service, Platform as a Service and Infrastructure as a Service.

Software as a Service (SaaS) means the application is running on the hardware of the provider and the consumer can use this services with a thin client, like a web browser, but also business applications. Typical SaaS applications are Facebook, Youtube, Google Apps. The main idea of Platform as a Service (PaaS) is to provide to the costumer a development framework or operating system, for instance Microsoft Azure. In the Infrastructure as a Service (IaaS) model the consumer can use the infrastructure of the provider, like storage, computation, networks and other computer resources.

illustration not visible in this excerpt

Figure 1.1 shows an overview of those three service models and some example existing appli­cations. You will find more detailed description about Cloud Computing here: [7] & [8]

1.2 iCloud Overview

Apple introduced iCloud on June 2011 as replacement of MobileMe. MobileMe was a fee- based cloud-service and included contacts, calendar, mail services, but also a storage solution

illustration not visible in this excerpt

Figure 1.1: Cloud services [8]

(iDisk). iCloud isn’t just a substitute, but an extension of the old service and includes some new features and application programming interfaces (API) for developers to build their own applications with integrated iCloud storage and data synchronization.

Now, iCloud is a free service - only some additionally features, like more storage, are fee-based. Apple describes iCloud on their homepage with the slogan "It’s the easiest way to manage your content. Because you don’t have to."[5] iCloud stores the user data, such as emails, photos, music, books, notes, documents in the cloud. In this case the cloud means many big data centres of Apple. You will find a complete list of the iCloud features on Apple’s homepage.[5] In addition to those Apple applications there are a lot of third party apps, which use the iCloud to store and synchronize their data. This paper provides the knowledge, how to develop this kind of application and shows how the data can be saved.

iCloud is fully integrated on mobile devices - these are iPhone, iPad and iPod touch - and desktop devices - all desktop Macs and MacBooks, running on OS X Lion or higher. The Apple TV and Computers with the Windows operating system also can use parts of the iCloud, like photos, music, emails. The main features of iCloud are also available via web browser.[9] According to the NIST standard of Cloud Computing iCloud is mainly a SaaS model, but in parts also a IaaS model.

illustration not visible in this excerpt

Figure 1.2: iCloud features [5]

2 iCloud

2.1 Users view of iCloud

To use iCloud the user has to setup the iOS device with an Apple ID. Many of the iCloud features are also integrated on Apple’s desktop operating system OS X, or will be integrated in upcoming versions.

After activating the iCloud the user can choose in the settings windows for any supported application, if the data will be saved in the iCloud or not. This settings for the apps developed by Apple are summarized in a iCloud page, for all other third party apps, there is a separate settings page with the name of the application. The data, which is saved in the iCloud, is automatically downloaded if needed to all registered devices and is also available, when the device does not have an Internet connection. Changed data will be uploaded once the connection is re-established. Normally, there are no other manual operations to do by the user, only if there exists different versions of the document. This problem arises, when the changed data cannot be uploaded to or downloaded from the iCloud immediately. Your application has to handle this synchronization conflict, for instance by asking the user which version should be saved.

iCloud is especially designed for Apple devices and operating systems, but there also exist a control panel for Microsoft Windows. With this control panel it is possible to synchronize the contacts, calendar entries, mails, notes and photos, but there are much more features, which cannot be used by the windows version of iCloud.

The main features of iCloud are also accessible via web browser from any, also on not supported operating systems [9].

illustration not visible in this excerpt

Figure 2.1: iCloud web page [9]

2.2 Developers view of iCloud

The vision of Apple for iCloud is a very simple and user friendly way to use it. But this means that the user doesn’t know or see how the data are organized and stored on the device and in the cloud. It doesn’t matter, there is no reason for the user to know that. As developer it is indispensable to know the internals of the iCloud storage.

There are three different types of iCloud storage: Key-Value storage, Documents storage and Database storage. Each of them is described in chapter 5 and are similar to the associated local storage types, described in chapter 4. In fact, the local storage and cloud storage uses many of the same mechanism and also the same classes.

As additional service Apple provides also an iCloud Developer web page. On this web page you can browse through the documents, which are saved in the iCloud. You will find your documents, databases and property list files, for applications, winch uses the iCloud document storage.

illustration not visible in this excerpt

Figure 2.2: iCloud Developer web page [10]

iCloud is one of the major features of iOS 5 and the users, who are using this feature are growing every day. But not all devices are supporting this version of iOS. If you plan to purchase your application on iPhone 3G or older, make sure your application checks if iCloud is supported or not.

2.2.1 Testing & Debugging

For testing your iCloud solution you need a real device. The simulator doesn’t support iCloud (yet). The first step to deploy the application on the device or in the AppStore is to create a provisioning profile and an AppID with activated iCloud feature on Apple’s developer web page. Then you have to set in your Xcode project the entitlements for the target and enable iCloud for the Key-Value Storage and/or the Ubiquity Containers. A Ubiquity Container is the storage for the iCloud documents and databases on your device.

However, for debugging of all the iCloud features in your application it might be useful to have a second device to test the synchronization and conflict handling. For provoking version conflicts turn on the flight modus on one device, change some data on two devices. After you deactivated the flight modus check if your handling works as you have designed for your application.

Moreover, you also have to be patient, because the iCloud synchronization needs some time. Sometimes it is not easy to find out whether the application has an error or there exists other inconsistency in your ubiquity container on the basis of unresolved conflicts or changed document types.

3 Comparison iCloud with other Cloud Services

Besides iCloud there also exist many other cloud solutions with an API for iOS. Why should you use iCloud instead for example Dropbox? The main reason pro iCloud is the full integration in Apple’s mobile devices with iOS. During the first setup of a new device such as iPhone or iPad the user is asked if he wants to use the iCloud. The easy setup and usage of iCloud results in more than 150 million iCloud user accounts (April 2012) [11]

Nevertheless, there is a point that speaks against iCloud. The integration in other operating system doesn’t exist. If you need this integration, you cannot use iCloud and you have to use an other cloud solution instead or additionally. The table 3.1 shows a comparison of some frequently used cloud solution, which provides an API for iOS. On the developer websites you will find the API for iOS including the documentation and some example source code.

illustration not visible in this excerpt

Table 3.1: Cloud API Comparison

Instead of using a cloud solution you can use the old, but frequently used standards for file transfer through the internet: FTP and WebDAV. But this means you have to offer a server with those protocols or let the user configure their own server in your application. Many popular applications such as GoodReader have implemented almost all major cloud systems in their applications.

If you plan to use an other cloud solution in your application, you also should think about adding iCloud support, because the integration for this support doesn’t mean much additional work when using the right components of the iOS frameworks.

4 Saving data on the device

Applications have to store various kinds of data, beginning from simple user settings until documents and databases. Those data can be stored on the device or in the cloud. This chapter describes the basics, how to store data on the device and what is necessary for understanding the iCloud storage methods.

4.1 Property Lists

Many applications need some preferences, for instance default values or some user specific settings. In iOS you can use the class NSUserDefaults to store your preferences on the device. NSUserDefaults allows only scalar values like boolean, integer, float or double and the following data types:

NSData, NSString, NSNumber, NSDate, NSArray, NSDictionary

illustration not visible in this excerpt

Table 4.1: Property list types and their representations [12]

Those types are the standard property list object types. Property lists save the data with a name and a list of values. Each of the property list can include different object types. This means you can save a text with NSString and a array of numbers with NSArray, and so on. Property lists are serializable and deserializable, so it is possible to store the list on the file system in XML notation. The conversion from an object to XML and back is very efficient for small pieces of data. In most cases there is no need to save the property list directly to a file in your application. The main usage is to save them in the user defaults with the NSUserDefaults class.

Apple advises to use property lists for data not much more than a few hundred kilobytes [12] (Data Management - Property List Programming Guide - About Property Lists). If your application saves those settings to the iCloud, you have to note the key-value storage of the iCloud is also limited, see section 5.1 on page 23.

Property lists are also used in Xcode for the metadata of your application. Other, more complex, object types have to implement the NSCoding protocol, so the archiver can encode and decode the data. After encoding with an archiver this data can be stored as user defaults.

4.1.1 NSUserDefaults

Loading the user defaults data is very simple. You only need an instance of the NSUserDe­faults, then you can access to every item in the property list with its name and object type. The following listing shows how to load a boolean value into to the property iCloud, which is a boolean value. The NSLog command is only to demonstrate if this loading works.

illustration not visible in this excerpt

Listing 4.2: Save UserDefaults

You only have to set the value and its key name into an instance of NSUserDefaults. Depend­ing on the property list type of the object, there exists several methods for setting or getting the data, for example: boolForKey or setBool, stringForKey / setString. iOS automatically stores those values to the disk by calling the synchronize message, but you also can send this message to store the data immediately. This is necessary for instance if your application is closing and you want save the latest changes. Figure 4.1 shows the user defaults property list, which is used in the listings 4.1 and 4.2.

4.1.2 Settings Bundle

Those messages load and save data without any graphical user interface and you can use these messages in any part of your application. However, if you want to give the user a settings window in your application, you need a settings bundle in your Xcode project. The settings bundle is a property list file. In this plist file you can configure one or more items as text field, slider or toggle switch. Xcode helps you to configure this property list. The settings page will be automatically created when the application is installed.

illustration not visible in this excerpt

Figure 4.1: Settings Bundle

The settings window looks like figure 4.2 on an iPhone. You should use this mechanism only for settings which are normally not often changed by the user, because it isn’t very comfortable for the user to leave the application and go to the settings window.

illustration not visible in this excerpt

Figure 4.2: Settings Window

After changing the user defaults in the settings window, your application is starting with the actual values after executing the loadUserDefaults message. But what happens if your application is currently started and in the background? The next time you send this message you have to changed values. To inform about changes you have to implement an observer for NSUserDefaultsDidChangeNotification. A good place for this observer is didFinishLaunching- WithOptions message in your AppDelegate class or in viewDidLoad of your view controller class. The selector defaultsChanged is a method, in which you can update your user defaults in the application.

illustration not visible in this excerpt

Listing 4.4: myapplication.plist

4.1.3 Archiving Objects

What about any other objects, which are not a property list? If you want to store those objects, you need a serialized representation of them. There are two ways to generate this serialized representation: archives and serializations. Both variants create an architecture-independent stream, which can be written to the file system. The difference is that archives provide more detailed information, for instance naming values. Archives can store Objective-C objects, but also Java objects. [12] (File Management - Archives and Serialization Programming Guide)

A coder object is needed to encode and decode the object data into/from a byte stream. This stream can be stored on the file system or in the property list. Stored byte streams can be decoded into an object. Those objects have to implement the NSCoding protocol, which requires two messages: "Initializing with a Coder" and "Encoding with a Coder".

illustration not visible in this excerpt

Listing 4.5: Initializing with a Coder

illustration not visible in this excerpt

Listing 4.6: Encoding with a Coder

There exist several methods for encoding / decoding the data, depending on the object type, for instance encodeObject, encodeFloat and decodeObject, decodeFloat.

How the data of the objects are encoded or decoded is the task of the coder object. NSCoder is an abstract class. Therefore we need a concrete subclass for the coder. There are three types of archives: sequential, keyed and distributed archives.

Sequential archives read and write data sequentially and you have to use the same order for reading and writing in your messages. Keyed archives use naming values, like property lists, and are accessible directly by the key. The last type is if you have distributed objects.

However, only the keyed archives are interesting for us, because sequential archives only exists in older versions of Mac OS X and are deprecated. If we have prepared our class with the NSCoding protocol, it is very simple to store and load this object to and from the user defaults.

The class-method archivedDataWithRootObject, shown in listing 4.7, is serializing the object lastSavedLocation and the result will be saved into a NSData instance. Now the new data object can be saved with the known messages.

illustration not visible in this excerpt

Listing 4.7: Saving an object to UserDefaults

[...]

Details

Pages
39
Year
2013
ISBN (eBook)
9783656381785
ISBN (Book)
9783656382713
File size
928 KB
Language
English
Catalog Number
v210118
Institution / College
University of Applied Sciences Technikum Vienna – Informations- und Kommunikationssysteme
Grade
1
Tags
iCloud iPhone iPad iOS

Author

Share

Previous

Title: Implementing iOS-Apps with iCloud Support