An open source framework for CloudKit written in Swift and inspired by Apple's CloudKit framework. OpenCloudKit is backed by CloudKit Web Services and is designed to allow easy CloudKit integration into Swift Servers whilst being familiar for developers who have experience with CloudKit on Apple's platforms.
Add the following to dependencies in your Package.swift
.
.Package(url: "https://github.com/BennyKJohnson/OpenCloudKit.git", majorVersion: 0, minor: 5)
Or create the 'Package.swift' file for your project and add the following:
import PackageDescription
let package = Package(
dependencies: [
.Package(url: "https://github.com/BennyKJohnson/OpenCloudKit.git", majorVersion: 0, minor: 5),
]
)
Configuring OpenCloudKit is similar to configuring CloudKitJS. Use the CloudKit.shared.configure(with: CKConfig)
method to config OpenCloudKit with a CKConfig
instance.
You can store CloudKit configuration in a JSON file
// API Token Config
{
"containers": [{
"containerIdentifier": "[insert your container ID here]",
"apiTokenAuth": {
"apiToken": "[insert your API token and other authentication properties here]"
},
"environment": "development"
}]
}
// Server-to-Server Config
{
"containers": [{
"containerIdentifier": "[Container ID]",
"serverToServerKeyAuth": {
"keyID": "[Key ID]",
"privateKeyFile":"eckey.pem"
},
"environment": "development"
}]
}
Initialize a CKConfig from JSON file, OpenCloudKit supports the same structure as CloudKit JS
let config = CKConfig(contentsOfFile: "config.json")
Below is an example of building a CKConfig manually
let serverKeyAuth = CKServerToServerKeyAuth(keyID: "[KEY ID]",privateKeyFile: "eckey.pem")
let defaultContainerConfig = CKContainerConfig(containerIdentifier: "[CONTAINER ID]", environment: .development, serverToServerKeyAuth: serverKeyAuth)
let config = CKConfig(containers: [defaultContainerConfig])
CloudKit.shared.configure(with: config)
Get the database in your app’s default container
let container = CKContainer.default()
let database = container.publicCloudDatabase
let movieRecord = CKRecord(recordType: "Movie")
movieRecord["title"] = "Finding Dory"
movieRecord["directors"] = NSArray(array: ["Andrew Stanton", "Angus MacLane"])
database.save(record: movieRecord) { (movieRecord, error) in
if let savedRecord = movieRecord {
// Insert Successfully saved record code
} else if let error = error {
// Insert error handling
}
}