想要检查ios6设备上是否存在EKEventStore权限

时间:2013-01-11 10:54:08

标签: ios6 ekeventstore

想要检查ios6设备上是否存在EKEventStore权限。

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");
         }
     }];
}

此代码仅在第一时间有效。

1 个答案:

答案 0 :(得分: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();
    }

}