GithubHelp home page GithubHelp logo

cloudkitpublicdatabasesample's Introduction

Sample Code: CloudKit Public Database

This is a sample app that allows users to add Events to CloudKit's public database, and see Events that other users have added.

Setting Up

Create your iCloud Container on CloudKit

  1. 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). Image of Xcode project file showing Signing and Capabilities.
  2. Change the Bundle Identifier of the app to be unique to you. For example:
    • Bundle: com.yourname.appname
  3. Click the 'plus' button under your iCloud containers to create a new container with a name that matches your Bundle Identifier.
    • iCloud container: iCloud.com.yourname.appname Image of Xcode project file showing Bundle ID and iCloud Containers.
  4. Press the refresh button below the iCloud containers so that Xcode updates it.
  5. Click CloudKit Console and log in with your developer account info. Click on "iCloud database." Image of CloudKit Console showing iCloud Database.
  6. 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. Image of CloudKit Console showing iCloud Database.
  7. Go to the CloudKitService class and change its container property. Paste your iCloud container name (from signing and capabilities OR iCloud console) into the CKContainer(identifier:) initializer.

Testing Current Functionality

  1. Run the app on an iOS simulator or device.
  2. 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.)
  3. If you just signed in to your iCloud account, you might need to quit the app and reopen it.
  4. 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.
  5. 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. Image of CloudKit console showing adding an index for recordName.

Adding Your Own Data Types

  1. Create a new file named like YourTypeName.swift.
  2. Create a new struct representing this type.
struct YourTypeName {
    var someProperty: SomeType
    // etc
}
  1. 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
}
  1. Create a ViewModel to store your type, or (if this new type is related to Events), integrate this type into the existing EventView Model

Further Learning

  • 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.

cloudkitpublicdatabasesample's People

Contributors

sibutrip avatar zcutlerf avatar

Stargazers

 avatar

Watchers

Dezmond Blair avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.