菜鸟教程小白 发表于 2022-12-11 20:23:46

ios - Swift 4 如何从日历中获取所有事件?


                                            <p><p>我使用的是 Swift 4.1。我想编写一个函数来收集 iOS 日历应用程序中所有日历中的所有事件。感谢stackoverflow上的这个答案:<a href="https://stackoverflow.com/questions/33618685/how-to-get-all-events-out-of-a-calendar-swift" rel="noreferrer noopener nofollow">How to get all Events out of a Calendar (Swift)</a>我能够编写自己的 <code>class</code> 并将其称为 <code>Cale</code>。请看这个:</p>

<pre><code>import UIKit
import EventKit

class Cale {

    private func createDate(year: Int) -&gt; Date {
      var components = DateComponents()
      components.year = year
      components.timeZone = TimeZone(secondsFromGMT: 0)

      return Calendar.current.date(from: components)!
    }

    private let eventStore = EKEventStore()

    private func get() {
      let calendars = eventStore.calendars(for: .event)

      for calendar in calendars {
            // This checking will remove Birthdays and Hollidays callendars
            guard calendar.allowsContentModifications else {
                continue
            }

            let start = createDate(year: 2016)
            let end = createDate(year: 2025)

            print(&#34;start: \(start)&#34;)
            print(&#34;end: \(end)&#34;)

            let predicate = eventStore.predicateForEvents(withStart: start, end: end, calendars: )

            print(&#34;predicate: \(predicate)&#34;)

            let events = eventStore.events(matching: predicate)

            for event in events {
                print(&#34;    title: \(event.title!)&#34;)
                print(&#34;startDate: \(event.startDate!)&#34;)
                print(&#34;endDate: \(event.endDate!)&#34;)
            }
      }
    }

    func checkStatusAndGetAllEvents() {
      let currentStatus = EKEventStore.authorizationStatus(for: EKEntityType.event)

      switch currentStatus {
      case .authorized:
            //print(&#34;authorized&#34;)
            self.get()
      case .notDetermined:
            //print(&#34;notDetermined&#34;)
            eventStore.requestAccess(to: .event) { accessGranted, error in
                if accessGranted {
                  self.get()
                } else {
                  print(&#34;Change Settings to Allow Access&#34;)
                }
            }
      case .restricted:
            print(&#34;restricted&#34;)
      case .denied:
            print(&#34;denied&#34;)
      }
    }
}
</code></pre>

<p>非常简单的类,你可以使用它,它可以工作,但有一个异常(exception)。
主要函数是 <code>get()</code> 在这个函数中,我基于两个日期创建谓词:开始和结束。如您所见,开始日期是:</p>

<blockquote>
<p>2016-01-01 00:00:00 +0000</p>
</blockquote>

<p>结束日期是:</p>

<blockquote>
<p>2025-01-01 00:00:00 +0000</p>
</blockquote>

<p>但是如果我们运行程序,我们会看到谓词是这样的:</p>

<blockquote>
<p>CADEventPredicate start:01/01/2016, 03:00; end:01/01/2020, 03:00;
cals:(
      2 )</p>
</blockquote>

<p>仅从 2016 年到 2020 年,4 年!我已经在不同的日期对其进行了测试,但我可以得到最大间隔为 4 年的谓词。这意味着,它不会给我所有的事件!所以问题是:如何从日历中获取所有事件?如果可能,不要使用日期!</p>

<p>感谢您对 future 的任何帮助或建议!</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>如果有人仍然想知道,Apple 故意将其限制为四年。</p>

<p>来自 <a href="https://developer.apple.com/documentation/eventkit/ekeventstore/1507479-predicateforevents" rel="noreferrer noopener nofollow">predicateForEvents(withStart:end:calendars:)</a> :</p>

<blockquote>
<p>For performance reasons, this method matches only those events within a four year time span. If the date range between startDate and endDate is greater than four years, it is shortened to the first four years.</p>
</blockquote>

<p>如果您想要一个更长的范围,我想您必须将其包装在您自己的函数中以将其拆分为四年 block 。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - Swift 4 如何从日历中获取所有事件?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/51439574/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/51439574/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - Swift 4 如何从日历中获取所有事件?