大阪市中央区 システムソフトウェア開発会社

営業時間:平日09:15〜18:15
MENU

Widget作成メモ(7)ーカレンダーイベントの使用許可

著者:川上洋
公開日:2020/11/26
最終更新日:2020/11/26
カテゴリー:技術情報 雑記

こんにちは、川上です。

Widgetに、取り敢えず、今日明日のカレンダーイベントの表示で十分ですwね。

内臓持ちの「カレンダー.app」で、カレンダーイベントを追加するのがラクかなっと。

巷から参照したカレンダーイベントの表示処理を、チョコっと、編集しました。
で、部品I/Fから。。。

import Foundation
import UIKit
import EventKit
// MARK: - +++ UIKit U/I +++ CFLib_Mgr()
class CFLib_Mgr {
    // MARK: - カレンダーイベント用 メンバー
    static let myEventStore = EKEventStore()
    var myTargetCalendar: EKCalendar!

    ・・・・
    
     // MARK: +++ 許可状況を確認して、許可されていなかったら許可を得る
 static func allowAuthorization() -> Bool{
     var bRet :Bool = true
     
     if getAuthorization_status() {
         // 許可されている
         return bRet
     } else {
         bRet = false
         // 許可されていない
         myEventStore.requestAccess(to: .event, completion: {
         (granted, error) in
             if granted {
                 return
             }
             else {
                 print("Not allowed")
             }
         })

     }
     
     return bRet
 } // +++ allowAuthorization()
 
 // MARK: +++認証ステータスを確認する
 static func getAuthorization_status() -> Bool {
     // 認証ステータスを取得
     let status = EKEventStore.authorizationStatus(for: .event)

     // ステータスを表示 許可されている場合のみtrueを返す
     switch status {
     case .notDetermined:
         print("NotDetermined")
         return false

     case .denied:
         print("Denied")
         return false

     case .authorized:
         print("Authorized")
         return true

     case .restricted:
         print("Restricted")
         return false
     @unknown default:
         return true
     }
 } // +++ getAuthorization_status()


}

 

info.plistのPrivacy – Calendars Usage Description
を忘れずに。。

次回も、カレンダーイベントの取得へと続くのでした。。。
ではでは。

    上に戻る