Domanda

I've been working on adding todo function to my app. While everything seems to look right I get a failed create on the Reminder, but an error value of NIL. Here's the relevant code - (I am pulling the text and the date from a UIViewController - everything is wired correctly).`

-(IBAction)createButtonPressed
{

    // Setup Variables for coverting date:
    int year;
    int month;
    int day;
    int hour;
    int minute;
    int second;
    NSDate *actualDate;
    NSDateComponents *dateComps;
    NSCalendar  *calendar;
    NSDateFormatter *dateFormatter;

    actualDate = dateValue.date;
    calendar = [[NSCalendar currentCalendar] copy];
    dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeZone:[calendar timeZone]];
    [dateFormatter setDateFormat:@"yyyy"];
    year = [[dateFormatter stringFromDate:actualDate] intValue];
    [dateFormatter setDateFormat:@"MM"];
    month = [[dateFormatter stringFromDate:actualDate] intValue];
    [dateFormatter setDateFormat:@"dd"];
    day = [[dateFormatter stringFromDate:actualDate] intValue];
    [dateFormatter setDateFormat:@"HH"];
    hour = [[dateFormatter stringFromDate:actualDate] intValue];
    [dateFormatter setDateFormat:@"mm"];
    minute = [[dateFormatter stringFromDate:actualDate] intValue];
    [dateFormatter setDateFormat:@"ss"];
    second = [[dateFormatter stringFromDate:actualDate] intValue];
    dateComps = [[NSDateComponents alloc] init];
    [dateComps setTimeZone:[calendar timeZone]];
    [dateComps setDay:day];
    [dateComps setMonth:month];
    [dateComps setYear:year];
    // Create the Todo
    EKReminder *reminder = [EKReminder reminderWithEventStore:store];


    [reminder setTitle:self.actionText.text];

    [reminder setDueDateComponents:dateComps];

    EKCalendar *defaultReminderList = [store defaultCalendarForNewReminders];

    [reminder setCalendar:defaultReminderList];

    NSError *error = nil;
    BOOL success = [store saveReminder:reminder
                                commit:YES
                                 error:&error];
    if (!success) {
        NSLog(@"Error saving reminder: %@", [error localizedDescription]);
        // Popup a messaging saying the reason why you can't create the todo.

    } else {
        // Popup a message saying the to do was created
    }


}

`The majority of this code is trying to get the date in correct format. my NSLog shows the following: "Error saving reminder: (null)"

È stato utile?

Soluzione

Added the following code to the ViewDidLoad function

   // Add Todo Feature
    store = [[EKEventStore alloc] init];
    [store requestAccessToEntityType:EKEntityTypeReminder
                          completion:^(BOOL granted, NSError *error) {
                              // Handle not being granted permission
                          }];
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top