【问题标题】:Reminder Create Fails with NIL for Error Message提示创建失败,错误消息为 NIL
【发布时间】:2013-06-04 02:53:42
【问题描述】:

我一直致力于为我的应用添加待办事项功能。虽然一切看起来都正确,但我在提醒上创建失败,但错误值为 NIL。这是相关代码 - (我从 UIViewController 中提取文本和日期 - 一切都正确连接)。`

-(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
    }


}

`这段代码的大部分是试图以正确的格式获取日期。我的 NSLog 显示以下内容: "错误保存提醒:(null)"

【问题讨论】:

  • NSLog(@"%@", store); 就在提醒保存之前,看看它会给你什么。
  • store is null...这一定意味着我没有访问日历的权限...据我所知
  • 检查设置并确保该应用有权访问日历 - 设置 > 隐私 > 日历 > 您的应用(应该打开)
  • 就是这样.. 我发现我的权限部分没有加载.. 将它移到 ViewDidLoad.. 并且一切正常!谢谢!
  • 哦.. 我没有代表 :)

标签: ios error-handling eventkit ios6.1 reminders


【解决方案1】:

在 ViewDidLoad 函数中添加以下代码

   // Add Todo Feature
    store = [[EKEventStore alloc] init];
    [store requestAccessToEntityType:EKEntityTypeReminder
                          completion:^(BOOL granted, NSError *error) {
                              // Handle not being granted permission
                          }];

【讨论】:

    猜你喜欢
    • 2014-04-30
    • 2012-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-15
    • 1970-01-01
    • 2020-05-14
    • 1970-01-01
    相关资源
    最近更新 更多