簡體   English   中英

iPhone:如何檢測是否可以修改EKEvent實例?

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

在iPhone上使用EventKit時,我注意到可能存在一些無法修改的事件。 我到目前為止遇到的例子是生日和與CalDAV同步的事件。 當您在iPhone上的標准內置日歷應用程序中查看事件的詳細信息時,右上角的“編輯”按鈕在這些情況下不可見,在查看“正常”事件時可以看到該按鈕。

我到處搜索,閱讀所有文檔,但我根本找不到任何告訴我如何檢測這種行為! 我之后只能檢測到它:

  1. 編輯活動的標題
  2. 將其保存到事件存儲區
  3. 檢查事件的標題,如果沒有更改,則不可編輯!

我正在尋找一種方法,我可以預先檢測事件的不可編輯行為。 我知道這是可能的,因為我已經看到其他日歷應用程序正確實現了這一點。

好吧,似乎SDK沒有向我提供任何我可以用來檢查EKEvent是否為只讀的東西。 我創建了一個解決方法,創建了一個向所有EKEvent實例添加“isReadOnly”方法的類別。

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

當上面的文件到位時,我可以在我選擇的EKEvent上調用isReadOnly

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

我還沒有使用Event Kit,但從文檔中可以看出,可編輯性是日歷的屬性,而不是事件的屬性。 event.calendar您提供事件的日歷, calendar.allowsContentModifications告訴您日歷是只讀還是讀寫。

是。 有可能的。 代碼如下所示:通過使用可編輯/不可編輯的事件記錄我使用的對象的輸出來嘗試與代碼相關,您將了解工作:)

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

在顯示視圖之前嘗試使用EKEventViewController的allowEditing屬性。

我認為為EKEvent添加此類別方法可處理事件不可編輯的所有情況:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM