2015-10-13 67 views
0

xCode中的Objective-C是否可以打開本機iPhone的「添加事件」日曆提示,其中有幾個字段已填充?例如姓名,地址和開始/結束日期?如果是這樣,怎麼樣? 這將允許用戶仍然改變幾個參數:他什麼時候想要提醒,等等。iOS - 允許用戶添加日曆事件,其中填充了一些字段

我環顧四周,但我找到的所有方法都是自動添加事件,而無需用戶確認。

回答

0

下面是我如何處理它...與EKEventEditViewController!

第一:

@import EventKitUI; 

在您.m文件的最頂端。 然後設置EKEventEditViewDelegate

然後,當你要添加的事件,用下面的方法:

- (IBAction)addToCalendar:(id)sender { 
    EKEventStore *eventStore = [[EKEventStore alloc] init]; 
    if ([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)]) 
    { 
     // the selector is available, so we must be on iOS 6 or newer 
     [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) { 
      dispatch_async(dispatch_get_main_queue(), ^{ 
       if (error) 
       { 
        NSLog(@"%@", error); 
        // display error message here 
       } 
       else if (!granted) 
       { 
        NSLog(@"%@ acce sdenied", error); 
        // display access denied error message here 
       } 
       else 
       { 
        EKEvent *event = [EKEvent eventWithEventStore: eventStore]; 
        event.title = nom; 
        event.location = adresse; 

        // Set the start and end dates to the event. 
        event.startDate = startDate; 
        event.endDate = endDate; // 
        EKEventEditViewController *eventViewController = [[EKEventEditViewController alloc] init]; 
        eventViewController.event = event; 
        eventViewController.eventStore=eventStore; 
        eventViewController.editViewDelegate = self; 
        [event setCalendar:[eventStore defaultCalendarForNewEvents]]; 
        [eventViewController setModalPresentationStyle:UIModalPresentationFullScreen]; 
        [self presentViewController:eventViewController animated:YES completion:NULL]; 
       } 
      }); 
     }]; 
    } 
} 

最後,添加此委託方法來處理完成的動作:

-(void)eventEditViewController:(EKEventEditViewController *)controller 
     didCompleteWithAction:(EKEventEditViewAction)action { 
    NSError *error; 
    switch (action) { 
     case EKEventEditViewActionCancelled: 
      // User tapped "cancel" 
      NSLog(@"Canceled"); 
      break; 
     case EKEventEditViewActionSaved: 
      NSLog(@"Saved"); 
      [controller.eventStore saveEvent:controller.event span: EKSpanFutureEvents error:&error]; 
      [calendarBouton setTitle:@"Ajouté!" forState:UIControlStateDisabled]; 
      calendarBouton.enabled = NO; 
      break; 
     case EKEventEditViewActionDeleted: 
      // User tapped "delete" 
      NSLog(@"Deleted"); 
      break; 
     default: 
      NSLog(@"Default"); 
      break; 
    } 

    [self dismissViewControllerAnimated:YES completion:nil]; 

} 
1

步驟1 先日曆權限

dispatch_async(dispatch_get_main_queue(), ^{ 


    [self.eventManager.eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) { 



     if(granted==NO) 
     { 

      BOOL permission=[[NSUserDefaults standardUserDefaults] boolForKey:@"CalendarPermissionAlert"]; 

      if(permission==NO) { 

       kAppDelegateObject.eventManager.eventsAccessGranted=NO; 

       [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"CalendarPermissionAlert"]; 

       UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Calendar Access is OFF" 
                    message:kCalenderResetMessage 
                    delegate:self 
                 cancelButtonTitle:@"CANCEL" 
                 otherButtonTitles:@"SETTING",nil]; 
       [alert show]; 
       alert.tag=101; 

       return ; 
      } 


     } 

步驟2

//添加事件

-(void)addEventWithMessage:(NSString*)eventMessage withEventDate:(NSDate *)eventDate 
EKEventStore *eventStore; 
eventStore = [[EKEventStore alloc] init]; 

//創建一個新的事件對象。 EKEvent * event = [EKEvent eventWithEventStore:eventStore];

// Set the event title. 
event.title = eventMessage; 

// Set its calendar. 


NSString *identifier=[[NSUserDefaults standardUserDefaults]objectForKey:@"calenderId"]; //your application id 
// NSLog(@"cal identifier: %@",identifier); 
event.calendar = [eventStore calendarWithIdentifier:identifier]; 

//set Alarm 
NSTimeInterval secondsInOneHours = 1 * 60 * 60; 

NSDate *dateOneHoursAhead = [eventDate dateByAddingTimeInterval:secondsInOneHours]; 

// Set the start and end dates to the event. 
event.startDate = eventDate; 
event.endDate = dateOneHoursAhead; // 

NSError *error; 
    if ([eventStore saveEvent:event span:EKSpanFutureEvents commit:YES error:&error]) { 

    // NSLog(@"Event Added"); 
} 
else{ 
    // An error occurred, so log the error description. 
    // NSLog(@"%@", [error localizedDescription]); 
} 
+0

謝謝您的幫助!你能否正確地重新加載你的代碼?很難看到代碼中究竟是什麼,或者不是!非常感謝 –

+0

EventManager從哪裏來? –

+0

EventManager是NSObject類,在這裏我聲明瞭EKEventStore對象。 EventManager.h @property(nonatomic,strong)EKEventStore * eventStore; EventManager.m - (instancetype)init { self = [super init]; (self){ eventStore = [[EKEventStore alloc] init]; } return self; } –