LoginSignup
4
5

More than 5 years have passed since last update.

[macOS][iOS]EventKitを試す

Posted at

仕事効率化のアプリケーションには興味があって、タスク管理アプリを作ろうと考えている。その為の第一歩として、EventKitを試してみる。macOSとiOSの両方を考えているが、サンプルはiOSで。

iOSでは、Info.plistに以下のような定義が必要だ。

 NSCalendarsUsageDescription
    Access the data of the calendar.
    NSRemindersUsageDescription
    Access the data of the reminder.

macOSでは、com.apple.security.personal-information.calendars エンタイトルメントを含めるみたいだが、まだ試していないので紹介に止める。

初期化処理としてイベントストアへの接続というのがあるのだが、ユーザへの認証が必要となる。

let store = EKEventStore()
let status = EKEventStore.authorizationStatus(for: .event)
var isAuth = false
switch status {
case .notDetermined:
    isAuth = false
case .restricted:
    isAuth = false
case .denied:
    isAuth = false
case .authorized:
    isAuth = true
}
if !isAuth {
    store.requestAccess(to: .event, completion: {
        (granted, error) in
        if granted {
        }
        else {
            return
        }
    })
}

デフォルトのカレンダーに対して、過去一年分のイベントを取得する。

let startDate = Date(timeIntervalSinceNow: -365.0 * 24.0 * 60.0 * 60.0)
let endDate = Date()
let defaultCalendar = store.defaultCalendarForNewEvents
let predicate = store.predicateForEvents(withStart: startDate, end: endDate, calendars: [defaultCalendar!])
let events = store.events(matching: predicate)
print(events)

以下の通り、取得できた。

EventKit.jpg

ソースコード
GitHubからどうぞ。

https://github.com/murakami/workbook/tree/master/ios/GettingThingsDone - GitHub

関連情報
Cocoa勉強会 BUKURO.swift (connpass)

Cocoa勉強会 BUKURO.swift (ATND)

Cocoa勉強会 BUKURO.swift (Peatix)

【Cocoa練習帳】
http://www.bitz.co.jp/weblog/

http://ameblo.jp/bitz/(ミラー・サイト)

Qiita

4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5