import UIKit
import EventKit
// 関数定義
func eventsWithStore(eventStore: EKEventStore ) -> [EKEvent]{
let startDate = NSDate()
let endDate = startDate.dateByAddingTimeInterval(86400*100) // 100日後を指定
let predicate = eventStore.predicateForEventsWithStartDate(startDate, endDate: endDate, calendars: nil)
let events = eventStore.eventsMatchingPredicate(predicate)
return events;
}
// main
let eventStore = EKEventStore() // 生成にコストがかかるので毎回生成はしない
var events: [EKEvent]?
// カレンダーにアクセスが許可されているかチェック
if EKEventStore.authorizationStatusForEntityType(.Event) == EKAuthorizationStatus.Authorized {
events = eventsWithStore(eventStore)
}else{
// 許可されてないので許可を要求
eventStore.requestAccessToEntityType(.Event,
completion: {(granted: Bool, error: NSError?) -> Void in
if granted{
print("granted!")
events = eventsWithStore(eventStore)
}else{
print("denied!")
}
})
}
// 取得したイベントをハンドリング
if let _events = events{
print("Events count is \(_events.count)")
for event in _events{
print(event.title)
}
}
More than 5 years have passed since last update.
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme