0

想检查EKEventStoreios6设备上是否存在权限。

EKEventStore *eventStore = [[UpdateManager sharedUpdateManager] eventStore];

if ([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)])
{
    [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
     {
         if (granted)
         {   
             addEventBlock();
         }
         else
         {
             NSLog(@"Not granted");
         }
     }];
}

此代码仅在第一次工作。

4

1 回答 1

0

像这样使用。

- (IBAction)btnAddFromDBTouched:(id)sender {
    void (^addEventBlock)();

    addEventBlock = ^
    {
        self.eventStore  = [[EKEventStore alloc] init];
        self.eventsList = [[NSMutableArray alloc] initWithArray:0];
        self.defaultCalendar = [self.eventStore defaultCalendarForNewEvents];
        NSDate *startDate = [NSDate date];

        // endDate is 1 day = 60*60*24 seconds = 86400 seconds from startDate
        NSDate *endDate = [NSDate dateWithTimeIntervalSinceNow:86400*365];

        // Create the predicate. Pass it the default calendar.
        NSArray *calendarArray = [NSArray arrayWithObject:defaultCalendar];
        NSPredicate *predicate = [self.eventStore predicateForEventsWithStartDate:startDate endDate:endDate
                                                                        calendars:calendarArray];

         NSArray *events = [self.eventStore eventsMatchingPredicate:predicate];


        nmfChooseEvent *myView = [[nmfChooseEvent alloc] init];
        [myView setTableSource:events];
        [self presentModalViewController:myView animated:YES];

        myView.mainView=self;
        return;

    };
    if ([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)])
    {
        [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
         {
             if (granted)
             {
                 addEventBlock();
             }
             else
             {
                 dispatch_async(dispatch_get_main_queue(), ^{
                     UIAlertView *myView = [[UIAlertView alloc] initWithTitle:@"warning" message:@"To be Able to Use This Function, You Must Authorize xxx in  Settings>>Privacy>>Calendars Section." delegate:nil cancelButtonTitle:"OK" otherButtonTitles:nil, nil];
                     [myView show];
                 });

             }
         }];
    }
    else
    {
        addEventBlock();
    }

}
于 2013-01-28T22:46:36.060 回答