• Stars
    star
    389
  • Rank 110,500 (Top 3 %)
  • Language
    Swift
  • License
    BSD 2-Clause "Sim...
  • Created over 6 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An encoder and decoder for CKRecord

CloudKit + Codable = ❤️

Badge showing the current build status

This project implements a CloudKitRecordEncoder and a CloudKitRecordDecoder so you can easily convert your custom data structure to a CKRecord and convert your CKRecord back to your custom data structure.

Be aware this is an initial implementation that's not being used in production (yet) and it doesn't support nesting. Nested values would have to be encoded as CKReference and I haven't implemented that yet (feel free to open a PR 🤓).

Usage

CustomCloudKitCodable

The types you want to convert to/from CKRecord must implement the CustomCloudKitCodable protocol. This is necessary because unlike most implementations of encoders/decoders, we are not converting to/from Data, but to/from CKRecord, which has some special requirements.

There are also two other protocols: CustomCloudKitEncodable and CustomCloudKitDecodable. You can use those if you only need either encoding or decoding respectively.

The protocol requires two properties on the type you want to convert to/from CKRecord:

var cloudKitSystemFields: Data? { get }

This will be used to store the system fields for the CKRecord when decoding. The system fields contain metadata for the record such as its unique identifier and they're very important when syncing.

var cloudKitRecordType: String { get }

This property should return the record type for your custom type. It's implemented automatically to return the name of the type, you only need to implement this if you need to customize the record type.

URLs

There's special handling for URLs because of the way CloudKit works with files. If you have a property that's a remote URL (i.e. a website), it's encoded as a String (CloudKit doesn't support URLs natively) and decoded back as a URL.

If your property is a URL and it contains a URL to a local file, it is encoded as a CKAsset, the file will be automatically uploaded to CloudKit when you save the containing record and downloaded when you get the record from the cloud. The decoded URL will contain the URL for the location on disk where CloudKit has downloaded the file.

Example

Let's say you have a Person model you want to sync to CloudKit. This is what the model would look like:

struct Person: CustomCloudKitCodable {
    var cloudKitSystemFields: Data?
    let name: String
    let age: Int
    let website: URL
    let avatar: URL
    let isDeveloper: Bool
}

Notice I didn't implement cloudKitRecordType, in that case, the CKRecord type for this model will be Person (the name of the type itself).

Now, before saving the record to CloudKit, we encode it:

let rambo = Person(
    cloudKitSystemFields: nil,
    name: "Guilherme Rambo",
    age: 26,
    website: URL(string:"https://guilhermerambo.me")!,
    avatar: URL(fileURLWithPath: "/Users/inside/Pictures/avatar.png"),
    isDeveloper: true
)

do {
   let record = try CloudKitRecordEncoder().encode(rambo)
   // record is now a CKRecord you can upload to CloudKit
} catch {
   // something went wrong
}

Since avatar points to a local file, the corresponding file will be uploaded as a CKAsset when the record is saved to CloudKit and downloaded back when the record is retrieved.

To decode the record:

let record = // record obtained from CloudKit
do {
   let person = try CloudKitRecordDecoder().decode(Person.self, from: record)
} catch {
   // something went wrong
}

Requirements

  • iOS 13.0+
  • macOS 11.0+
  • Xcode 13.2+

Installation

Swift Package Manager

Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the Swift build system.

Once you have your Swift package set up, adding CloudKitCodable as a dependency is as easy as adding it to the dependencies value of your Package.swift.

dependencies: [
    .package(url: "https://github.com/insidegui/CloudKitCodable.git", from: "0.2.0")
]

Manually

If you prefer not to use Swift Package Manager, you can integrate CloudKitCodable into your project manually by copying the files in.

More Repositories

1

WWDC

The unofficial WWDC app for macOS
Swift
8,601
star
2

VirtualBuddy

Virtualize macOS 12 and later on Apple Silicon, VirtualBuddy is a virtual machine GUI for macOS M1, M2, M3
Swift
4,987
star
3

AssetCatalogTinkerer

An app that lets you open .car files and browse/extract their images.
Swift
2,050
star
4

PodcastMenu

Put Overcast on your Mac's menu bar
Swift
1,267
star
5

AnimojiStudio

Make cute emoji videos with unlimited duration and share anywhere (iPhone X, XS and XS Max only!)
Objective-C
1,180
star
6

MultipeerKit

MultipeerConnectivity + Codable = ❤️
Swift
1,089
star
7

AppleEvents

The unofficial Apple Events app for macOS
Objective-C
825
star
8

NoiseBuddy

Control the listening mode on your AirPods Pro in the Touch Bar or Menu Bar.
Swift
726
star
9

PlayAlways

