defaultCalendarForNewEvents failed

前端 未结 8 2242
北海茫月
北海茫月 2020-11-29 01:25

When I try to call [newEventStore defaultCalendarForNewEvents], it returns an error message says:

[707:907] defaultCalendarForNewEvents failed: Error Domain         


        
相关标签:
8条回答
  • 2020-11-29 01:58

    Ios app wont be able to get any data from the Calendar on the iOS6 system if you don’t call the – requestAccessToEntityType:completion: function to prompt a dialog to ask your users to grant access to your app to access the Calendar/Reminder . Code will look like:

    //CalendarEventHandler.h  
    @interface CalendarEventHandler : NSObject
    {
    EKEventStore *eventStore;
    }
    @property (nonatomic, strong) EKEventStore *eventStore;
    
    
    //CalendarEventHandler.m 
    self.eventStore =[[EKEventStore alloc] init];
    if([self checkIsDeviceVersionHigherThanRequiredVersion:@"6.0"]) {
            [self.eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
    
                if (granted){
                    //---- codes here when user allow your app to access theirs' calendar.
    
                }else
                {
                    //----- codes here when user NOT allow your app to access the calendar.  
                }  
            }];
    
        }else {
                    //---- codes here for IOS < 6.0.
    
        }
    

    // Below is a block for checking is current ios version higher than required version.

     - (BOOL)checkIsDeviceVersionHigherThanRequiredVersion:(NSString *)requiredVersion
     {
      NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
    
       if ([currSysVer compare:requiredVersion options:NSNumericSearch] != NSOrderedAscending)
       {
           return YES;
       }
    
           return NO;
      }
    
    0 讨论(0)
  • 2020-11-29 02:01

    Go to Simulator/Device settings/Privacy/Calendars/YourApp Tap ON to allow calendar access. And retry your code.It will work.

    0 讨论(0)
  • 2020-11-29 02:09

    Solved. I accidentally turn off the app access to calendar in Setting->Privacy on IOS6

    0 讨论(0)
  • 2020-11-29 02:11

    Swift form

    (based on @yunas answer)

    _estore = EKEventStore()
    _estore.reset()
    
    _estore.requestAccessToEntityType(EKEntityTypeEvent) { (granted, error) in
        if granted {
            println("allowed")
            /* ... */
        } else {
            println("not allowed")
        }           
    }
    

    It will open "Access" pop-up

    0 讨论(0)
  • 2020-11-29 02:11

    For Xamarin users:

    EKEventStore eventStore = new EKEventStore();
    eventStore.RequestAccess(EKEntityType.Event, (bool granted, NSError e) =>
    {
        if (granted)
        {
           //Your code here when user gief permissions
        }
    });
    
    0 讨论(0)
  • 2020-11-29 02:16

    On iOS6, Apple introduced a new privacy control that allows the user to control the accessibility of contacts and calenders for each app. So, on the code side, you need to add some way to request the permission. In iOS5 or before, we can always call

    EKEventStore *eventStore = [[[EKEventStore alloc] init] autorelease];
    if ([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)]) {
        // iOS 6 and later
        [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
            if (granted) {
                // code here for when the user allows your app to access the calendar
                [self performCalendarActivity:eventStore];
            } else {
                // code here for when the user does NOT allow your app to access the calendar
            }
        }];
    } else {
        // code here for iOS < 6.0
        [self performCalendarActivity:eventStore];
    }
    
    0 讨论(0)
提交回复
热议问题