Type Library Revision 2017.3060 Keywords iCloud, sync, storage Platforms iOS, tvOS, macOS
The iCloud plugin enables access to Apple's iCloud APIs and provides instruments to synchronize data across a user's devices.
While iCloud is part of the iOS operating system, users may not log in with their iCloud account, or a user may choose to disable iCloud entirely. Thus, it's important that users can interact with your app even if iCloud is not enabled.
iCloud provides three distinct ways of storing and sharing data:
Key-Value Storage (KVS) is the easiest aspect of iCloud to use and Apple recommends that all apps benefit from its implementation. Essentially, it provides the ability to associate data (values) with keys (strings). These
Note that storage space for KVS is strictly limited. You can declare a maximum of 1024 keys and the total size of all values should be less than 1 MB. Keys should be less than 64 symbols (bytes). You may opt to store all data in a single key with a large set of data or divide the data into multiple keys — simply note that if a
Documents in iCloud is designed to synchronize files across a user's
Note that the storage space for Documents in iCloud is limited to the user's iCloud Drive space. Also be aware that the user can manually delete specific stored files via the iOS settings screen, so your app should not assume that a certain file exists in iCloud storage.
CloudKit is the most recent addition to the iCloud framework. This is a distributed database where you can store records either privately and publicly. Private records can only be accessed by the user who created them, while public records are shared between users. Essentially, CloudKit is a very powerful tool for storing data as well as sharing data between users.
Note that CloudKit storage space "scales with your users" but, in general practice, your app should not exceed the free storage limit unless there's a dedicated need for extensive database usage.
To use this plugin, add an entry into the plugins
table of build.settings
. When added, the build server will integrate the plugin during the build phase.
settings = { plugins = { ["plugin.iCloud"] = { publisherId = "com.coronalabs" }, }, }
Using iCloud requires certain settings within your app's provisioning configuration. You can not use a wildcard App ID with iCloud, so you must configure and use an explicit one (guide). To configure iCloud with your app, follow these basic steps:
Log into the Apple Developer portal, enter the Member Center, and navigate to the
Click on Identifiers under the desired platform. There you should see all of your apps listed. Select the app for which you want to add iCloud support and, below the list of services, click Edit.
In the list of services, select the iCloud checkbox.
Generate
In addition to the steps above, if you want to use Documents in iCloud or CloudKit, you must associate an iCloud Container with the app as follows:
In the Apple Developer portal, from the same section reached in the steps above, select
In the same row, click the Edit button. Here you can create a new iCloud Container or associate an existing iCloud Container with the app. If you are creating an iCloud Container for the first time, note that you'll need to create/register it first, then repeat this step to associate the new container with the app and iCloud.
Generate
-- This simple example counts user taps across devices and keeps a -- synchronized counter in the cloud using Key-Value Storage (KVS) local iCloud = require( "plugin.iCloud" ) -- Create text object for text readout local text = display.newText( "iCloud", display.contentCenterX, display.contentCenterY, nil, 20 ) -- Set/reset the text readout local function setText() text.text = tostring( iCloud.get( "taps" ) ) end -- KVS listener function local function listenKVS( event ) setText() end -- Set the KVS listener iCloud.setKVSListener( listenKVS ) -- Set the initial text readout setText() -- Tap listener function local function globalTap( event ) local tapCount = 1 + ( iCloud.get( "taps" ) or 0 ) iCloud.set( "taps", tapCount ) iCloud.synchronize() setText() end -- Detect taps in Runtime Runtime:addEventListener( "tap", globalTap )