You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Swift筛选内置日历以获取订阅日历?

如何区分iOS中的订阅日历与系统内置节假日日历?

你遇到的问题是系统内置的节假日日历(如Święta w Polsce)和自定义订阅日历(如USOS)的公开属性(type、allowsContentModifications等)完全一致,无法通过常规属性过滤。以下是两种无需依赖名称的区分方法:

方法1:通过日历标识符前缀过滤

iOS系统内置的节假日日历的calendarIdentifier通常以固定前缀com.apple.mobilecal.holiday.开头,而用户手动添加的订阅日历不会使用该前缀。可以通过这个特征过滤:

var calendars = eventStore.calendars(for: .event)
calendars = calendars.filter { calendar in
    calendar.type == .subscription && 
    !calendar.calendarIdentifier.hasPrefix("com.apple.mobilecal.holiday.")
}

方法2:通过日历源的External ID过滤

系统内置节假日日历的source.externalID通常包含holiday关键词,而自定义订阅日历的源ID不会带有这个标识。可以结合该特征筛选:

var calendars = eventStore.calendars(for: .event)
calendars = calendars.filter { calendar in
    guard calendar.type == .subscription else { return false }
    return !(calendar.source.externalID?.contains("holiday") ?? false)
}

这两种方法都避开了名称过滤的局限性(比如多语言环境下日历名称会本地化),利用系统内置日历的固定标识特征实现精准筛选。

内容的提问来源于stack exchange,提问作者admi126n

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 03:42:18