无法在日历中添加事件 [英] Unable to add event in calendar

查看:233
本文介绍了无法在日历中添加事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在someName日历中添加事件。如果一个具有给定名称的日历不存在,那么我将以编程方式创建一个。我的问题是当localSource(类型为EKSource)被证明为null时,不会添加该事件。我添加了3个检查,以确保我得到一个localSource的值,但即使在某些情况下,localSource是零。所以在我的手机事件得到添加,但在我朋友的手机,他们没有。

I want to add events in "someName" calendar. In case, a calendar with the given name doesn't exist then I will create one programatically. My problem is that the event does not get added when localSource (of type EKSource) turns out to be null. I added 3 checks to make sure that I get a value of localSource but even then in some cases localSource is nil. So on my phone events get added but on my friend's phone they don't.

我遵循了各种帖子,我了解到EKSource可以有6种类型: https://developer.apple.com/reference/eventkit/eksourcetype

I followed various posts and I understood that EKSource can be of 6 types: https://developer.apple.com/reference/eventkit/eksourcetype

我不明白是在什么case localSource将为零?这在正常语言中是什么意思?我可以从代码中做一些事情,使其无法使用,还是设备上的用户必须完成某些操作?

What I fail to understand is in what cases localSource would be nil? What does this mean in normal language? Can I do something from the code to make it non-nil or something has to be done by the user on device?

- (void)setCalendar {
    NSArray *calendars = [self.eventStore calendarsForEntityType:nil];
    NSString *calendarTitle = someName;
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"title matches %@", calendarTitle];
    NSArray *filtered = [calendars filteredArrayUsingPredicate:predicate];
    if ([filtered count]) {
        self.calendar = [filtered firstObject];
    }
    else {
        self.calendar = [EKCalendar calendarForEntityType:EKEntityTypeEvent eventStore:self.eventStore];
        self.calendar.title = calendarTitle;
        EKSource *localSource;
        for (EKSource *source in self.eventStore.sources)
        {

            //if iCloud account is setup then add the event in that calendar
            if (source.sourceType == EKSourceTypeCalDAV && [source.title isEqualToString:@"iCloud"])
            {
                localSource = source;
                break;
            }
        }
        if (localSource == nil)
        {
            for (EKSource *source in self.eventStore.sources)
            {
                //if iCloud is not setup then look for local source
                if (source.sourceType == EKSourceTypeLocal)
                {
                    localSource = source;
                    break;
                }
            }

        }
        if (!localSource) {
            localSource = [self.eventStore defaultCalendarForNewEvents].source;
        }
        self.calendar.source = localSource;
        NSError *calendarErr = nil;
        BOOL calendarSuccess = [self.eventStore saveCalendar:self.calendar commit:YES error:&calendarErr];
        if (!calendarSuccess) {
            NSLog(@"Error while updating calendar %@", calendarErr);
        }
    }

}

PS:我有权添加日历活动。

PS: I have permission to add calendar events.

推荐答案

本地源代码描述了在我的iphone ','icloud','gmail'在用户的日历。在我的代码中,当用户设置了icloud帐户但没有给予icloud帐户写入日历的权限时,本地源代码为null。所以即使应用程序有权限写入日历,但是本地的来源为零,因此日历事件的添加失败。

local source depicts the fields like 'on my iphone', 'icloud', 'gmail' in user's calendar. In my code, local source is null when the user has setup icloud account but did not give permissions to icloud account to write to calendar. So even if the app has the permission to write to calendar but local source is nil, hence, addition of event to calendar fails.

我的代码添加到给定的日历中2例:

My code adds to calendar in the given 2 cases:


  1. 用户已登录icloud,并已授予icloud写入日历的权限。在这种情况下,本地来源不为零,并且在iCloud中创建名称为someName的日历。

  2. 用户尚未登录到icloud。现在,在我的iphone本地源代码上创建名称为someName的日历。

这篇关于无法在日历中添加事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