SwiftUI跨平台App:MacOS创建Reminder失败iOS正常问题排查
iOS正常但MacOS无法创建Reminder的原因及解决方法
问题背景
开发SwiftUI跨平台应用时,创建Reminder的代码在iOS端运行正常,但MacOS端已在系统设置中授予权限的情况下,仍出现以下错误:
[CADXPCProxyHelper] Received error from calaccessd connection: Error Domain=NSCocoaErrorDomain Code=4099 "The connection to service named com.apple.CalendarAgent was invalidated: failed at lookup with error 159 - Sandbox restriction." UserInfo={NSDebugDescription=The connection to service named com.apple.CalendarAgent was invalidated: failed at lookup with error 159 - Sandbox restriction.}. Attempting to call any reply handler. [EventKit] Can't save reminder either because you have no access or you're trying to save using the legacy store, which is no longer supported Error Domain=EKErrorDomain Code=29 "(null)"
核心原因
MacOS的App Sandbox机制比iOS严格得多:
- iOS端无需手动配置App Sandbox权限(即使上架App Store,系统也会自动关联权限描述);
- MacOS端必须在App Sandbox中明确开启Reminders的读写权限,否则即使用户在系统设置中授权,App也会被沙箱限制,无法连接
com.apple.CalendarAgent服务,最终触发权限错误。
解决步骤
1. 配置App Sandbox权限
打开Xcode,选中你的MacOS应用目标,进入Signing & Capabilities标签页:
- 若未添加
App Sandbox,点击右上角+ Capability搜索并添加; - 在App Sandbox的权限列表中,找到Reminders选项,勾选
Read/Write权限。
2. 优化EKEventStore实例(可选但推荐)
避免在不同函数中重复创建EKEventStore实例,复用同一个实例可以减少潜在的服务连接问题:
import EventKit // 全局复用EKEventStore实例 private let eventStore = EKEventStore() func checkRemindersPermission() { eventStore.requestAccess(to: .reminder) { granted, error in if granted { createReminder() } } } func createReminder() { // 确保localDate和reminderTitle已正确赋值 guard let targetDate = localDate, let title = reminderTitle else { return } let alarm = EKAlarm(absoluteDate: targetDate) let reminder = EKReminder(eventStore: eventStore) reminder.title = title reminder.addAlarm(alarm) reminder.calendar = eventStore.defaultCalendarForNewReminders() do { try eventStore.save(reminder, commit: true) } catch { print("创建Reminder失败:\(error.localizedDescription)") } }
补充说明
错误中的EKErrorDomain Code=29对应EKErrorNotAuthorized,但这里并非用户未授权,而是沙箱限制导致App无法正常访问Reminders服务,配置沙箱权限后即可解决该问题。
内容的提问来源于stack exchange,提问作者Serhii Didanov
相关产品推荐
相关产品推荐

