Enabling CloudKit in Your App
CloudKit is an app service available only to apps distributed through the App Store and Mac App Store. CloudKit requires additional configuration in your Xcode project. Your app must be provisioned and code signed to access CloudKit. To avoid code signing issues, enable CloudKit using the Capabilities pane in Xcode. There should be no need for you to edit entitlements directly in Xcode or Member Center.
在你的應用中安裝CloudKit
CloudKit是一個通過app Store和Mac app Store下載的應用程序的服務。CloudKit 需要在Xcode項目中進行額外的配置。你的App必須配置好並用代碼訪問CloudKit。爲了避免代碼簽名問題,使用Xcode中功能面板啓用CloudKit。你應該沒有必要直接需要權利在Xcode或會員中心進行編輯。
About Containers and Databases
Multiple apps and users have access to iCloud, but data is segregated and encapsulated in partitions called containers. The containers belonging to your apps cannot be accessed by apps from another developer. However, your apps can share containers. Multiple apps can share the same container, and one app can use multiple containers. There’s one default container per app, but you can create additional custom containers. The identifier for the default container matches the app’s bundle ID. The other container IDs you specify need to be unique across all developer accounts. Containers can’t be deleted.
Setup 設置步驟
To perform all the steps in this document, you need: 要實現此文檔中的所有步驟,你需要:
-
A Mac computer with Xcode 6 or later installed 有Xcode6及之後版本的Mac
-
For the best experience, the latest OS X and Xcode releases installed 最好是最新的OSX和Xcode
-
An Xcode project that builds without errors沒有錯誤地構建一個Xcode項目
-
Membership in either the iOS Developer Program or the Mac Developer ProgramiOS或Mac應用開發者會員,有開發者帳號
-
Permission to create code signing and provisioning assets in Member Center
允許創建代碼簽名和在會員中心配置資產
Verify that you have performed these tasks before you begin using CloudKit. For step-by-step instructions, read App Distribution Quick Start.
|
Task |
---|---|
|
Join the iOS Developer Program or Mac Developer Program. |
|
Create an Xcode project that builds and runs. |
|
Add your Apple ID to Accounts preferences. |
|
Create your team provisioning profile:
|
|
For Mac apps, enable App Sandbox in the Capabilities pane. |
Enable iCloud and Select CloudKit 啓用iCloud並選擇CloudKit
CloudKit is one of three app services provided by iCloud. The other iCloud app services—key-value storage and iCloud documents—also appear in the iCloud settings in Xcode. To use CloudKit, you first enable iCloud and then select the CloudKit service.
Important: When you select CloudKit, Xcode creates a default container ID based on the bundle ID. Because you can’t delete iCloud containers, verify that your bundle ID is correct in the General pane in Xcode before selecting CloudKit. To change your bundle ID, read Set the Bundle ID inApp Distribution Quick Start.
To enable iCloud and select CloudKit 啓動iCloud然後選擇CloudKit
-
In the Capabilities pane, select the switch in the iCloud row.
Xcode provisions your app to use iCloud. (Key-value storage is enabled by default.) 在功能窗格中,打開iCloud的開關(鍵-值存儲模式會默認打開)
-
Select the CloudKit checkbox.
Xcode creates a default CloudKit container based on the bundle ID and adds the CloudKit framework to your project. 選擇CloudKit複選框,Xcode會創建基於包ID的默認CloudKit數據庫並且將CloudKit framework添加到你的項目中
Your app can now store data and documents in iCloud.
Access CloudKit Dashboard
Use CloudKit Dashboard to manage your CloudKit container schema and records. The schema describes the organization of records, attributes, and relationships in a database. A record is an instance of a record type. In a relational database, a record type corresponds to a table and a record corresponds to a row in a table.
訪問CloudKit儀表板
使用CloudKit儀表板來管理您的CloudKit容器的模式和記錄。該模式描述了數據庫的記錄,屬性和關係的組織。記錄是一個記錄類型的實例。在關係數據庫中,記錄類型對應於一個表 ,一個記錄對應於一個表中的行。
To sign in to CloudKit Dashboard
-
In the iCloud settings in the Capabilities pane, click CloudKit Dashboard.
Alternatively, go to https://icloud.developer.apple.com/dashboard.
-
If necessary, enter your Apple ID credentials and click Sign In.
All the containers for all the teams you belong to appear in the container pop-up menu in the upper-left corner of the window. The assets for the selected container (named Gallery in the screenshot) are displayed.
1.在功能窗格中的iCloud設置,點擊CloudKit儀表板。
或者,去https://icloud.developer.apple.com/dashboard。
2.如果有必要,輸入你的Apple ID憑據,然後單擊登錄。
To sign out, choose Sign Out from the account pop-up menu (named Tom Clark in the screenshot) in the upper-right corner of the window.
Share Containers Between Apps
Optionally, configure your app to use multiple containers or share a container with other apps. For example, you might use one app internally to create record types and records programmatically to return a database to a known state. This app needs to share the same container as the end-user app you are developing and testing. To do this, you enable the first app to use the default container of the second app or create a custom container that both apps share. iOS and Mac apps can also be configured to share the same containers.
在應用程序之間分享容器
這功能是可選的,配置您的應用程序使用多個容器或與其他應用程序共享相同的容器。例如,你可以使用一個應用程序內部創造的記錄類型和記錄程序到數據庫恢復到已知狀態。作爲正在開發和測試的最終用戶的應用程序,這個應用程序需要共享同一個容器。要做到這一點,你啓用了第一個應用程序來使用第二個應用程序的默認容器或創建一個這兩個應用程序共享的自定義的容器。
iOS和Mac應用程序也可以被配置爲共享相同的容器。
Add Containers to an App
Select an existing container ID used by another app or create a new one.
To add a container to an app
-
In the Capabilities pane under the iCloud settings, select “Specify custom containers.”
When you previously selected the CloudKit service, Xcode created a default container ID for your app that matches the bundle ID. A checkmark appears next to the default container ID.
1.在該功能下的iCloud設置面板中,選擇“指定自定義容器。”
如果您之前選擇的CloudKit服務,Xcode中創建一個與應用程序包ID相匹配的默認的容器ID。複選標記旁邊出現默認容器ID。
-
If necessary, click the Refresh button below the Containers table to download containers from MemberCenter that are used by other apps.
-
In the row of the container ID you want to add, select the checkbox.
Xcode updates the list of container IDs in the entitlements file.
The screenshot below shows the Curator and Gallery app sharing the Gallery app’s default container.
3.在您想要添加的容器ID的行前面,選擇複選框。
Xcode會在權利文件中更新容器ID列表。
下面的截圖展示了設置Curator和Gallery兩個應用程序共享Gallery應用的默認容器。
Create Custom Containers
Alternatively, create a custom container shared by multiple apps
Important: Only team admins can create containers. If you are an individual, you are the team agent (with admin privileges) for your one person team.
To create a custom container
-
If "Use default container” is selected, select “Specify custom containers.”
-
Click the Add button (+) at the bottom of the table.
-
In the dialog that appears, enter an identifier for the container you want to add.
A container ID begins with iCloud.
followed
by a string in reverse DNS notation, as in the iCloud.com.example.ajohnson.SharedGallery
container
ID.
4.Click OK.
Xcode adds the new container ID to the Xcode project entitlements file and to Member Center.
If you want to share the new container ID with another app, add the container to the app, as described in Add Containers to an App.
創建一個自定義容器(例外,創建一個多個應用共享的自定義容器)
1.如果"Use default container"被選擇了,那選擇"指定自定義容器"
2.點擊表格底部的添加按鈕(+)。
3.在出現的對話框中,輸入一個你想要添加的容器標識符。
警告:你不能刪除容器ID,所以小心設置容器標識符
容器ID一般以 iCloud. 開頭 ,後面跟着一個反向DNS表示的字符串,例如iCloud.com.example.ajohnson.SharedGallery
4.點擊OK
Verify Your Steps
You can view all the container IDs for your team in the iCloud settings or Member Center. In Member Center, you can also add containers and edit the name of containers.
To view container IDs in Member Center
-
In Member Center, select Certificates, Identifiers & Profiles.
-
Under Identifiers, select iCloud Containers.
在iCloud中的設置或會員中心中您可以查看所有的容器標識。在會員中心,你還可以添加容器和容器編輯的名字。
1.在會員中心,選擇證書,標識和簡介。
2.在標識符,選擇iCloud容器。
Create an iCloud Account for Development
You’ll need an iCloud account to save records to a CloudKit container. You’ll enter the credentials for this iCloud account on the device that you run your app. If you don’t have an iCloud account, create one that you can use during development. On your Mac, launch System Preferences and click iCloud. Click Create Apple ID under the Apple ID text field and follow the instructions.
Recap
In this chapter, you learned how to:
-
Enable CloudKit in your Xcode project, which creates your app’s default container.
-
Access CloudKit Dashboard to view the container’s schema and records.
-
Create an iCloud account to use for development.
在本章中,我們學習瞭如何:
在您的Xcode項目,創造你的應用程序的默認容器啓用CloudKit。
訪問CloudKit儀表板,以查看容器的模式和記錄。
創建一個帳戶的iCloud用於發展。