【问题标题】:How to get the name/id of default calendar in iOS 6如何在 iOS 6 中获取默认日历的名称/ID
【发布时间】:2012-09-27 01:42:14
【问题描述】:

我的 iPad 上有两 (2) 个日历 (iCal)(一个用于个人,一个用于应用程序)。它们同步到我的 iMac 仅用于测试。 (节省了我输入特定应用日历的时间)。

我目前正在编写一个需要访问应用程序日历的应用程序。它是 iPad 上的主日历。我正在尝试让 Apple 的 SimpleEKDemo(未修改)与应用程序的日历一起使用,但到目前为止,我什至无法让它不崩溃,更不用说返回任何东西了。几个小时以来,我一直在研究 Google 和 SO 的问题,并决定是时候召集大佬了。

这是崩溃的代码:

- (void)viewDidLoad
{
    self.title = @"Events List";

    // Initialize an event store object with the init method. Initilize the array for events.
    self.eventStore = [[EKEventStore alloc] init];

    self.eventsList = [[NSMutableArray alloc] initWithArray:0];

    // Get the default calendar from store.
    self.defaultCalendar = [self.eventStore defaultCalendarForNewEvents];  //  <---- crashes here    --------

    //  Create an Add button
    UIBarButtonItem *addButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:
                                      UIBarButtonSystemItemAdd target:self action:@selector(addEvent:)];
    self.navigationItem.rightBarButtonItem = addButtonItem;
    [addButtonItem release];

    self.navigationController.delegate = self;

    // Fetch today's event on selected calendar and put them into the eventsList array
    [self.eventsList addObjectsFromArray:[self fetchEventsForToday]];

    [self.tableView reloadData];
}

这是“崩溃”的输出:

2012-10-05 14:33:12.555 SimpleEKDemo[874:907] defaultCalendarForNewEvents failed: Error Domain=EKCADErrorDomain Code=1013 "The operation couldn’t be completed. (EKCADErrorDomain error 1013.)"

我需要确保我在正确的日历上...我该怎么做?

【问题讨论】:

  • 崩溃的输出是什么?
  • 糟糕...我编辑了问题以添加它...抱歉...
  • OK... 我将代码更改为: // 从商店获取默认日历。 self.defaultCalendar = [self.eventStore calendarWithIdentifier:@"Prager Software"];但它似乎没有工作,因为它在“获取今天的事件”上崩溃,因为那里什么都没有。如何验证日历的名称?

标签: objective-c xcode4 icalendar


【解决方案1】:

您需要确保在尝试访问 Event Store 之前获得许可。请注意,您只需要调用一次。如果用户拒绝访问,他们需要转到 iOS 设置(参见代码中的注释)为您的应用启用权限。

/* 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(@"User has granted permission!");
         }
         else
         {
             NSLog(@"User has not granted permission!");
         }
     }];
}

在 iOS 5 中,您只能在 Event Store 中访问事件 (EKEntityTypeEvent),而在 iOS 6 中,您可以访问提醒 (EKEntityTypeReminder)强>)。但是您需要上述代码至少获得 1 次授权。

我还应该提到,在您访问 EventStore 之前,您需要获得许可,在您的情况下:[self.eventStore defaultCalendarForNewEvents];

另外,defaultCalendarForNewEvents 将是访问用户默认日历的正确方法。如果您希望访问具有其他名称的日历,则需要遍历日历并根据返回的结果选择合适的日历。

【讨论】:

  • 我认为我们在正确的轨道上(询问许可问题,但不等待答案)......当你说“遍历日历”时,什么我应该使用的方法会给我日历列表吗?在 iOS 6 中,“日历”已被弃用,我没有看到任何可以提供相同信息的内容。
  • 有关日历的名称,请参阅 EKCalendar title。您应该知道如何使用 for 循环遍历日历。
  • 我认为它比 -for 循环更复杂...非常感谢您的帮助...非常感谢...
  • 在 iOS 6 中使用 [self.eventStore calendarsForEntityType:EKEntityTypeEvent]; 来获取日历列表。
  • 当用户在您的应用运行时更改隐私设置时,不要担心您的应用会被 iOS 终止。这是已知/预期的行为,而不是错误:stackoverflow.com/questions/12652502/…
【解决方案2】:

//检查用户设备上是否安装了iOS6或更高版本*********强>

if([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)]) {

    //Request the access to the Calendar
    [eventStore requestAccessToEntityType:EKEntityTypeReminder completion:^(BOOL granted,NSError* error){

        //Access not granted-------------
        if(!granted){
            NSString *message = @"Hey! I Can't access your Calendar... check your privacy settings to let me in!";
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Warning"
                                                               message:message
                                                              delegate:self
                                                     cancelButtonTitle:@"Ok"
                                                     otherButtonTitles:nil,nil];
            //Show an alert message!
            //UIKit needs every change to be done in the main queue
            dispatch_async(dispatch_get_main_queue(), ^{[alertView show];});

            //Access granted------------------
        }
        else
        {
            self.defaultCalendar=[self.eventStore defaultCalendarForNewEvents];

        }
    }];
}

//Device prior to iOS 6.0  *********************************************
else{
    self.defaultCalendar=[self.eventStore defaultCalendarForNewEvents];
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多