首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >EventKit权限

EventKit权限
EN

Stack Overflow用户
提问于 2016-08-09 14:29:37
回答 1查看 1.5K关注 0票数 2

我希望我的EventKit在尝试将事件添加到拒绝访问的日历时请求对日历的权限。它只是在第一次使用应用程序时询问,如果您在设置中拒绝访问,它将不会提示用户再次授予访问权限。请帮帮忙。

额外的学分:我也希望它检查事件是否已经存在于日历中,如果存在,编辑它。任何人在这方面的帮助都将不胜感激!

下面是我的代码:

代码语言:javascript
复制
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")
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-09 15:11:49

您不能直接提示用户再次授予访问权限。但您可以向用户显示有关权限状态的弹出窗口,也可以从设备设置请求启用权限。以下是objective-c中的一个示例

代码语言:javascript
复制
    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的此方法检查存储中的特定事件。您需要传递事件标识符。

代码语言:javascript
复制
- (nullable EKEvent *)eventWithIdentifier:(NSString *)identifier;
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38843597

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档