2013-03-06 47 views
4

我試圖從我的應用保存/檢索提醒,但由於某種原因,EKReminder好像並未保存。這裏是我的代碼:無法在iOS 6中保存EKReminder

EKEventStore * _eventStore = [[EKEventStore alloc] init]; 
[_eventStore requestAccessToEntityType:EKEntityTypeReminder completion:^(BOOL granted, NSError *error) { 

    //create a new calendar for reminders. 
    EKCalendar *calendar = [EKCalendar calendarForEntityType:EKEntityTypeReminder eventStore:_eventStore]; 
    EKSource *localSource = nil; 
    for (EKSource *source in _eventStore.sources) 
     if (source.sourceType == EKSourceTypeLocal) 
     { 
      localSource = source; 
      break; 
     } 

    calendar.source = localSource; 

    self.calendarIdentifier = calendar.calendarIdentifier; 
    EKReminder *reminder = [EKReminder reminderWithEventStore:_eventStore]; 
    reminder.calendar = calendar; 
    reminder.title = @"Test Reminder"; 
    reminder.startDateComponents = [[NSCalendar currentCalendar] components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]]; 
    reminder.completionDate = [NSDate dateWithTimeIntervalSinceNow:100]; 

    EKAlarm *alarm = [EKAlarm alarmWithAbsoluteDate:[NSDate dateWithTimeIntervalSinceNow:100]]; 
    reminder.alarms = @[alarm];  

    NSError *errorOb = nil; 
    BOOL saved = [_eventStore saveReminder:reminder commit:YES error:&errorOb]; 
    if (saved) NSLog(@"Saved Reminder"); 
    else NSLog(@"Failed to save reminder"); 

}]; 

然後後來,我試圖驗證該提醒實際上已被保存。我通過使用此代碼來實現這一點:

NSPredicate *predicate = [_eventStore predicateForRemindersInCalendars:nil]; 
[_eventStore fetchRemindersMatchingPredicate:predicate completion:^(NSArray *reminders) { 
    for (EKReminder *reminder in reminders) { 
     NSLog(@"Reminder Title: %@", reminder.title); 
    } 
}]; 

而我的新提醒永遠不會顯示!

任何人都可以告訴我我在做什麼錯嗎?

回答

2

如果授權狀態爲EKAuthorizationStatusNotDetermined,則只需撥打-requestAccessToEntityType:completion:即可。您可以使用+[EKEventStore authorizationStatusForEntityType:]檢查授權狀態。

完成塊將只在第一次調用-requestAccessToEntityType:completion時被調用。在模擬器上它甚至不會被調用一次,因爲在模擬器上你的應用程序已經可以訪問。

EKEventStore * _eventStore = [[EKEventStore alloc] init]; 
EKAuthorizationStatus authorizationStatus = [EKEventStore authorizationStatusForEntityType:EKEntityTypeReminder]; 
if (authorizationStatus == EKAuthorizationStatusNotDetermined) { 
    [_eventStore requestAccessToEntityType:EKEntityTypeReminder completion:^(BOOL granted, NSError *error) { 
     if (granted) { 
      [self createNewReminder]; 
     } else { 
      [self showNowAccessAlert]; 
     } 
    }]; 
} else { 
    if (authorizationStatus == EKAuthorizationStatusAuthorized) { 
     [self createNewReminder]; 
    } else { 
     [self showNowAccessAlert]; 
    } 
} 
+0

這是真的。感謝您的洞察力 – 2013-03-07 00:01:30

0

我得到這個工作,通過切換localSource是EKSourceTypeCalDAV。不完全確定爲什麼這是必需的,也許有人可以闡明這一點?

+0

您正在將'EKSource'類型的'localSource'分配給'EKCalendar'類型的'calendar',這可能是一個壞主意。你爲什麼不簡單地使用默認日曆? – 2013-03-07 07:19:24

+0

這是一個錯字。修復。我沒有使用默認日曆,因爲我希望能夠將我的提醒與活動商店中的所有其他提醒區分開來。 – 2013-03-07 18:17:52

+0

好吧。我不知道爲什麼本地資源不起作用。你有沒有嘗試設置日曆的標題? – 2013-03-07 19:20:51

0

希望這對你有用。

if([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)]) 
    { 
     // iOS 6 and later 
     // asks user to allow application to use his device calendar 
     [eventStore requestAccessToEntityType:EKEntityTypeReminder completion:^(BOOL granted, NSError *error) 
     { 
      if (granted) 
      { 
       EKReminder *reminder = [EKReminder reminderWithEventStore:eventStore]; 
       reminder.title = aNotification; 
       reminder.calendar = [eventStore defaultCalendarForNewReminders]; 
       NSDate *date = //aDate; 
       EKAlarm *alarm = [EKAlarm alarmWithAbsoluteDate:date]; 
       [reminder addAlarm:alarm]; 
       NSError *error = nil; 
       [eventStore saveReminder:reminder commit:YES error:&error]; 

       if(error) 
        NSLog(@"unable to Reminder!: Error= %@", error); 
      } 
     }]; 
    } 
    // iOS < 6 
    else 
    { 
     EKReminder *reminder = [EKReminder reminderWithEventStore:eventStore]; 
     reminder.title = aNotification; 
     reminder.calendar = [eventStore defaultCalendarForNewReminders]; 
     NSDate *date = //aDate; 
     EKAlarm *alarm = [EKAlarm alarmWithAbsoluteDate:date]; 
     [reminder addAlarm:alarm]; 
     NSError *error = nil; 
     [eventStore saveReminder:reminder commit:YES error:&error]; 

     if(error) 
      NSLog(@"unable to Reminder!: Error= %@", error); 
    } 

如果您在將事件保存到設備日曆時遇到問題,請檢查我的this post