I have an app that is supposed to load a list of calendars into a pickerview. It works, but only after the app crashes and is opened again. I am not sure why it will not work on the initial opening of the app.
Calendar Permission Request:
func requestCalendarPermissions() {
eventInstance.requestAccess(to: .event, completion: {(accessGranted: Bool, error: Error?) in
if accessGranted == true {
print("Access Has Been Granted")
}
else {
print("Change Settings to Allow Access")
}
})
}
Checking the Permission Status:
func checkStatus() {
let currentStatus = EKEventStore.authorizationStatus(for: EKEntityType.event)
if currentStatus == EKAuthorizationStatus.notDetermined {
requestCalendarPermissions()
}
else if currentStatus == EKAuthorizationStatus.authorized {
print("Access Has Been Granted")
}
else if (currentStatus == EKAuthorizationStatus.denied) || (currentStatus == EKAuthorizationStatus.restricted){
print("Access Has Been Denied")
}
}
PickerView Code:
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return calendars.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return calendars[row].title
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
CalendarField.text = calendars[row].title
calID = calendars[row].calendarIdentifier
}
This error comes up:
Error getting all calendars: Error Domain=EKCADErrorDomain Code=1013 "(null)"
The app finally crashes because it tries to fill the pickerView and there is no data to fill with. I do load the calendars.
But, for some reason this doesn't happen after the app crashes and is reopened.
Thanks
[Update]
Console out:
2016-11-26 18:04:23.856 PTF[98143:2347570] Error getting all calendars: Error Domain=EKCADErrorDomain Code=1013 "(null)" Calendars Loaded 2016-11-26 18:04:23.869 PTF[98143:2347569] Error getting all calendars: Error Domain=EKCADErrorDomain Code=1013 "(null)" Calendars Loaded
Access Has Been Granted 2016-11-26 18:04:25.954 PTF[98143:2347569] Error getting all calendars: Error Domain=EKCADErrorDomain Code=1013 "(null)" Calendars Loaded
2016-11-26 18:04:46.620 PTF[98143:2347522] * Terminating app due to uncaught exception 'NSRangeException', reason: '* -[NSArray0 objectAtIndex:]: index 0 beyond bounds for empty NSArray' *** First throw call stack: ( 0 CoreFoundation 0x000000010bb6634b __exceptionPreprocess + 171 1 libobjc.A.dylib
0x000000010b5c721e objc_exception_throw + 48 2 CoreFoundation
0x000000010bb7dddd -[__NSArray0 objectAtIndex:] + 93 3
libswiftCore.dylib 0x000000010d95311d _TFVs12_ArrayBuffer19_getElementSlowPathfSiPs9AnyObject_ + 125 4 libswiftCore.dylib 0x000000010d953003 _TFVs12_ArrayBuffer10getElementfTSi20wasNativeTypeCheckedSb_x + 147 5 libswiftCore.dylib 0x000000010d963b8a _TFSag9subscriptFSix + 138 6 PTF 0x000000010af8df12 _TFC3PTF19MainScreenTableView10pickerViewfTCSo12UIPickerView12didSelectRowSi11inComponentSi_T_ + 226 7 PTF 0x000000010af8e10a _TToFC3PTF19MainScreenTableView10pickerViewfTCSo12UIPickerView12didSelectRowSi11inComponentSi_T_ + 74 8 UIKit 0x000000010c0d301d -[UIPickerView _sendSelectionChangedForComponent:notify:] + 104 9 UIKit 0x000000010c0d326e -[UIPickerView _sendSelectionChangedFromTable:notify:] + 370 10 UIKit 0x000000010c8a560d -[UIPickerTableView _scrollingFinished] + 187 11 UIKit 0x000000010c8a57fd -[UIPickerTableView scrollViewDidEndDecelerating:] + 30 12 UIKit 0x000000010c1df821 -[UIScrollView(UIScrollViewInternal) _scrollViewDidEndDeceleratingForDelegate] + 91 13 UIKit 0x000000010c1dc362 -[UIScrollView(UIScrollViewInternal) _stopScrollDecelerationNotify:] + 316 14 UIKit 0x000000010c1dc5b9 -[UIScrollView(UIScrollViewInternal) _stopScrollingNotify:pin:tramplingDragFlags:] + 505 15 UIKit 0x000000010c1d4503 -[UIScrollView _smoothScrollWithUpdateTime:] + 3304 16 QuartzCore 0x0000000113fa3bd5 _ZN2CA7Display15DisplayLinkItem8dispatchEy + 57 17 QuartzCore 0x0000000113fa3a95 _ZN2CA7Display11DisplayLink14dispatch_itemsEyyy + 449 18 CoreFoundation 0x000000010baf8964 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION + 20 19 CoreFoundation 0x000000010baf85f3 __CFRunLoopDoTimer + 1075 20 CoreFoundation 0x000000010baf817a __CFRunLoopDoTimers + 250 21 CoreFoundation
0x000000010baeff01 __CFRunLoopRun + 2065 22 CoreFoundation
0x000000010baef494 CFRunLoopRunSpecific + 420 23 GraphicsServices
0x000000011124aa6f GSEventRunModal + 161 24 UIKit
0x000000010c0eff34 UIApplicationMain + 159 25 PTF
0x000000010af9f36f main + 111 26 libdyld.dylib
0x000000010f30368d start + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException