EKEventEditViewController freezes app and loads af

2019-09-21 19:41发布

I have the following code in swift:

    var eventController = EKEventEditViewController()
    eventController.editViewDelegate = self
    var store = EKEventStore()
    eventController.eventStore = store

    var event = EKEvent(eventStore: store)
    event.title = viewModel.roleName
    event.location = viewModel.location
    event.startDate = viewModel.startDate
    event.endDate = viewModel.endDate
    eventController.event = event

    var status = EKEventStore.authorizationStatusForEntityType(EKEntityTypeEvent)
    switch status {
    case .Authorized:
        self.setNavBarAppearanceStandard()            
        dispatch_async(dispatch_get_main_queue(), { () -> Void in
            self.presentViewController(eventController, animated: true, completion: nil)
        })
    case .NotDetermined:
        store.requestAccessToEntityType(EKEntityTypeEvent, completion: { (granted, error) -> Void in
            if granted == true {
                self.setNavBarAppearanceStandard()
                dispatch_async(dispatch_get_main_queue(), { () -> Void in
                    self.presentViewController(eventController, animated: true, completion: nil)
                })
            }
        })
    case .Denied, .Restricted:
        UIAlertView(title: "Access Denied", message: "Permission is needed to access the calendar. Go to Settings > Privacy > Calendars to allow access for the Be Collective app.", delegate: nil, cancelButtonTitle: "OK").show()
        return
    }

It's pretty straight forward. It works perfectly on simulator. But when you try to run the code on device, the whole UI freezes up and sometimes after about 1 minute the edit event view controller comes up, and sometimes not at all.

Please help.

1条回答
对你真心纯属浪费
2楼-- · 2019-09-21 20:09

After a bit of digging I've manager to figure out that the eventStore (EKEventStore) variable must be an instance variable or a global var in a singleton class. If it is a local var it will take forever to present the view controller. This is probably some optimisation issues on the SDK.

查看更多
登录 后发表回答