I took CKShare with Zone example - https://github.com/apple/sample-cloudkit-sharing
Modified it a little bit so code looks like this:
struct ResourceView: View {
@State private var showingShare: Bool = false
@State private var shareView: CloudSharingView?
...
var body: some View {
HStack {
Button(action: {
Task {
let (share,container) = try! await shareConfiguration()
shareView = CloudSharingView(container: container, share: share)
showingShare = true
}
}) {
Label("Share", systemImage: "circle")
}
...
.sheet(isPresented: $showingShare) {
if let shareView = shareView {
shareView
} else {
Text("No sheet to show")
}
}
And the first time I click on Share button I am getting "No sheet to show" despite showingShare boolean being set after shareView variable. Presumably because shareView is nil.
The second time I click on Share button it shows the sharing view.
Post
Replies
Boosts
Views
Activity
I'm currently managing two independent ModelContext instances in my app—one dedicated to CKSyncEngine and another for the Main/UI thread.
While this setup works to some extent, I'm encountering a couple of issues:
UI Updates: When SwiftData is updated via CKSyncEngine, the UI doesn't automatically refresh. To address this, I've had to implement .refreshable() and write imperative Swift code to (re)fetch data. This approach feels counterintuitive since it prevents me from fully leveraging SwiftUI's declarative nature, such as using @Query and user must explicitly trigger refresh.
Deletion Logic: If users delete data via the UI, I have to manage a different delete code path. Specifically, I need to ensure that the object is removed from the UI's ModelContext without triggering a deletion in CKSyncEngine's ModelContext. This dual-path deletion logic feels unnecessarily complex.
Also, I intend to later re-use CKSyncEngine part for Command Line tool app that will not have UI at all.
What is the correct way to manage SwiftData in a background process like CKSyncEngine while maintaining a seamless and declarative approach in SwiftUI?
Suppose I have two iPhones that are offline. On the first iPhone, at 1 PM, I create a Person object with the details: name: "John", lastName: "Smith", and age: 40. Then, on the second iPhone, which is also offline, I also create Person object at 2 PM with the same name: "John" and lastName: "Smith", but with a different age: 30.
Both iPhones come online at 3 PM and sync with CloudKit. I would expect CloudKit to reconcile these two records and end up with a single record—specifically, Person(name: "John", lastName: "Smith", age: 30), assuming a "last writer wins" approach.
Any guidance or best practices for handling this situation would be greatly appreciated!
My idea is that I could generate a 128bit UUID as hash from first name and last name and then I would have to force this UUID to be used as recordName in CKRecord as this would trigger a conflict on CloudKit side and prevent two instance to be created. But how do I accomplish this with SwiftData or CoreData?
I am able to fetch CloudKit records from my MacOS command line tool/daemon.
However, I would like CloudKit to notify my daemon whenever CKRecords were altered so I would not have to poll periodically.
In CloudKit console I see that my app successfully created CloudKit subscription, but the part that confuses me is where in my app do I define callback function that gets called whenever CloudKit attempted to notify my app of CloudKit changes?
My first question - do I need to define callback in my implementation of UNUserNotificationCenterDelegate? NSApplicationDelegate? Something else?
My second question, would CKSyncEngine work from command line application?
In Xcode I have created UI-less application. I tried to add following code:
import CloudKit
let container = CKContainer.default()
And it is failing with:
In order to use CloudKit, your process must have a com.apple.developer.icloud-services entitlement. The value of this entitlement must be an array that includes the string "CloudKit" or "CloudKit-Anonymous".
If I go to project and select my Command Line Tool target I don't see CloudKit capability that I usually see in UI based applications.
So, is it impossible to use CloudKit from Command Line tools?
At 11:37 in this video - https://developer.apple.com/videos/play/wwdc2021/10123/ - Nolan instantiates MyModel() in swiftui view file. And then at 12:02 he just uses MyModel from extension.
I have the exact same code and when I try to build my project, it fails with error that MyModel() could not be found.
I shared my MyModel.swift file between extension target and main app. Now it builds. However, it seems there are two separate instances of MyModel.
What is proper way for DeviceActivityMonitor extension to pass data to main app? I simply want to increment counter from extension every minute and let the main app to know that.
Or even better, - is there a way to use SwiftData from Device Activity Monitor extension?
My swift app has several places where it updates data stored with SwiftData. In each place I have to remember to add function doSomethingWithTheUpdatedData().
I would like my app to be reactive and call doSomethingWithTheUpdatedData() from a single place opposed to be scattered throughout my app. How to accomplish this?
As can be seen here, all categories are expanded but nothing under them:
Not sure if related, but when I tried to upload my app for internal TestFlight testing I got following error:
The code looks like this:
@ObservedObject var model : MyModel
@State var isPresented = false
var body: some View {
Button("select apps to discourage"){
isPresented = true
}.onAppear {
Task {
do {
try await AuthorizationCenter.shared.requestAuthorization(for: .child)
} catch {
}
}
}.familyActivityPicker(isPresented: $isPresented, selection: $model.selectionToDiscourage)
}
}
Now I am wondering, is there one more thing that I need to do before familyActivityPicker would work?
Here is what I have done: I subscribed to $99 Apple developer plan, Then I was finally able to see and add Family Controls capability under my Xcode project, Then I created Apple Family and logged in iphone (and simulator) as child. Then several days ago I submitted this form - https://developer.apple.com/contact/request/family-controls-distribution (not sure if this was mandatory for internal testing, but I have not received any communication from Apple - even confirmation for my request).