requestAccessToEntity iOS6- التوافق مع الإصدارات السابقة - EKEventStore

StackOverflow https://stackoverflow.com//questions/12660206

  •  11-12-2019
  •  | 
  •  

سؤال

بعد iOS6 eventsKit وإعدادات الخصوصية الجديدة، أستخدم الكود التالي - والذي يعمل بشكل جيد على أجهزة 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();
}

أعتقد أن هذا يجب أن يقلل من تكرار التعليمات البرمجية.

نصائح أخرى

إذا قمت بعرض الحدث في modalviewcontroller باستخدام EKEventEditViewController، فسيعرض لك iOS تلقائيًا طريقة عرض توضح ما يجب فعله إذا تم رفض الإذن.لذلك أفعل هذا:

في العرضDidLoad:

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).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top