0

无论用户是否允许访问日历,我都有返回 iOS6 的方法。

-(NSString *)CheckCalendarAllowed{

  __block NSString *AllowCalendar;

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

[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
    if(granted){
        NSLog(@"Event store granted");
        AllowCalendar = @"1";
        
        NSLog(@"AllowCalendar in block = %@",AllowCalendar);
        
    }else{
        NSLog(@"Event store not granted");
        AllowCalendar = @"0";
        
        NSLog(@"AllowCalendar in block = %@",AllowCalendar);
    }
}];


NSLog(@"AllowCalendar before return = %@",AllowCalendar);
return AllowCalendar;
}

在控制台中我得到了这个。

2012-12-16 20:48:18.418 22052012_xxxx[4346:907] 返回前允许日历 = (null)

2012-12-16 20:48:18.460 22052012_xxxx[4346:110b] 授予事件存储

2012-12-16 20:48:18.462 22052012_xxxx[4346:110b] 块中的 AllowCalendar = 1

完成所有 requestAccessToEntityType 块后如何调用 Return 参数?

4

1 回答 1

0

您不能以这种方式使用块。有很多方法可以实现这一点。一种方法是尝试将调用者方法拆分为两个单独的方法,并执行以下功能,

-(void)CheckCalendarAllowed{

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

  [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
    if(granted){
        NSLog(@"Event store granted");
        AllowCalendar = @"1";

        NSLog(@"AllowCalendar in block = %@",AllowCalendar);
        [self returnAllowCalendarValue:AllowCalendar];
    }else{
        NSLog(@"Event store not granted");
        AllowCalendar = @"0";

        NSLog(@"AllowCalendar in block = %@",AllowCalendar);
        [self returnAllowCalendarValue:AllowCalendar];
    }
  }];
}

然后调用该方法,

[self CheckCalendarAllowed];

并在方法中收到上述值后实施这些功能,

-(void)returnAllowCalendarValue:(NSString *)allowCalendarString {
  //implement the rest of the operations here
  NSLog(@"AllowCalendar = %@", allowCalendarString);
}

附带说明一下,根据苹果编码约定,变量名和方法名应以小写开头。请使用allowCalendarcheckCalendarAllowed作为名称。

于 2012-12-17T01:21:02.467 回答