How to create and save EKCalendar on ios 6

后端 未结 7 1177
自闭症患者
自闭症患者 2020-12-05 03:42

I\'m having an issue where I create my EKCalendar and everything looks good but then when I go to list my calendars, it doesn\'t show up. I also go to check my calendar list

相关标签:
7条回答
  • 2020-12-05 04:10

    I had this same problem. I did as sixthcent suggested and had the app ask for permission. That solved one part but the calendar didn't show up still but was being created according to the NSLogs. I had an Exchange calendar (I didn't have iCloud) on my phone and had to turn it off. Once that was deleted it showed up as a Local calendar. When I went back to re-add Exchange it asked if I wanted to keep both and now both calendars are showing up. Here is my code below.

    #import "ViewController.h"
    #import <EventKit/EventKit.h>
    #import <EventKitUI/EventKitUI.h>
    
    @interface ViewController ()
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad
    {
    [super viewDidLoad];  
    
    EKEventStore *store = [[EKEventStore alloc] init];
    EKSource *localSource = nil;
    for (EKSource *source in store.sources)
    {
        if (source.sourceType == EKSourceTypeCalDAV && [source.title isEqualToString:@"iCloud"])
        {
            localSource = source;
            break;
        }
    }
    if (localSource == nil)
    {        
        for (EKSource *source in store.sources) {
            if (source.sourceType == EKSourceTypeLocal)
            {
                localSource = source;
                break;
            }
        }
    }
    
    EKEventStore *es = [[EKEventStore alloc] init];
    [es requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
        /* This code will run when uses has made his/her choice */
    
        if (error)
        {
            // display error message here
        }
        else if (!granted)
        {
            // display access denied error message here
        }
        else
        {
            // access granted          
        }
    
    
    }];
    
    //NSString *identifier; //Use to create for the first time and store somewhere
    NSString *identifier = @"704A1304-5213-4AB3-9C7B-F6B59E3454BB"; //Stored version
    
    //Create the calendar
    EKCalendar *cal;
    if (identifier == nil)
    {
        cal = [EKCalendar calendarForEntityType:EKEntityTypeEvent eventStore:store];
    
        cal.title = @"Demo1 Calendar";
        cal.source = localSource;
        [store saveCalendar:cal commit:YES error:nil];
        NSLog(@"cal id = %@", cal.calendarIdentifier);
    
    } else {
        //Calendar already exists!
        cal = [store calendarWithIdentifier:identifier];
        NSLog(@"cal id = %@", cal.calendarIdentifier);
    }
    
    //calendar properties
    NSLog(@"%@", cal);
    
    //Add Event to Calendar
    NSLog(@"Adding event!");
    EKEventStore *eventStore = [[EKEventStore alloc] init];
    
    EKEvent *event  = [EKEvent eventWithEventStore:eventStore];
    event.title     = @"Event3";
    
    NSDate *startDate = [NSDate date];
    event.calendar = cal;
    event.startDate = startDate;
    event.endDate = [startDate dateByAddingTimeInterval:3600];
    
    NSError *error = nil;
    BOOL result = [eventStore saveEvent:event span:EKSpanThisEvent commit:YES error:&error];
    if (result) {
        NSLog(@"Saved event to event store.");
    } else {
        NSLog(@"Error saving event: %@.", error);
    }
    
    }
    
    0 讨论(0)
提交回复
热议问题