RequestAccessToEntity ios6-後方互換性 - EkEventStore.
-
11-12-2019 - |
質問
IOS6 EventKitと新しいプライバシー設定に続く次のコードを使用しています - IOS6デバイスで完全にうまく機能します。
それでも、同じコードがiOS 5.xを持つデバイスにも機能することをお勧めします。
誰かがエレガントな解決策を助けますか?
. EKEventStore *eventStore = [[EKEventStore alloc] init];
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
// some code
}];
解決
私はこれを使っています:
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();
}
.
コードの重複を減らすべきだと思います。
他のヒント
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];
}
. 私はのeventutil.mファイルを持っています
+(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.hファイルをインポートしてこの関数を呼び出します。
[EventUtil proxyForIOS6EventKitToCallFunction:@selector(displayModifyCalendarAlertView) WithViewController:self];
.
DisplayModifyCalendarAlertViewは、カレンダ権限が指定されているかどうか(iOS6またはiOS <6)の場合は呼び出したい関数です。
所属していません StackOverflow