 
                            iPhoneでファイルダウンロードの際に嵌った話
 のえる
                    のえる
                 のえる
                        のえる
                     
                                    どうも、のえるです。
 
インフルエンザが蔓延する時期ですね。
通勤にマスクは必須になってます。
 
さて、今回はiOSのデフォルトカレンダーにイベントを追加する方法を紹介します。
アプリで日付があるものは、カレンダーへの追加機能があると非常に便利です。
 
環境は以下の通りです。
 
Xcode: 8.2.1
Language: Swift 3
Target OS: iOS9, 10
 
iOSのカレンダー追加には「EventKit.framework」と「EventKitUI.framework」が必要です。
まずはプロジェクトにライブラリを追加しましょう。
 
そして、呼び出す画面のControllerは「EKEventEditViewDelegate, UINavigationControllerDelegate」の2つを継承させます。
また、ソースコードの「 eventStore:EKEventStore 」はローカルプロパティである必要があります。
 

 
| 1 2 3 4 5 6 7 8 9 10 | import UIKit import EventKit import EventKitUI class ViewController: UIViewController, EKEventEditViewDelegate, UINavigationControllerDelegate { 	// Property(Private) 	private let eventStore:EKEventStore = EKEventStore() . . . } | 
 
続いて、カレンダー追加のボタン押下時のメソッド内に、イベント追加用のプログラムを書きましょう
 
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | // イベント追加ボタンタップ時メソッド @IBAction func touchUpAddEventButton(_ sender: UIButton) { 	let title:String = "イベントタイトル" 	let location:String = "愛知県名古屋市中区" 	let startDate:Date = Date() 	let endDate:Date = startDate.addingTimeInterval(60*60*3) 	let notes:String = "開場:10分前" 	let timezone:TimeZone = TimeZone(identifier: "Asia/Tokyo")! 	self.showCalendarView(title: title, location: location, startDate: startDate, endDate: endDate, notes: notes, timezone: timezone) } // イベント作成権限チェックのカスタムメソッド func showCalendarView(title:String, location:String?, startDate:Date, endDate:Date, notes:String?, timezone:TimeZone) { 	// カレンダー追加の権限ステータスを取得 	let authStatus = EKEventStore.authorizationStatus(for: .event) 	switch authStatus { 	case .authorized: 		self.openCalendarView(title: title, location:location, startDate: startDate, endDate: endDate, notes: notes, timezone: timezone) 	case .denied: break 	case .restricted: break 	case .notDetermined: 		self.eventStore.requestAccess(to: .event, completion: { (result:Bool, error:Error?) in 			if result { 				self.openCalendarView(title: title, location:location, startDate: startDate, endDate: endDate, notes: notes, timezone: timezone) 			} else { 				// 使用拒否 			} 		}) 	} } // イベント作成画面を開くカスタムメソッド func openCalendarView(title:String, location:String?, startDate:Date, endDate:Date, notes:String?, timezone:TimeZone) { 	let event:EKEvent = EKEvent(eventStore: self.eventStore) 	event.title = title 	event.location = location 	event.startDate = startDate 	event.endDate = endDate 	event.notes = notes 	event.timeZone = timezone 	event.calendar = self.eventStore.defaultCalendarForNewEvents 	let eventController = EKEventEditViewController() 	eventController.delegate = self 	eventController.event = event 	eventController.editViewDelegate = self 	eventController.eventStore = self.eventStore 	self.present(eventController, animated: true, completion: nil) } // イベント作成画面用デリゲート func eventEditViewController(_ controller: EKEventEditViewController, didCompleteWith action: EKEventEditViewAction) { 	var result = -1 	switch action { 	case .canceled: break 	case .saved: 		do { 			try controller.eventStore.save(controller.event!, span: .thisEvent) 			result = 1 		} catch { 			// イベント追加失敗 			result = 9 		} 	case .deleted: break 	} 	controller.dismiss(animated: true) { 		var message = "" 		if result != -1 { 			if result == 1 { 				message = "イベント追加 完了" 			} 			else if result == 9 { 				message = "イベント追加 失敗" 			} 			// 結果をアラート表示 			let alert = UIAlertController(title: "Result", message: message, preferredStyle: .alert) 			alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil)) 			self.present(alert, animated: true, completion: nil) 		} 	} } | 
 
eventEditViewController が、カレンダーへのイベント追加画面から戻ってきた際のデリゲートメソッドです。
ここで登録した結果を取得し、判定しています。
 
これでカレンダーへのイベント追加ができます。
※なお、実際に作成される場合は、カレンダーへのアクセス権限がない場合などは、アラートで表示する等の処理が必要になるでしょう。
 
 
いかがでしたか?
 
カレンダーへのイベント追加ですが、iPhoneは非常にやりやすいと思います。
Androidは少々手間ですね(そのあたりも紹介出来たらします)。
 
次回もプログラムネタをお送りしたいと思います。
 
それではっ!!
POPULAR
 
                        のえる
Full-stack Developer
人気記事