开发者

iPhone: How to detect if an EKEvent instance can be modified?

开发者 https://www.devze.com 2023-01-30 23:53 出处:网络
While working with the EventKit on iPhone I noticed that some events can开发者_Python百科 exist which cannot be modified. Examples I encountered so far are birthdays and events synced with CalDAV. Whe

While working with the EventKit on iPhone I noticed that some events can开发者_Python百科 exist which cannot be modified. Examples I encountered so far are birthdays and events synced with CalDAV. When you view the event's details in the standard built-in calendar app on iPhone the "Edit" button in the top-right corner is not visible in these cases, where it would be visible when viewing "normal" events.

I've searched everywhere, read all documentation there is but I simply can't find anything that tells me how to detect this behavior! I can only detect it afterwards:

  1. edit an event's title
  2. save it to the event store
  3. check the event's title, if it has not changed it is not editable!

I am looking for a way that I can detect the non-editable behavior of an event beforehand. I know this is possible because I've seen other calendar apps implement this correctly.


Ok it appears as if the SDK doesn't provide me with anything I can use to check if an EKEvent is read-only. I created a workaround by creating a category that adds an "isReadOnly" method to all EKEvent instances.

EKEvent+ReadOnlyCheck.h

@interface EKEvent(ReadOnlyCheck)
- (BOOL) isReadOnly;
@end`

EKEvent+ReadOnlyCheck.m

#import "EKEvent+ReadOnlyCheck.h"

@implementation EKEvent(ReadOnlyCheck)

- (BOOL) isReadOnly {
    BOOL readOnly;
    NSString *originalTitle = [self.title retain];
    NSString *someRandomTitle = [NSString stringWithFormat:@"%i", arc4random()];

    self.title = someRandomTitle;
    readOnly = [originalTitle isEqualToString:self.title];
    self.title = originalTitle;
    [originalTitle release];

    return readOnly;
}
@end

When the above files are in place I can simply call isReadOnly on the EKEvent of my choice.

#import "EKEvent+ReadOnlyCheck.h"
...
if ([event isReadOnly]) {
    // Do your thing
}
...


I haven't worked with Event Kit yet, but from the documentation it seems that editability is a property of a calendar, not of an event. event.calendar gets you the event's calendar, and calendar.allowsContentModifications tells you if the calendar is read-only or read-write.


try allowsEditing property of EKEventViewController before displaying the view.


Yes. It is possible. The code would look like following : Try relating to code by logging the output of objects I use with editable/non editable events and you will understand the working:)

EKEventViewController *controller = [[EKEventViewController alloc] init];
controller.event = myEvent; /*myEvent is of type EKEvent*/          
if(controller.navigationItem.leftBarButtonItem != NULL)
{
    /*Event is Editable, Your code here*/
}


I think adding this category method for EKEvent handles all cases where events are not editable:

- (BOOL)isReadOnly {
    if (self.calendar.allowsContentModifications == NO) return YES;
    if (self.organizer && [self.organizer isCurrentUser] == NO) return YES;
    return NO;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消