gpt4 book ai didi

ios - EventKit 权限

转载 作者:可可西里 更新时间:2023-11-01 00:51:03 52 4
gpt4 key购买 nike

我希望我的 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")
}
}

最佳答案

您不能直接提示用户再次授予访问权限。但是您可以向用户显示有关权限状态的弹出窗口,并且可以请求从设备设置中启用权限。这是 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;

关于ios - EventKit 权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38843597/

52 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com