输入文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.files.user-selected.read-only</key>
<true/>
<key>com.apple.security.personal-information.calendars</key>
<true/>
</dict>
</plist>
Info.plist相关条目:
<key>NSCalendarsUsageDescription</key>
<string>This app needs access to your calendar to display today's events</string>
<key>NSCalendarsFullAccessUsageDescription</key>
<string>This app needs full access to your calendar to display today's events</string>
<key>NSCalendarsWriteOnlyAccessUsageDescription</key>
<string>This app needs write access to your calendar to add and update events</string>
代码请求日历访问:
@available(macOS 14.0, *)
func requestAccess() {
Task {
// Try to access calendar events to trigger permission dialog
let calendar = Calendar.current
let startDate = calendar.startOfDay(for: Date())
let endDate = calendar.date(byAdding: .day, value: 1, to: startDate)!
let predicate = eventStore.predicateForEvents(withStart: startDate, end: endDate, calendars: nil)
let _ = eventStore.events(matching: predicate)
// Basic access request
let basicPromise = DispatchGroup()
basicPromise.enter()
eventStore.requestAccess(to: .event) { (granted, error) in
basicPromise.leave()
}
_ = basicPromise.wait(timeout: .now() + 5)
// Open system settings
if let url = URL(string: "x-apple.systempreferences:com.apple.settings.Privacy.Calendars") {
NSWorkspace.shared.open(url)
}
// Check status
let currentStatus = EKEventStore.authorizationStatus(for: .event)
// If basic access is granted, request full access
if currentStatus == .authorized || currentStatus == .fullAccess {
do {
let _ = try await eventStore.requestFullAccessToEvents()
} catch {
print("Full access error: \(error.localizedDescription)")
}
}
}
}
我也尝试了:
手动打开系统设置>隐私和安全>日历
使用基本requestAccess(to:.event)和完整访问请求requestfullaccesstoevents()方法(MACOS 14+)
在明确请求许可之前,force访问日历事件 请求之前和之后检查授权状态
预期的行为
我设法解决了这个问题。事实证明,问题源于我的Info.plist
Info.plist
Solution:
I通过完全删除我的项目中的现有文件,然后通过Xcode的“文件> new> New> File ...”菜单(选择“属性列表”)添加新的,正确格式的文件来解决此问题。重新输入所需的
Info.plist
键,并确保正确设置权利的设置后,该应用现在如预期出现在隐私与安全>日历设置中。 本质上,问题可能是由于原始
Info.plist