0

奇妙なタイトルですが、私の問題を説明する最も簡単な方法は次のとおりです。Parse サービスには、作成済みのサインアップ コントローラーがあり、新しいユーザーがどんなサービスにもサインアップできるようになっています。その実装を編集する方法はありません。デリゲート メソッドでのみイベントを処理できます。したがって、これを「サインアップ」ボタンの IBAction に配置することはできません。私がやりたいことは、ユーザーが「サインアップ」に触れたときに、API を呼び出して何かが存在するかどうかを確認する必要があることです。それが既に存在する場合は、ユーザーに署名させないでください。上。ボタンが押されたときに検証を処理するためのデリゲート メソッドを次に示します。

// Sent to the delegate to determine whether the sign up request should be submitted to the server.
- (BOOL)signUpViewController:(PFSignUpViewController *)signUpController shouldBeginSignUp:(NSDictionary *)info {

ここに私が入れようとしているものがあります:

[self processJSONDataWithURLString:[NSString stringWithFormat:@"https://www.someapi.com/api/profile.json?username=%@",username] andBlock:^(NSData *jsonData) {

    NSDictionary *attributes = [jsonData objectFromJSONData];

    // Check to see if username has data key, if so, that means it already exists
    if ([attributes objectForKey:@"Data"]) {
        return NO; // Do not continue, username already exists
    // I've also tried:
        dispatch_sync(dispatch_get_main_queue(), ^{ return NO; } );
    }
else
        return YES; //Continue with sign up
        dispatch_sync(dispatch_get_main_queue(), ^{ return YES; } );
}];

ただし、何かを返そうとするとエラーが発生します。単純に YES を返すと、「^(NSData *jsonData)」に黄色の下線が引かれ、「互換性のないブロック ポインター タイプが BOOL (^)NSData *_strongを void(^)NSData * _strong 型のパラメーターに送信しています」というメッセージが表示されます。 .

基本的に、このメソッドでAPI呼び出しを行って何かをチェックし、結果に応じてYESまたはNOを返す方法はありますか?

ありがとう!

4

2 に答える 2

2
于 2013-04-22T18:07:37.473 に答える
1

これを試して:

[self processJSONDataWithURLString:[NSString stringWithFormat:@"https://www.someapi.com/api/profile.json?username=%@",username] andBlock:^(NSData *jsonData) {

    NSDictionary *attributes = [jsonData objectFromJSONData];
    BOOL status=YES; 
    // Check to see if username has data key, if so, that means it already exists
    if ([attributes objectForKey:@"Data"]) {
        status=NO; // Do not continue, username already exists

    [self performSelectorOnMainThread:@selector(callDelegate:) withObject:[NSNumber numberWithBool:status] waitUntilDone:YES];
}];

-(void)callDelegate:(NSNumber*) status
 {
   BOOL returnStatus = [status boolValue];

   //now retutn returnStatus to your delegate.
 }

しかし、これは正しい方法ではありません。非同期通信をサポートするために、作成したロジックを変更する必要があります。あなたが自分のやり方でそれをしたい場合にのみ、あなたは私のものを考えることができます.

于 2013-04-22T18:23:29.963 に答える