문제

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

및 뷰 컨트롤에서 캘린더에 액세스하려는 CaleneTil.h 파일을 가져 와서이 함수를 호출합니다.

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

DisplayModifyCalendAraLertView는 캘린더 권한이 주어지면 호출하려는 함수입니다 (iOS6 또는 iOS <6).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top