2013-02-04 121 views
0

如何在没有权限时显示错误? (例如,“你不要让我们持续写日历。”)。权限添加日历活动iOS

这是我的代码:

if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"6.0")) { 

EKEventStore *es = [[EKEventStore alloc] init]; 
[es requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) { 
    /* This code will run when uses has made his/her choice */ 
}]; 
} 
     NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init]; 
     [dateFormatter setDateFormat:@"ddMMyy"]; 

     NSString *fechainicio = datum; 
     NSString *fechafin = datum; 
     titel = [NSString stringWithFormat:@"%@ (%@)", titel, plaatslabel.text]; 

     NSDate * date = [[NSDate alloc] init]; 
     date = [dateFormatter dateFromString:fechainicio]; 
     NSDate * date2 = [[NSDate alloc] init]; 
     date2 = [dateFormatter dateFromString:fechafin]; 

     EKEventStore *eventStore = [[EKEventStore alloc] init]; 

     EKEvent *event = [EKEvent eventWithEventStore:eventStore]; 
     event.title  = @"Optreden Ronald Goedemondt"; 
     event.location = titel; 
     event.allDay = YES; 

     event.startDate = date; 
     event.endDate = date2; 

     [event setCalendar:[eventStore defaultCalendarForNewEvents]]; 
     NSError *err; 
     [eventStore saveEvent:event span:EKSpanThisEvent error:&err]; 

WBSuccessNoticeView *notice = [WBSuccessNoticeView successNoticeInView:self.view title:@"Show geplaatst in uw agenda."]; 
[notice show]; 

回答

8

granted将是如果授予访问权限或者YESNO其他。
此外,你应该确保你只调用-requestAccessToEntityType:completion:时,它必须这样做:

EKEventStore *es = [[EKEventStore alloc] init]; 
EKAuthorizationStatus authorizationStatus = [EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent]; 
BOOL needsToRequestAccessToEventStore = (authorizationStatus == EKAuthorizationStatusNotDetermined); 

if (needsToRequestAccessToEventStore) { 
    [es requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) { 
     if (granted) { 
      // Access granted 
     } else { 
      // Denied 
     } 
    }]; 
} else { 
    BOOL granted = (authorizationStatus == EKAuthorizationStatusAuthorized); 
    if (granted) { 
     // Access granted 
    } else { 
     // Denied 
    } 
} 
+0

谢谢,它效果很好! – AYS

3

这是我使用的代码。工程就像一个魅力:

-(void)askPermissionForCalendarAccess { 
    EKEventStore *eventStore = [[EKEventStore alloc] init]; 
    /* iOS 6 requires the user grant your application access to the Event Stores */ 
    if ([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)]) 
    { 
     /* iOS Settings > Privacy > Calendars > MY APP > ENABLE | DISABLE */ 
     [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) { 

      if (granted) { 

       NSLog(@"granted"); 
       //This method checks to make sure the calendar I want exists, then move on from there... 
       [self checkForCalendar]; 

      } else { 

       //put error popup code here. 
       NSLog(@"denied"); 
       [self performSelectorOnMainThread:@selector(showDeniedAccessAlert) withObject:nil waitUntilDone:NO]; 
      } 
     }]; 
    } 
} 
+0

我会用self.eventStore respondsToSelector得到错误... – AYS

+0

现在就来试试。我为你调整了我的代码。我使用事件存储作为我的视图控制器的属性.. – jhilgert00

+0

当访问被拒绝时,应用程序崩溃。 – AYS