2016-08-09 6 views
1

액세스가 거부 된 일정에 일정을 추가하려고 할 때 내 EventKit에게 일정에 대한 사용 권한을 요청하고 싶습니다. 앱을 처음 사용할 때만 묻고 설정에서 액세스를 거부하면 사용자에게 다시 액세스 권한을 부여하라는 메시지가 표시되지 않습니다. 도와주세요.이벤트 키드 권한

추가 크레딧 : 이벤트가 이미 캘린더에 존재하는지 확인하고, 그렇다면 편집하십시오. 이것에 대한 어떤 도움도 인정 될 것입니다 !!

func addToCalendar(){ 
    let eventStore = EKEventStore() 

    let startDate = NSDate() 
    let endDate = startDate.dateByAddingTimeInterval(60 * 60) // One hour 

    if (EKEventStore.authorizationStatusForEntityType(.Event) != EKAuthorizationStatus.Authorized) { 
     eventStore.requestAccessToEntityType(.Event, completion: { 
      granted, error in 
      self.createEvent(eventStore, title: "\(self.meal.text!)", startDate: startDate, endDate: endDate) 
     }) 
    } else { 
     createEvent(eventStore, title: "\(self.meal.text!)", startDate: startDate, endDate: endDate) 
    } 
} 

func createEvent(eventStore: EKEventStore, title: String, startDate: NSDate, endDate: NSDate) { 
    let event = EKEvent(eventStore: eventStore) 

    event.title = title 
    event.startDate = startDate 
    event.endDate = endDate 
    event.calendar = eventStore.defaultCalendarForNewEvents 
    event.notes = "\(self.Recipe.text!) - See Meal Planning on Listacular" 

    do { 
     try eventStore.saveEvent(event, span: .ThisEvent) 
     savedEventId = event.eventIdentifier 
    } catch { 

     print("Denied") 
    } 
} 

답변

3

직접 다시 액세스 권한을 부여 할 수 없습니다 프롬프트 사용자 :

여기 내 코드입니다. 그러나 권한 상태에 대한 사용자 팝업을 표시 할 수 있으며 장치 설정에서 권한을 사용하도록 요청할 수 있습니다. 다음은 objective-c의 예입니다

EKEventStore *store = [EKEventStore new]; 
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) { 
    if (!granted) 
    { 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      UIAlertController* alertController=[UIAlertController alertControllerWithTitle:@"Access to Calendar is Restricted" message:@"To re-enable, please go to Settings and turn on Calendar Settings for this app else Continue to create party without saving it to your Calendar" preferredStyle:UIAlertControllerStyleAlert]; 
      UIAlertAction *actionOK = [UIAlertAction actionWithTitle:@"Continue" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) 
             { 
              [self addEvent:store]; //your method 
             }]; 
      [alertController addAction:actionOK]; 
      [alertController addAction:[UIAlertAction actionWithTitle:kSETTINGS style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) 
             { 
              [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]; 
             }]]; 
      [self presentViewController:alertController animated:NO completion:nil]; 
     }); 
    } 
    else 
    { 
     [self addEvent:store]; // your method 
    } 
}]; 

이 EKEventStore 메소드를 사용하여 특정 이벤트에 대한 상점을 확인할 수 있습니다. 이벤트 식별자를 전달해야합니다.

- (nullable EKEvent *)eventWithIdentifier:(NSString *)identifier; 
+0

감사합니다. ObjC를 번역 해 주셔서 감사합니다. 알아낼 수 있었지만 eventWithIdentifier를 사용하여 이벤트를 확인하고 편집하는 방법을 모르겠습니다. –

+0

eventWithIdentifier 메소드는 이벤트가 지정된 식별자로 저장되어 있으면 이벤트를 리턴하고 그렇지 않으면 nil을 리턴합니다. –

+0

분명히 내가 잘못 했어. 저장하면'savedEventId = event.eventIdentifier'를 확인하거나 식별자를 저장하고 읽지 않습니까? –