2012-09-30 74 views
3

下面的iOS6 eventKit和新的隱私設置我使用下面的代碼 - 這在iOS6設備上工作得很好。requestAccessToEntity iOS6-向後兼容性 - EKEventStore

不過,我想要相同的代碼也適用於iOS 5.x設備,我不希望兩次寫入「相同的代碼」 - 看起來不對。

任何人都可以協助優雅的解決方案

EKEventStore *eventStore = [[EKEventStore alloc] init]; 
    [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) { 

// some code 


}]; 

回答

9

我使用這個:

void (^addEventBlock)(); 

addEventBlock =^
{ 
    NSLog(@"Hi!"); 
}; 

EKEventStore *eventStore = [[UpdateManager sharedUpdateManager] eventStore]; 

if ([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)]) 
{ 
    [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) 
    { 
     if (granted) 
     { 
      addEventBlock(); 
     } 
     else 
     { 
      NSLog(@"Not granted"); 
     } 
    }]; 
} 
else 
{ 
    addEventBlock(); 
} 

我認爲應該減少代碼的重複。

+0

嗯,不是最優雅的解決方案,但我可以忍受它,謝謝。 – chewy

4

如果您在帶有EKEventEditViewController的modalviewcontroller中顯示事件,iOS會自動向您顯示一個視圖,說明拒絕權限時該怎麼做。所以,我這樣做:

在viewDidLoad中:

eventStore = [[EKEventStore alloc] init]; 

在用來添加事件的方法:

if ([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)]) 
{ 
    __block typeof (self) weakSelf = self; // replace __block with __weak if you are using ARC 
    [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) 
    { 
     if (granted) 
     { 
      [weakSelf performSelectorOnMainThread:@selector(addEventToCalendar) withObject:nil waitUntilDone:YES]; 
     } 
     else 
     { 
      NSLog(@"Not granted"); 
     } 
    }]; 
} 
else 
{ 
    [self addEventToCalendar]; 
} 
+0

我有這個問題,它不等待,直到它完成,所以我得到null在我所有事件的數組中,我不能使用我想要的地方和3秒後它得到打印我的所有事件數組如何過來這個 –

0

我有

+(void)proxyForIOS6EventKitToCallFunction:(SEL)function WithViewController:(UIViewController*)viewController { 
    #pragma clang diagnostic push 
    #pragma clang diagnostic ignored "-Warc-performSelector-leaks" 
    AppDelegate *app = (AppDelegate*)[[UIApplication sharedApplication] delegate]; 
    if([app.eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)]) { 
     // For iOS 6 
     MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:viewController.view animated:YES]; 
     hud.labelText = @""; 
     //invoke requestAccessToEntityType... 
     [app.eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) { 
      //Handle the response here… 
      //Note: If you prompt the user, make sure to call the main thread 
      if (granted == YES) { 
       dispatch_async(dispatch_get_main_queue(), ^{ 
        [viewController performSelector:function]; 
       }); 
      } 
     }]; 
    } 
    else { 
     [viewController performSelector:function]; 
    } 
    #pragma clang diagnostic pop 
} 
的EventUtil.m文件

而在我想要訪問日曆的視圖控制器中,我導入EventUtil.h文件並調用此函數:

[EventUtil proxyForIOS6EventKitToCallFunction:@selector(displayModifyCalendarAlertView) WithViewController:self]; 

displayModifyCalendarAlertView是我想如果日曆權限被賦予(無論是iOS6的或iOS < 6)來調用函數。