获取所有日历错误:错误域=EKCADErrorDomain Code=1013“(空)”Swift 3

6

我有一个应用程序,需要在选择器中加载一个日历列表。虽然可以工作,但只能在应用程序崩溃并重新打开后才能使用。我不确定为什么在应用程序初始打开时无法正常工作。

日历权限请求:

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")
    }
    })
}

权限状态检查:

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 代码:

    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
}

这个错误出现了:
获取所有日历的错误:Error Domain=EKCADErrorDomain Code=1013 "(null)"
应用程序最终崩溃,因为它试图填充pickerView,但没有数据可供填充。我确实加载了日历。
但是,由于某种原因,在应用程序崩溃并重新打开后,这种情况不会发生。
谢谢
[更新]
控制台输出:
2016年11月26日18:04:23.856 PTF[98143:2347570] 获取所有日历错误:错误域=EKCADErrorDomain,代码=1013,“(空白)”已加载日历2016年11月26日18:04:23.869 PTF[98143:2347569] 获取所有日历错误:错误域=EKCADErrorDomain,代码=1013,“(空白)”已加载日历。已授权访问2016年11月26日18:04:25.954 PTF[98143:2347569] 获取所有日历错误:错误域=EKCADErrorDomain,代码=1013,“(空白)”已加载日历。2016年11月26日18:04:46.620 PTF[98143:2347522] * 由于未捕获的异常“NSRangeException”,原因:“* [NSArray0 objectAtIndex:]:索引0超出了空NSArray的范围”终止应用程序。第一个抛出调用堆栈:(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_scrollFinished] + 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 +

哪一行代码导致了错误信息?为什么你的应用程序会崩溃?如果你的应用程序因为没有访问日历而崩溃,那么你需要修复你的代码。 - rmaddy
该应用程序可以访问日历,但在授权后,用户应该能够单击文本字段并弹出一个包含日历列表的选择器。然而,在应用程序初始安装时,列表为空,然后崩溃。在发生崩溃后,可以重新打开应用程序,此时选择器现在已填充了日历列表。 - rutheferd
你没有回答我的任何问题。哪一行代码导致了你发布的错误?哪一行代码导致了你的崩溃?崩溃时出现了什么错误信息?如果你不想提供细节让别人帮助你,那为什么要发布这个问题呢? - rmaddy
我已经添加了控制台输出,在崩溃之前和之后。我发布的错误没有明确调用任何导致错误的代码行。我假设导致崩溃的代码行是pickerview didselectrow,因为日历列表为空。 - rutheferd
1个回答

14

我刚遇到了这个问题。在执行'requestAccess'之后,文档中说要在事件实例上调用'reset'方法。然而,即使这样也无法使其正常工作。但是创建一个新的事件实例可以解决问题。我正在使用Objective-C,所以释放我使用的EKEventStore对象并分配一个新对象。


1
这应该是被接受的答案!对我也起作用了! - ph1lb4
这对我在Objective-C中也起作用,而在我的存储实例上调用“-reset”并没有解决问题。 - Natalia

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接