トップ «前の日記(2017-12-15) 最新 次の日記(2017-12-18)» 編集

Cocoa練習帳

iOS/iPhone/iPad/watchOS/tvOS/MacOSX/Android プログラミング, Objective-C, Cocoa, Swiftなど

2012|01|02|03|04|05|06|07|08|09|10|11|12|
2013|01|02|03|04|05|06|07|08|09|10|11|12|
2014|01|02|03|04|05|06|07|08|09|10|11|12|
2015|01|02|03|04|05|06|07|08|09|10|11|12|
2016|01|02|03|04|05|06|07|08|09|10|11|12|
2017|01|02|03|04|05|06|07|08|09|10|11|12|
2018|01|02|03|04|05|06|07|08|09|10|11|12|
2019|01|02|03|04|05|06|07|08|09|10|11|12|
2020|01|02|03|04|05|06|07|08|09|10|11|12|
2021|01|02|03|04|05|06|07|08|09|10|11|12|
2022|01|02|03|04|05|06|07|08|09|10|11|12|
2023|01|02|03|04|05|06|07|08|09|10|11|12|
2024|01|02|

2017-12-17 [macOS][iOS]EventKitを試す

仕事効率化のアプリケーションには興味があって、タスク管理アプリを作ろうと考えている。その為の第一歩として、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

_ ソースコード

GitHubからどうぞ。
https://github.com/murakami/workbook/tree/master/ios/GettingThingsDone - GitHub

_ 【Cocoa練習帳】

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

トップ «前の日記(2017-12-15) 最新 次の日記(2017-12-18)» 編集