Create Xcode playgrounds from your menu bar
Swift
550
star
10

DarkModeBuddy

Switch your Mac's system theme based on the ambient light intensity.
Swift
482
star
11

StatusBuddy

Keep track of Apple's developer and consumer system statuses right in your menu bar.
Swift
450
star
12

TouchBarSpaceFight

A simple, fun game for the MacBook Pro's Touch Bar
Swift
361
star
13

AppearanceMaker

Create custom appearances to customize your app's look with NSAppearance
Objective-C
234
star
14

F3X

Fight Flash Fraud: test your SD cards to see if they're genuine and in good working order
Objective-C
203
star
15

MacPreviewUtils

Handy tools for SwiftUI previews on macOS.
Swift
195
star
16

NoteTaker

A simple note taking app for macOS and iOS which uses Realm and CloudKit for syncing
Swift
174
star
17

GRProKit

Open-source ProKit clone
Objective-C
173
star
18

AudioCap

Sample code for recording system audio on macOS 14.4+
Swift
172
star
19

EmojiPickerUI

Framework implementing a macOS-style emoji picker for iPadOS.
Swift
168
star
20

MultipeerDemo

AirDrop-like functionality using MultipeerConnectivity
Swift
158
star
21

GRGlassWindow

A NSWindow subclass with a beautiful glass-like title bar
Objective-C
151
star
22

Milkshakr

A sample app used in many of my apps and workshops.
Swift
149
star
23

sheet

Custom iOS sheet
Swift
143
star
24

VisualPairingHack

An experiment with the private VisualPairing framework used for automatic iOS and watchOS device pairing
Objective-C
139
star
25

BlurredBgApp

iOS 13 app that shows the user's wallpaper as its background.
Swift
138
star
26

dmgdist

Automate the process of creating, uploading and notarizing the DMG of a Mac app.
Swift
135
star
27

MeshBuddy

Simple SwiftUI MeshGradient editor
Swift
130
star
28

CAARPlayer

An app that lets you view .caar files
Objective-C
119
star
29

FanFan

If you're a fan of fans, this app is for you.
Swift
117
star
30

CustomStatusBarWindow

Demonstrates a custom NSWindowController that can be attached to a NSStatusItem on macOS.
Swift
116
star
31

ocrit

Simple command-line utility for performing OCR using Apple's Vision framework
Swift
114
star
32

PIPContainer

An easy to use interface for picture-in-picture on macOS 10.12 and later
Objective-C
113
star
33

DealStack

Simple card stack implemented with SwiftUI
Swift
113
star
34

FOTWindow

A NSWindow subclass that makes auto hiding and showing the title bar (à la QuickTime X) a lot easier, and allows for a custom title bar drawing block.
Objective-C
112
star
35

CloudKitchenSink20

Sample code for my article on CloudKit.
Swift
110
star
36

ChromeCastCore

An open source implementation of the Google Cast SDK for macOS
Swift
107
star
37

GRCustomizableWindow

NSWindow subclass with support to custom titlebar height, background color and title color
Objective-C
103
star
38

DarkMode

Enable "real dark mode" on OS X / macOS
Objective-C
100
star
39

AppleRemoteFramework

Use the Apple Remote to control your OS X apps.
Objective-C
93
star
40

ThemingWithBundles

Sample project for my article on bundles
Swift
90
star
41

TextTransformer

Apple ExtensionFoundation/ExtensionKit sample app
Swift
81
star
42

GRNavigationController

Simple navigation controller for OS X apps.
Objective-C
77
star
43

mvcwithsugar

Companion Playground for my talk at dotSwift 2020
Swift
75
star
44

WWDC-tvOS

[ABANDONED] WWDC app for the new Apple TV
Swift
74
star
45

launchapp

Launch iOS applications from the command line (jailbreak only)
Shell
73
star
46

BreatheReplica

A simple replica of the Apple Watch Breathe app animation written in SwiftUI.
Swift
73
star
47

PearlSaver

Face ID detection animation as a screensaver
Objective-C
72
star
48

KeyframesPlayer

A simple macOS app to preview animations created with Facebook's keyframes framework
Swift
70
star
49

TwitterPublishPlugin

A plugin for Publish that let's you easily embed tweets in your posts.
Swift
70
star
50

GRPushSegue

A custom NSStoryboardSegue to make a nice iOS-like push transition.
Objective-C
68
star
51

FinderSyrahUI

An experiment showing how Finder might look like on the next OSX.
Objective-C
65
star
52

fluid-interactions-talk

A Swift playground implementing some simple fluid interactions.
Swift
64
star
53

D22Bar

Test your app with the new iPhone status bar
Objective-C
63
star
54

