我希望我的EventKit在尝试将事件添加到拒绝访问的日历时请求对日历的权限。它只是在第一次使用应用程序时询问,如果您在设置中拒绝访问,它将不会提示用户再次授予访问权限。请帮帮忙。
额外的学分:我也希望它检查事件是否已经存在于日历中,如果存在,编辑它。任何人在这方面的帮助都将不胜感激!
下面是我的代码:
func addToCalendar(){
let eventStore = EKEventStore()
let startDate = NSDate()
let endDate = startDate.dateByAddingTimeInterval(60 * 60) // One hour
if (EKEventStore.authorizationStatusForEntityType(.Event) != EKAuthorizationStatus.Authorized) {
eventStore.requestAccessToEntityType(.Event, completion: {
granted, error in
self.createEvent(eventStore, title: "\(self.meal.text!)", startDate: startDate, endDate: endDate)
})
} else {
createEvent(eventStore, title: "\(self.meal.text!)", startDate: startDate, endDate: endDate)
}
}
func createEvent(eventStore: EKEventStore, title: String, startDate: NSDate, endDate: NSDate) {
let event = EKEvent(eventStore: eventStore)
event.title = title
event.startDate = startDate
event.endDate = endDate
event.calendar = eventStore.defaultCalendarForNewEvents
event.notes = "\(self.Recipe.text!) - See Meal Planning on Listacular"
do {
try eventStore.saveEvent(event, span: .ThisEvent)
savedEventId = event.eventIdentifier
} catch {
print("Denied")
}
}发布于 2016-08-09 15:11:49
您不能直接提示用户再次授予访问权限。但您可以向用户显示有关权限状态的弹出窗口,也可以从设备设置请求启用权限。以下是objective-c中的一个示例
EKEventStore *store = [EKEventStore new];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (!granted)
{
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertController* alertController=[UIAlertController alertControllerWithTitle:@"Access to Calendar is Restricted" message:@"To re-enable, please go to Settings and turn on Calendar Settings for this app else Continue to create party without saving it to your Calendar" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *actionOK = [UIAlertAction actionWithTitle:@"Continue" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action)
{
[self addEvent:store]; //your method
}];
[alertController addAction:actionOK];
[alertController addAction:[UIAlertAction actionWithTitle:kSETTINGS style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action)
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}]];
[self presentViewController:alertController animated:NO completion:nil];
});
}
else
{
[self addEvent:store]; // your method
}
}];您可以使用EKEventStore的此方法检查存储中的特定事件。您需要传递事件标识符。
- (nullable EKEvent *)eventWithIdentifier:(NSString *)identifier;https://stackoverflow.com/questions/38843597
复制相似问题