How to access an EKCalendar's `account` proper

2020-04-07 15:46发布

Take a look at the documentation for EKCalendar. It's pretty simple, it has five properties, only one of which is a string called title. Now if you have multiple calendars on your iPhone and you open iCal's Calendar settings, you can see that all calendars are nicely grouped by another string called account.

What I can't figure out, is how to do the same, because although you can see the account when you NSLog a EKCalendar instance, you can't access it:

EKCalendar <0x1851b0> {title = Work; type = CalDAV; account = some@addr.ess; allowsModify = YES; color = 0.690196 0.152941 0.682353 1.000000}

There is no account property, and trying to access the valueForKey:@"account" isn't working either, unsurprisingly.

So how do I get to that account property? Such a simple thing, driving me nuts! Help is much appreciated.

Update: since iOS 5.0, EKCalendar has a source property.

3条回答
冷血范
2楼-- · 2020-04-07 16:26

If you use class-dump on the iOS 4.3 (Simulator) SDK, you'll see that there's a read-only method -(id)accountName. However, since it's not in the headers, it's unfortunately private API and you can't use it if you want your App to be accepted in the App Store.

I recommend that you file an enhancement request with Apple requesting that this method be made public.

Though thinking about it: If you're really desperate, why not parse the output of -[EKCalendar description]? It's very very very bad style and it'll probably break in the future, but you might make it through the App Store review ;) In particular if you only use it for grouping and write your code extremely defensive so it doesn't break but simply doesn't group, should the output of the description method be formatted differently.

查看更多
混吃等死
3楼-- · 2020-04-07 16:29

This information is not available in the 4.0 SDK. Have you looked at the developer previews of the forthcoming SDK to see if that might contain more information?

查看更多
我想做一个坏孩纸
4楼-- · 2020-04-07 16:49

And on iOS6? It seems the private property [EKCalendar accountName] vanished. And [EKCalendar description] does not contain account anymore. :

EKCalendar <0x200f50e0> {title = test@gmail.com; type = Exchange; allowsModify = YES; color = #44A703;}

The [EKCalendar source] provides a EKSource object, that has a title but this is not the name I typed when I created the account but seems to be a more generic name, e.g. Exchange, Other, CalDAV.

查看更多
登录 后发表回答