Introduction to Calendars and Reminders
Introduction to Calendars and Reminders The EventKit framework helps you access users’ Calendar and Reminders information. Although two different apps display users’ calendar and reminder data, the same framework manipulates the data. Similarly, the da
developer.apple.com
EventKit 공식 문서 간단 해석
주관적일 수 있습니당 틀린 내용이 있으면 댓글로 달아주세요!
- EventKit는 사용자의 기존 캘린더 및 미리 알림 데이터를 가져올 수 있음
- 사용자의 캘린더에 새로운 이벤트 및 알림도 만들 수 있음
- 이벤트 및 미리 알림을 편집 및 삭제할 수 있음
- 알람 추가나 반복 이벤트 지정과 같은 고급 태스크도 EventKit을 사용하여 수행 가능
- 앱 외부에서 캘린더 데이터베이스가 변경된 경우 EventKit은 알림을 통해 변경 사항을 감지하여 앱이 적절하게 작동할 수 있도록 함
- EventKit를 사용하여 달력 항목을 변경하면 연관된 달력(CalDAV, Exchange 등)에 자동으로 동기화됩니다.
실제 적용기
async/await 으로 구현하여 iOS 15+ 에서 사용가능합니다
func addEvent(startDate: Date, eventName: String) async throws -> Bool {
let eventStore = EKEventStore()
print("🔨권한을 요청합니다.")
// 권한요청이 비동기라 async await으로 사용자 응답 대기
let isAccessed = try await isAccessPermission(store: eventStore)
if isAccessed {
let calendars = eventStore.calendars(for: .event)
for calendar in calendars {
if calendar.title == "캘린더" || calendar.title == "Calendar" {
let event = EKEvent(eventStore: eventStore)
event.calendar = calendar
event.startDate = startDate
event.title = eventName
event.endDate = event.startDate.addingTimeInterval(1800)// 시작 시간으로부터 30분 let reminder1 = EKAlarm(relativeOffset: 0)// 해당 시간이 되면 알람울림
let reminder1 = EKAlarm(relativeOffset: 0)// 해당 시간이 되면 알람 울림!
event.alarms = [reminder1]
do {
print("🔨이벤트 등록을 시도합니다.")
try eventStore.save(event, span: .thisEvent)
print("✅ 이벤트가 등록되었습니다.")
return true
} catch {
print(#fileID, #function, #line, error.localizedDescription)
}
} else {
print("캘린더를 찾을 수 없습니다:", calendar.title)
}
}
} else {
print("❌ 권한이 거부됨")
return false
}
print("❌ addEvent 종료됨")
return false
}
func isAccessPermission(store: EKEventStore) async throws -> Bool {
var isRequestAccessed = false
switch EKEventStore.authorizationStatus(for: .event) {
case .notDetermined:
print("EventManager: not Determined")
// 여기서 권한 허용 팝업이 뜸!
isRequestAccessed = try await store.requestAccess(to: .event)
case .restricted:
print("EventManager: restricted")
case .denied:
// 권한 거부 시 사용자가 직접 설정 - '앱 이름' - 캘린더 접근 허용해야 함
print("EventManager: denied")
case .authorized:
print("EventManager: autorized")
isRequestAccessed = true
default:
print(#fileID, #function, #line, "unknown")
}
return isRequestAccessed
}
// startDate: Date, eventName: String
let isSuccess = try await addEvent(startDate: startDate, eventName: eventName)
순서
1. EKEventStore 인스턴스를 생성
2. EKEventStore.authorizationStatus(for: .event) 가 .notDetermined 이면
store.requestAccess(to: .event) 로 사용자에게 권한 요청(비동기)
3. 권한 허용되면 이벤트 등록 로직을 탐
틀린 내용이 있으면 편하게 댓글로 달아주세요!
'iOS' 카테고리의 다른 글
협업 시 팀원과 Provisioning Profile 및 Certificate 공유하기 (9) | 2022.10.22 |
---|---|
[Gradient Animation] AngularGradient Border 애니메이션 넣기 (3) | 2022.09.25 |
Swift 5.7 - if let 문 간략화 (0) | 2022.09.13 |
Swift 5.7 릴리즈 - 언어 및 표준라이브러리, 개발자 경험 (0) | 2022.09.13 |
[SwiftUI]커스텀 백버튼 백 제스처 시 화면 멈춤 이슈 해결 (0) | 2022.09.11 |