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] リターン前の AllowCalendar = (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

このようにブロックを使用することはできません。これを実装する方法はたくさんあります。1 つの方法は、呼び出し元のメソッドを 2 つの別々のメソッドに分割して、次のように機能を実行することです。

-(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);
}

ちなみに、Apple のコーディング規則に従って、変数名とメソッド名は小文字で始める必要があります。allowCalendarcheckCalendarAllowedを名前として使用してください。

于 2012-12-17T01:21:02.467 に答える