无法创建iOS上的本地日历(Can't create local calendar on i

2019-10-21 01:48发布

我试图创建iOS上的本地日历。 我请求访问EKEntityTypeEvent并将它授予创建从日历EKEventStore (是的,相同的情况下,我要求从接入),找到EKSourceTypeLocal然后将它放在我的新日历。 调用saveCalendar:commit:error (与commit:YES )返回YES并没有NSError 。 得到的日历有一个calendarIdentifier分配。

但后来当我翻到iOS日历应用程序,我的日历是不存在的! 我试过在iOS 7和8个模拟器(一个“重置内容和设置...”后,所以没有配置的iCloud)和一个的iCloud连接的iPhone 5S与iOS 8.没有什么工作!

有什么我错过了?

我已创建了以下视图控制器裸项目来说明这个问题:

@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

Answer 1:

我测试过的设备上的代码,它的工作原理确定(改变EKEntityMaskEvent - > EKEntityTypeEvent )。 我可以看到的iOS日历的新日历。 看起来模拟器实际上并不节省您的日历。 我有同样的问题,前一段时间。



文章来源: Can't create local calendar on iOS
标签: ios eventkit