来自调试器的消息:由于信号9而终止

时间:2018-07-09 07:03:39

标签: ios objective-c ekevent

im尝试使用我的应用创建事件,然后使用以下代码将其添加到日历中:

- (IBAction)refreshTapped:(id)sender {
   // [self performSegueWithIdentifier:@"calendarsegue" sender:nil];
    EKEventStore *store = [[EKEventStore alloc] init];

    if([store respondsToSelector:@selector(requestAccessToEntityType:completion:)])
    {
        // iOS 6
        [store requestAccessToEntityType:EKEntityTypeEvent
                              completion:^(BOOL granted, NSError *error) {
                                  if (granted)
                                  {
                                      dispatch_async(dispatch_get_main_queue(), ^{
                                          [self createEventAndPresentViewController:store];
                                      });
                                  }
                              }];
    } else
    {
        // iOS 5
        [self createEventAndPresentViewController:store];
    }
}




   - (void)createEventAndPresentViewController:(EKEventStore *)store
    {
        EKEvent *event = [self findOrCreateEvent:store];

        EKEventEditViewController *controller = [[EKEventEditViewController alloc] init];
        controller.event = event;
        controller.eventStore = store;
        controller.editViewDelegate = self;

        [self presentViewController:controller animated:YES completion:nil];
    }

- (EKEvent *)findOrCreateEvent:(EKEventStore *)store
{
    NSString *title = @"My event title";

    // try to find an event

    EKEvent *event = [self findEventWithTitle:title inEventStore:store];

    // if found, use it

    if (event)
        return event;

    // if not, let's create new event

    event = [EKEvent eventWithEventStore:store];

    event.title = title;
    event.notes = @"";
   // event.location = @"Lebanon";
    event.calendar = [store defaultCalendarForNewEvents];

    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [[NSDateComponents alloc] init];
    components.hour = 4;
    event.startDate = [calendar dateByAddingComponents:components
                                                toDate:[NSDate date]
                                               options:0];
    components.hour = 1;
    event.endDate = [calendar dateByAddingComponents:components
                                              toDate:event.startDate
                                             options:0];

    return event;
}
- (EKEvent *)findEventWithTitle:(NSString *)title inEventStore:(EKEventStore *)store
{
    // Get the appropriate calendar
    NSCalendar *calendar = [NSCalendar currentCalendar];

    // Create the start range date components
    NSDateComponents *oneDayAgoComponents = [[NSDateComponents alloc] init];
    oneDayAgoComponents.day = -1;
    NSDate *oneDayAgo = [calendar dateByAddingComponents:oneDayAgoComponents
                                                  toDate:[NSDate date]
                                                 options:0];

    // Create the end range date components
    NSDateComponents *oneWeekFromNowComponents = [[NSDateComponents alloc] init];
    oneWeekFromNowComponents.day = 7;
    NSDate *oneWeekFromNow = [calendar dateByAddingComponents:oneWeekFromNowComponents
                                                       toDate:[NSDate date]
                                                      options:0];

    // Create the predicate from the event store's instance method
    NSPredicate *predicate = [store predicateForEventsWithStartDate:oneDayAgo
                                                            endDate:oneWeekFromNow
                                                          calendars:nil];

    // Fetch all events that match the predicate
    NSArray *events = [store eventsMatchingPredicate:predicate];

    for (EKEvent *event in events)
    {
        if ([title isEqualToString:event.title])
        {
            return event;
        }
    }

    return nil;
}

- (void)eventEditViewController:(EKEventEditViewController *)controller didCompleteWithAction:(EKEventEditViewAction)action
{
    [self dismissViewControllerAnimated:YES completion:nil];
   // [self fetchCalendarEvents];
}

事件页面正常打开,但是当我想通过键盘输入位置时,应用崩溃了,标题为enter image description here的错误提示给我 有人知道错误在哪里吗?谢谢

0 个答案:

没有答案