Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 返回事件标识符表单完成块_Ios_Iphone_Ios6 - Fatal编程技术网

Ios 返回事件标识符表单完成块

Ios 返回事件标识符表单完成块,ios,iphone,ios6,Ios,Iphone,Ios6,在iOS 5的cas中,它工作是因为代码不在“块”代码中。在iOS 6或更新版本中,代码进入完成块,“addEventToCalendar”方法末尾的if-else条件首先执行。有什么办法吗?我需要报警标识符保存在数据库中 - (NSString *) addEventToCalendar { EKEventStore *eventStore = [[EKEventStore alloc] init]; __block int calChk = 0;

在iOS 5的cas中,它工作是因为代码不在“块”代码中。在iOS 6或更新版本中,代码进入完成块,“addEventToCalendar”方法末尾的if-else条件首先执行。有什么办法吗?我需要报警标识符保存在数据库中

- (NSString *) addEventToCalendar
    {
        EKEventStore *eventStore = [[EKEventStore alloc] init];

        __block int calChk = 0;

        if ([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)])
        {
            // ios 6 or newer

            [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
             {
                 NSLog(@"granted claendar access");

                 dispatch_async(dispatch_get_main_queue(), ^{
                     if (error)
                     {
                         calChk = 0;
                     }

                     else if (!granted)
                     {   
                         calChk = 0;
                     }

                     else
                     {
                         calChk = 1;
                     }
                 });
             }];
        }

        else
        {
            calChk = 1;
        }

        if(calChk == 1)  // This gets executed before completion block completes and as   //a result the callChk value is 0    
        {
            return [self saveTaskToCalendar:eventStore];
        }

        else
        {
            return @"";
        }
    }


    - (NSString *) saveTaskToCalendar: (EKEventStore *) eventStore
    {
        EKEvent *event = [EKEvent eventWithEventStore:eventStore];

        event.title = @"EXEXEX";

        event.startDate = [standardFM dateFromString:dateString];

        NSDateComponents *dateComps = [[NSDateComponents alloc] init];

        dateComps.minute = 5;

        event.endDate = [[NSCalendar currentCalendar] dateByAddingComponents:dateComps toDate:event.startDate options:0];

        EKAlarm *eventAlarm = [EKAlarm alarmWithAbsoluteDate:event.startDate];

        event.alarms = [NSArray arrayWithObject:eventAlarm];

        event.notes = @"dwqdqd";

        [event setCalendar:[eventStore defaultCalendarForNewEvents]];

        NSError *err;

        [eventStore saveEvent:event span:EKSpanThisEvent error:&err];

        if(! [[err description] isEqualToString:@"(null)"])
        {
            NSLog(@"Event ID: %@", event.eventIdentifier);

            return event.eventIdentifier;
        }

        else
        {
            NSLog(@"Calendar error: %@", err);

            return @"";
        }
    }

如果使用信号量,则它会等待完成块完成:

- (NSString *) addEventToCalendar
{
    NSError *er;

    EKEventStore *eventStore = [[EKEventStore alloc] init];

    __block BOOL gotAccess = NO;

    if([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)])
    {
        dispatch_semaphore_t sema = dispatch_semaphore_create(0);

        [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {

            gotAccess = granted;

            dispatch_semaphore_signal(sema);
        }];

        dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
    }

    else
    {
        gotAccess = YES;
    }

    if (gotAccess)
    {

NSLog(@"got Access");
}

else
{
NSLog(@"No Access");
}

因为块是异步执行的。这就是为什么我要求解决。。。