gpt4 book ai didi

ios - 无法在 iOS 上创建本地日历

转载 作者:行者123 更新时间:2023-11-29 12:36:35 25 4
gpt4 key购买 nike

我正在尝试在 iOS 上创建本地日历。我请求访问 EKEntityTypeEvent 并获得授权,从 EKEventStore 创建日历(是的,我请求访问的同一实例),找到 EKSourceTypeLocal 然后将其设置在我的新日历上。调用 saveCalendar:commit:error(使用 commit:YES)返回 YES 并且没有 NSError。生成的日历分配了一个 calendarIdentifier

但是当我翻到 iOS 日历应用程序时,我的日历不在那里!我试过 iOS 7 和 8 模拟器(在“重置内容和设置...”之后,因此没有配置 iCloud)和装有 iOS 8 的 iCloud 连接的 iPhone 5s。没有任何效果!

我错过了什么?

我使用以下 View Controller 创建了一个裸项目来说明问题:

@import EventKit;
#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UILabel *resultLabel;

@property (nonatomic, assign) NSUInteger calendarCount;
@property (nonatomic, strong) EKEventStore *eventStore;

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

self.resultLabel.text = @"";
self.eventStore = [[EKEventStore alloc] init];
}

- (IBAction)userDidTapCreateCalendar:(id)sender {
EKAuthorizationStatus status = [EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent];

if (status == EKAuthorizationStatusNotDetermined) {
__weak typeof(self) weakSelf = self;
[self.eventStore requestAccessToEntityType:EKEntityTypeEvent
completion:^(BOOL granted, NSError *error) {
if (granted) {
[weakSelf createCalendar];
} else {
weakSelf.resultLabel.text = @"If you don't grant me access, I've got no hope!";
}
}];
} else if (status == EKAuthorizationStatusAuthorized) {
[self createCalendar];
} else {
self.resultLabel.text = @"Access denied previously, go fix it in Settings.";
}
}

- (void)createCalendar
{
EKCalendar *calendar = [EKCalendar calendarForEntityType:EKEntityMaskEvent eventStore:self.eventStore];
calendar.title = [NSString stringWithFormat:@"Calendar %0lu", (unsigned long)++self.calendarCount];

[self.eventStore.sources enumerateObjectsUsingBlock:^(EKSource *source, NSUInteger idx, BOOL *stop) {
if (source.sourceType == EKSourceTypeLocal) {
calendar.source = source;
*stop = YES;
}
}];

NSError *error = nil;
BOOL success = [self.eventStore saveCalendar:calendar commit:YES error:&error];
if (success && error == nil) {
self.resultLabel.text = [NSString stringWithFormat:@"Created \"Calendar %0lu\" with id %@",
(unsigned long)self.calendarCount, calendar.calendarIdentifier];
} else {
self.resultLabel.text = [NSString stringWithFormat:@"Error: %@", error];
}
}

@end

最佳答案

我已经在设备上测试了这段代码,它工作正常(更改 EKEntityMaskEvent -> EKEntityTypeEvent)。我可以在 iOS 日历中看到新日历。看起来模拟器实际上并没有保存您的日历。我前段时间遇到了同样的问题。

关于ios - 无法在 iOS 上创建本地日历,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26163631/

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