$npx -y skills add rshankras/claude-code-apple-skills --skill cloudkit-syncGenerate CloudKit sync infrastructure using CKSyncEngine with conflict resolution, sharing, and account monitoring. Use when adding iCloud sync to an iOS/macOS app.
| 1 | # CloudKit Sync Generator |
| 2 | |
| 3 | Generate production-ready CloudKit sync infrastructure using `CKSyncEngine` (iOS 17+ / macOS 14+), the modern replacement for manual `CKOperation` chains. |
| 4 | |
| 5 | ## When This Skill Activates |
| 6 | |
| 7 | Use this skill when the user: |
| 8 | - Asks to "add iCloud sync" or "sync data across devices" |
| 9 | - Mentions "CloudKit", "CKSyncEngine", or "cloud sync" |
| 10 | - Wants to "share data between users" via iCloud |
| 11 | - Asks about "conflict resolution" for synced data |
| 12 | - Mentions "CKRecord", "CKShare", or "CKRecordZone" |
| 13 | |
| 14 | ## Pre-Generation Checks |
| 15 | |
| 16 | ### 1. Project Context Detection |
| 17 | |
| 18 | Before generating, ALWAYS check: |
| 19 | |
| 20 | ```bash |
| 21 | # Check deployment target (CKSyncEngine requires iOS 17+ / macOS 14+) |
| 22 | grep -r "platform" Package.swift 2>/dev/null || true |
| 23 | grep -r "IPHONEOS_DEPLOYMENT_TARGET\|MACOSX_DEPLOYMENT_TARGET" --include="*.pbxproj" | head -3 |
| 24 | |
| 25 | # Find existing CloudKit implementations |
| 26 | rg -l "CKSyncEngine\|CKContainer\|CKRecord\|CKOperation" --type swift | head -10 |
| 27 | |
| 28 | # Check for existing entitlements |
| 29 | find . -name "*.entitlements" -exec cat {} \; 2>/dev/null | grep -i "icloud" |
| 30 | |
| 31 | # Check existing persistence layer |
| 32 | rg -l "@Model\|NSManagedObject\|PersistentModel" --type swift | head -5 |
| 33 | |
| 34 | # Check for existing sync infrastructure |
| 35 | rg "CKSyncEngineDelegate\|CKSubscription\|CKFetchRecordZoneChanges" --type swift | head -5 |
| 36 | ``` |
| 37 | |
| 38 | ### 2. Compatibility Verification |
| 39 | |
| 40 | **CKSyncEngine requires:** |
| 41 | - iOS 17.0+ / macOS 14.0+ / watchOS 10.0+ / tvOS 17.0+ |
| 42 | - CloudKit entitlement |
| 43 | - Active iCloud account on device |
| 44 | |
| 45 | If deployment target is below iOS 17 / macOS 14, warn the user that CKSyncEngine is not available and suggest either raising the target or using the older CKOperation approach (which this generator does not cover). |
| 46 | |
| 47 | ### 3. Conflict Detection |
| 48 | |
| 49 | If existing CloudKit code is found: |
| 50 | - Ask: Replace existing implementation, extend it, or migrate to CKSyncEngine? |
| 51 | |
| 52 | ## Configuration Questions |
| 53 | |
| 54 | Ask user via AskUserQuestion: |
| 55 | |
| 56 | 1. **What data needs syncing?** |
| 57 | - Provide your model types (e.g., Note, Task, Document) |
| 58 | - What properties does each model have? |
| 59 | |
| 60 | 2. **Database scope?** |
| 61 | - Private only (user's own data across their devices) |
| 62 | - Private + Shared (enable CKShare for collaboration) |
| 63 | |
| 64 | 3. **Conflict resolution strategy?** |
| 65 | - Server-wins (simplest -- always accept server version) |
| 66 | - Client-wins (always push local version) |
| 67 | - Timestamp-based merge (most recent modification wins) |
| 68 | - Custom merge (field-level merge logic) |
| 69 | |
| 70 | 4. **Existing persistence layer?** |
| 71 | - SwiftData (will generate CKRecord <-> SwiftData bridging) |
| 72 | - Core Data (will generate CKRecord <-> NSManagedObject bridging) |
| 73 | - Custom / in-memory (will generate standalone CKRecord mapping) |
| 74 | - None yet (will generate lightweight local store + sync) |
| 75 | |
| 76 | ## Generation Process |
| 77 | |
| 78 | ### Step 1: Read Templates |
| 79 | |
| 80 | Read code templates from this skill: |
| 81 | - `templates.md` - All CKSyncEngine code templates |
| 82 | |
| 83 | ### Step 2: Create Core Files |
| 84 | |
| 85 | Generate these files based on configuration: |
| 86 | |
| 87 | **Always generate:** |
| 88 | ``` |
| 89 | Sources/CloudSync/ |
| 90 | ├── SyncEngine.swift # CKSyncEngine setup + CKSyncEngineDelegate |
| 91 | ├── SyncConfiguration.swift # Zone names, container ID, database scope |
| 92 | ├── RecordMapping.swift # CKRecord <-> local model conversion |
| 93 | ├── ConflictResolver.swift # Conflict resolution strategy |
| 94 | ├── SyncMonitor.swift # Account status + sync state observation |
| 95 | └── CloudSyncError.swift # Typed error handling with CKError mapping |
| 96 | ``` |
| 97 | |
| 98 | **If sharing enabled:** |
| 99 | ``` |
| 100 | Sources/CloudSync/Sharing/ |
| 101 | ├── ShareManager.swift # CKShare creation and management |
| 102 | └── ShareParticipantView.swift # UICloudSharingController wrapper |
| 103 | ``` |
| 104 | |
| 105 | ### Step 3: Determine File Location |
| 106 | |
| 107 | Check project structure: |
| 108 | - If `Sources/` exists -> `Sources/CloudSync/` |
| 109 | - If `App/` exists -> `App/CloudSync/` |
| 110 | - Otherwise -> `CloudSync/` |
| 111 | |
| 112 | ### Step 4: Customize for Project |
| 113 | |
| 114 | Adapt templates to match: |
| 115 | - User's model types and property names |
| 116 | - Bundle identifier for CloudKit container ID |
| 117 | - Chosen conflict resolution strategy |
| 118 | - Database scope (private only vs. private + shared) |
| 119 | |
| 120 | ### Step 5: Entitlements Setup |
| 121 | |
| 122 | Generate or update entitlements file with required CloudKit capabilities. |
| 123 | |
| 124 | ## Entitlements and Capabilities Setup |
| 125 | |
| 126 | ### Required Xcode Capabilities |
| 127 | |
| 128 | 1. **iCloud** capability: |
| 129 | - Check "CloudKit" |
| 130 | - Add container: `iCloud.com.<team-identifier>.<app-bundle-id>` |
| 131 | |
| 132 | 2. **Background Modes** (recommended): |
| 133 | - Check "Remote notifications" (for push-based sync triggers) |
| 134 | |
| 135 | ### Required Entitlements |
| 136 | |
| 137 | ```xml |
| 138 | <?xml version="1.0" encoding="UTF-8"?> |
| 139 | <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
| 140 | <plist version="1.0"> |
| 141 | <d |