iosarchitecture

Randomly generate the next big thing ® on iOS app development
Swift
62
star
55

UXKitDemo

Demo app using Apple's private UXKit framework
Objective-C
61
star
56

GRProgressIndicator

reimplementation of NSProgressIndicator with some customization support
Objective-C
56
star
57

wwdc.io

The official website for the unofficial WWDC app for macOS 🤓 ❤️
HTML
55
star
58

Astronomer

Sample app for my talk at the CocoaHeads Conference
Swift
50
star
59

GRStatusBar

Safari-like status bar for macOS apps
Swift
50
star
60

DarkImagePublishPlugin

Publish plugin that lets you have both light and dark mode variants for images on your site 😎
Swift
50
star
61

GRVibrantTitlebarWindow

NSWindow with vibrant titlebar
Objective-C
49
star
62

Listr

Sample for my article on writing command line interfaces for iOS apps.
Swift
48
star
63

ConfigurableWidgetDemo

AppKit macOS app with a WidgetKit widget that can be configured with dynamic options from an intents extension
Swift
48
star
64

VLCX

[ABANDONED] QuickTime-like video player based on libvlc
Objective-C
45
star
65

CloudKitchenSink

A collection of CloudKit code samples
Swift
40
star
66

TTWindow

NSWindow subclass that allows you to set a custom tint color for the titlebar
Objective-C
37
star
67

followdown

Remove annoying badges from System Preferences on macOS.
Swift
37
star
68

VideoMeme

A sample project showing how a document-based app can be created with SwiftUI.
Swift
37
star
69

Flights

Simple flight information tracker for OS X.
Objective-C
36
star
70

DocumentBasedSwiftPlaygroundsApp

A document-based app in Swift Playgrounds 4 for iPad
Swift
31
star
71

Unsterificator

Mac menu bar app to quickly toggle mono/stereo audio
Swift
29
star
72

VisualEffectDebugger

Simple debugger for NSVisualEffectView
Objective-C
28
star
73

Chuck

The best Chuck Norris jokes in the palm of your hand
Swift
25
star
74

CheckmarkDemo

Sample code for my article on using Core Animation Archives
Swift
18
star
75

encodeformastodon

Simple command-line tool for macOS that encodes videos in a format suitable for publishing to Mastodon
Swift
17
star
76

Pawtraits_SampleCode

Sample code for the Fast & Fluid talk
Swift
17
star
77

GRCodeSignatureVerifier

Simple code signature verification for macOS apps
Objective-C
16
star
78

CAArchivesTalk

Code for my talk on CoreAnimation Archives
Swift
16
star
79

CoreFollowUpAttack

CoreFollowUp phishing attack on macOS
Objective-C
15
star
80

xGhostPreventer

An Xcode plug-in designed to prevent accidental app distribution through an unsafe version of Xcode
Objective-C
15
star
81

XPNotificationCenter

Cross-process notification center for iOS, WatchOS and OS X
Swift
15
star
82

WWDC-Win

WWDC app for Windows 10
C#
14
star
83

Binge

Projeto exemplo da minha palestra sobre desenvolvimento pra Mac
Swift
12
star
84

AdobeNativeFullscreen

A plugin to make some Adobe Creative Cloud apps support the native fullscreen mode on OS X.
Objective-C
11
star
85

killerapp

Handy helper to quickly restart stuff on OS X
Swift
10
star
86

libav-ios-build

Automatically builds libav as universal frameworks for iOS and iOS simulator.
Shell
7
star
87

IGListDiff

Diffing part of IGListKit isolated so it can be used on macOS
Objective-C
6
star
88

GRStatusBarDemo

Demo app for GRStatusBar
Swift
5
star
89

LocationAPI

Objective-C class for finding coordinates of a location using Google Maps
Objective-C
5
star
90

EnvVarExample

Sample of using xcconfig to inject environment variable values into app
Swift
5
star
91

GRThemeStore

A simple way of storing app assets (mainly images) in a compressed file
Objective-C
4
star
92

RealmBindingsDemo

A sample project demonstrating how to use Cocoa Bindings with Realm.
Swift
4
star
93

GRCodeSignatureVerifierDemo

Demo app for GRCodeSignatureVerifier
Swift
3
star
94

GRProKitResources

Contains the original sketch projects for the various UI elements in GRProKit
3
star
95

create_iconset

Shell script for creating OSX icon sets
Shell
2
star
96

pluggable

Sample cocoa app with support to javascript plugins, can be used as a starting point
Objective-C
2
star
97

wwdc-data

Static data used by the WWDC app for macOS
1
star
98

shakedown-data

Data used by Shakedown.app
1
star
99

correiocsv

Ruby
1
star