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).
Post
Replies
Boosts
Views
Activity
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?
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?
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?
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?
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'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?
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.
I am able to send invitation from my device to friend's device. When friend clicks on invitation that was shared through text messages it says:
Open "Resources"?
User X wants to collaborate.
You'll join as User Y
(user Y @iCloud.com).
|Not Now| |Open|
If friend clicks on |Open| then nothing happens. Share remains in "invited" state and the callbacks which I expected to be called are not.
The official Apple CloudKit Sharing App - https://github.com/apple/sample-cloudkit-sharing/blob/main/Sharing/App/AppDelegate.swift - is confusing me because it does not have following code like typical SwiftUI app:
@main
struct MainApp: App {
Instead it uses @main for AppDelegate.
Here is my code with prints that encode what is going on:
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
print("I see this getting called on App startup")
return true
}
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
print("I also see this getting called on App startup")
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
print("I don't see this getting called")
}
func application(userDidAcceptCloudKitShareWith cloudKitShareMetadata: CKShare.Metadata) -> Bool {
print("However, I expected this to be called when friend opened his CloudKit share invitation")
return false
}
}
@main
struct MainApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
static let sharedModelActor: ModelActorDatabase = {
let schema = Schema([
Resource.self,
])
let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: false, cloudKitDatabase: .none)
do {
let modelContainer = try ModelContainer(for: schema, configurations: [modelConfiguration])
return ModelActorDatabase(modelContainer: modelContainer)
} catch {
fatalError("Could not create ModelContainer: \(error)")
}
}()
@StateObject var syncedDatabase: SyncedDatabase = SyncedDatabase(modelContainer: Self.sharedModelActor.modelContainer)
var body: some Scene {
WindowGroup {
ResourceView()
.environmentObject(syncedDatabase)
}
.modelContainer( Self.sharedModelActor.modelContainer )
.database(SharedDatabase.shared.database)
}
}
I was expecting that this would call userDidAcceptCloudKitShareWith, but it is not. Why?
User A shares zone with User B (influenced from https://github.com/apple/sample-cloudkit-zonesharing, but I have just one zone "Contacts" that I am sharing):
private func shareConfiguration() async throws -> (CKShare, CKContainer) {
let container = CKContainer(identifier: "iCloud.com.***.syncer")
let database = container.privateCloudDatabase
let zone = CKRecordZone(zoneName: "Contacts")
let fetchedZone = try await database.recordZone(for: zone.zoneID)
guard let existingShare = fetchedZone.share else {
print("Does not have existing share")
let share = CKShare(recordZoneID: zone.zoneID)
share[CKShare.SystemFieldKey.title] = "Resources"
_ = try await database.modifyRecords(saving: [share], deleting: [])
return (share, container)
}
print("Has existing share")
guard let share = try await database.record(for: existingShare.recordID) as? CKShare else {
throw NSError(domain: "", code: 0, userInfo: nil)
}
return (share, container)
}
...
let (share,container) = try! await shareConfiguration()
shareView = CloudSharingView(container: container, share: share) // UIViewControllerRepresentable implementation
User B accepts share invitation (borrowed from https://github.com/apple/sample-cloudkit-zonesharing)
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func windowScene(_ windowScene: UIWindowScene, userDidAcceptCloudKitShareWith cloudKitShareMetadata: CKShare.Metadata) {
guard cloudKitShareMetadata.containerIdentifier == "iCloud.com.***.syncer" else {
print("Shared container identifier \(cloudKitShareMetadata.containerIdentifier) did not match known identifier.")
return
}
// Create an operation to accept the share, running in the app's CKContainer.
let container = CKContainer(identifier: "iCloud.com.***.syncer")
let operation = CKAcceptSharesOperation(shareMetadatas: [cloudKitShareMetadata])
debugPrint("Accepting CloudKit Share with metadata: \(cloudKitShareMetadata)")
operation.perShareResultBlock = { metadata, result in
let shareRecordType = metadata.share.recordType
switch result {
case .failure(let error):
debugPrint("Error accepting share: \(error)")
case .success:
debugPrint("Accepted CloudKit share with type: \(shareRecordType)")
}
}
operation.acceptSharesResultBlock = { result in
if case .failure(let error) = result {
debugPrint("Error accepting CloudKit Share: \(error)")
}
}
operation.qualityOfService = .utility
container.add(operation)
}
}
User B through CKSyncEngine is able to read all records. However, when User B tries to write to database through CKSyncEngine, User B on his device gets following error:
<CKSyncEngine 0x1282a1400> error fetching changes with context <FetchChangesContext reason=scheduled options=<FetchChangesOptions scope=all group=CKSyncEngine-FetchChanges-Automatic)>>: Error Domain=CKErrorDomain Code=2 "Failed to fetch record zone changes" UserInfo={NSLocalizedDescription=Failed to fetch record zone changes, CKPartialErrors={
"<CKRecordZoneID: 0x3024872a0; zoneName=Contacts, ownerName=_18fb98f978ce4e9c207daaa142be6024>" = "<CKError 0x30249ed60: \"Zone Not Found\" (26/2036); server message = \"Zone does not exist\"; op = DC9089522F9968CE; uuid = 4B3432A4-D28C-457A-90C5-129B24D258C0; container ID = \"iCloud.com.***.syncer\">";
}}
Also, in CloudKit console, if I go to Zones, I don't see any zones under Shared Database. Wasn't I supposed to see my zone here?
However, I see "Contacts" zone under Private Database. If I expand Zone details I see following:
Zone wide sharing is enabled. All records in this zone are being shared with the sharing participants below.
And under Participants I see both User A and User B. User B is marked as:
Permission READ_WRITE
Type USER
Acceptance INVITED
What puzzles me is why READ works, but not WRITE?
I’m working on a project where I’m using CKSyncEngine to sync different types of SwiftData models, specifically User and Organization, to CloudKit. Here’s how I schedule these models to be synced:
For the User model:
let pendingSaves: [CKSyncEngine.PendingRecordZoneChange] = [.saveRecord(user.recordID)]
syncEngine.state.add(pendingRecordZoneChanges: pendingSaves)
For the Organization model:
let pendingSaves: [CKSyncEngine.PendingRecordZoneChange] = [.saveRecord(organization.recordID)]
syncEngine.state.add(pendingRecordZoneChanges: pendingSaves)
The problem arises in my CKSyncEngineDelegate's nextRecordZoneChangeBatch method where from CKRecord.ID alone I need to create the actual CKRecord that will be synced to CloudKit. This recordID alone doesn’t provide enough information to determine 1) in which local model table I need to fetch actual data to build whole CKRecord; and 2) what to put in CKRecord.recordType - whether it’s a User or an Organization.
Question:
What is the best practice for passing or determining the model type (e.g., User or Organization) in nextRecordZoneChangeBatch? How should I handle this in a way that effectively differentiates between the different model types being synced?
Any advice or examples would be greatly appreciated!
Few ideas:
embed the Model type in RecordID.recordName string, but this makes my recordNames longer (like resource_29af3932).
fetch data by recordID in all local persistent storage, but this seems slow and there is constraint that User and Organization IDs should never be the same.
introduce lookup table where from CKRecordID I can look up model type.
Somehow extend CKRecordID to add model type field?
When I try to install my app via USB cable on my child's iPhone 13 device I see following error in Xcode:
Installation on this device is prohibited by ManagedConfiguration
If I go to VPN & Device Management, I don't see anything. This is my Child's iphone so it is not like it is enrolled in some kind of school or work MDM.
Developer mode is ON and I added device ID to Certificates, Identifiers & Profiles > Devices.
Domain: IXRemoteErrorDomain
Code: 9
Failure Reason: Unhandled error domain IXRemoteErrorDomain, code 9
User Info: {
FunctionName = "-[IXSRemoteInstaller initWithRemoteInstallOptions:xpcAssetStream:assetSize:error:]";
SourceFileLine = 149;
}```
Is it cellular network provider that has locked something? How to be sure about this?
I switched from using @Query to @ModelActor because of the following reasons:
Performance Issues: With @Query, my app became unresponsive with large datasets because data fetching occurred on the main thread.
Integration with CKSyncEngine: I needed to implement @ModelActor anyway to allow CKSyncEngine to add data to local persistent storage from the background.
In my current setup, the onAppear() method for my view calls the getItems() function on my model actor, which returns [ItemsDTO] and then View renders them.
However, I'm now facing a challenge in achieving the same automatic data refreshing and view updates that @Query provided. Here are a few potential solutions I'm considering:
Periodic Data Fetching: Fetch data at regular intervals to keep the view updated. But this seems expensive.
Local Write Monitoring: Monitor all local writes to automatically trigger updates when changes occur. But this is quite a lot of code I would have to write myself.
Switch to manual refresh: Users would have to manually trigger UI updates by pressing button.
Reintroduce @Query: Writes would happen from ModelActor, but reads would still happen from Main thread. But then again app would become unresponsive on reads.
If you have any additional ideas or best practices for maintaining reactivity with @ModelActor, I'd love to hear them!
I'm not entirely sure if this API only shows which fields have changed (e.g., change.updatedAttributes), or if it also provides the updated values for those fields. Ultimately, my goal is to allow users to click in the app to view how a model has mutated over time, and I was wondering if this feature could facilitate that.
My app is able to receive data updates when it is in foreground.
however, when i move it in background then sync engine stops syncing until I again move app to foreground.