5

我正在尝试从我的应用程序将事件保存到日历中。我的代码适用于 iOS 7,但在iOS 6上,它返回No calendar has been set。

在 iOS 7 上,应用程序提示用户授予对日历的访问权限。但在 iOS 6 上不会出现此类提示。尽管应用程序在设置->隐私->日历中被授予访问权限。

是的,我已经实施了requestAccessToEntityType:completion:.

这是我的代码片段

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

if ([objStore respondsToSelector:@selector(requestAccessToEntityType:completion:)])
{
    // iOS 6 and later
    [objStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            if (granted)
        {
            // code here for when the user allows your app to access the calendar
            EKEvent *calEvent = [EKEvent eventWithEventStore:objStore];
            calEvent.title = mstrTitleEvent;
            calEvent.startDate = self.dateToBeSet;
            calEvent.endDate = self.dateToBeSet;
            calEvent.calendar = objStore.defaultCalendarForNewEvents;

            EKAlarm *objAlarm = [EKAlarm alarmWithAbsoluteDate:self.dateToBeSet];
            [calEvent addAlarm:objAlarm];

            NSError *error;
            BOOL _bStatus = [objStore saveEvent:calEvent span:EKSpanThisEvent commit:YES error:&error];

            UIAlertView *alertV;

            if(_bStatus)
            {
                alertV = [[UIAlertView alloc]initWithTitle:@"Congratulations" message:@"Saved To Calendar" delegate:nil cancelButtonTitle:@"Right On!" otherButtonTitles:nil];
                [alertV show];
            }
            else
            {
                alertV = [[UIAlertView alloc]initWithTitle:@"Alert" message:[NSString stringWithFormat:@"Error saving to calendar, with error %@.",[error localizedDescription]] delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
                [alertV show];
            }
        }
            else
        {
            // code here for when the user does NOT allow your app to access the calendar
            UIAlertView *alertV = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"Please grant access to the calendar, and try again later." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
            [alertV show];
        }
        });
    }];

}

4

2 回答 2

1

只是设法以某种方式找到解决我的问题的方法。我必须从一个页面导航到另一个页面,因此将链接发布到两个页面。

首先-> https://discussions.apple.com/message/16497282#16497282

然后,从那里到 -> https://discussions.apple.com/message/16479587#16479587

我必须进入Settings>iCloud>并打开Calendars。在那之后,我尝试尝试运行我的代码,它再次运行良好。

如果您遇到类似问题,请尝试此操作。

我在 iPad 2 上工作,并在设备上安装了 iOS 6.1.3。

于 2014-05-21T06:49:07.480 回答
0

太棒了,我正在两种不同的设备上测试我的代码,一种可以正常工作,另一种不能正常工作。刚刚在设置中查看,它不工作的那个关闭了 iCloud 日历和提醒,只需打开它们,现在一切正常......这一定是一个错误

于 2017-04-06T08:20:17.690 回答