This is a sample app that allows users to add Events to CloudKit's public database, and see Events that other users have added.
- In the Xcode Project File (blue icon on the top left of Xcode) -> for this iOS target -> Signing & Capabilities: change the Development Team to your Development Team (i.e.: select your name from the list).
- Change the Bundle Identifier of the app to be unique to you. For example:
- Bundle:
com.yourname.appname
- Bundle:
- Click the 'plus' button under your iCloud containers to create a new container with a name that matches your Bundle Identifier.
- Press the refresh button below the iCloud containers so that Xcode updates it.
- Click CloudKit Console and log in with your developer account info. Click on "iCloud database."
- Using the dropdown on the top left, make sure that you can find the container you just created (i.e.:
iCloud.com.yourname.appname
) in the list of containers. - Go to the
CloudKitService
class and change itscontainer
property. Paste your iCloud container name (from signing and capabilities OR iCloud console) into theCKContainer(identifier:)
initializer.
- Run the app on an iOS simulator or device.
- Make sure that the simulator or device is signed into your iCloud account, and that iCloud Drive is turned on. (You may want to make another iCloud account just for development on the simulator. You can do this on iCloud.com.)
- If you just signed in to your iCloud account, you might need to quit the app and reopen it.
- Since this is a new container, you probably won't see any events on the view. Try adding an event with the phone or simulator.
- Quit the app (don't just put it in the background, but swipe up to close it, or press "stop" on Xcode if it's running) and reopen it. Do you see the event you just added?
Tip
It's possible that you will see an error like "field recordName is not marked queryable." To solve this, return to your CloudKit console for your app's container. Select "Indexes" on the right, and select your Event type. Add a new index for recordName to be queryable.
- Create a new file named like
YourTypeName.swift
. - Create a new struct representing this type.
struct YourTypeName {
var someProperty: SomeType
// etc
}
- Add
CloudKitService
methods that create, read, update, and delete instances of your type.
class CloudKitService {
// existing event methods
func saveNewType(_ newType: NewType) async throws {
// copy implementation from saveEvent(_:)
// adjust it to store the properties of your new type
}
// other methods for new type
}
- Create a ViewModel to store your type, or (if this new type is related to Events), integrate this type into the existing EventView Model
- Want to store images? Use CKAsset. (Note: this article is using UIKit and completion handlers, so it requires careful reading to use in the SwiftUI and Swift Concurrency context of this app.
- Want to create types that relate to each other? (i.e.: Comments on an Event) Use CKRecord.Reference.