2013-06-04 44 views
1

我一直在爲我的應用添加待辦事項功能。雖然一切看起來都很正常,但我在提醒中遇到了一個失敗的創建,但錯誤值爲NIL。下面是相關的代碼 - (我拉文本,並從一個UIViewController日 - 一切都正確連接).`提醒創建失敗,錯誤消息爲NIL

-(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顯示以下內容: 「錯誤保存提醒:(空)」

+0

'的NSLog(@ 「%@」,商店);'權前提醒節省,並看看有什麼給你。 – Undo

+0

store is null ...這意味着我沒有權限訪問日曆...從我能告訴的 –

+0

檢入設置並確保該應用程序具有訪問日曆的權限 - 設置>隱私>日曆>你的應用程序(應該是ON) – Undo

回答

1

下面的代碼添加到viewDidLoad中功能

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