1

次のコードを使用して Web サービスに接続し、応答を取得しています。コードは問題なく動作します。接続デリゲート connectionDidFinishLoading を使用して結果を解析し、何らかのアクションを実行しています。

- (IBAction)signInTouchUpInside:(id)sender {

    if (r == NO) {

        if ([Utility internet]) {

            [self.connection cancel];

            self.json = [[NSMutableData alloc] init];

            NSString* urlString = [NSString stringWithFormat:@"http://www.domain.com/signin?email=%@&password=%@", self.email.text, self.password.text];
            NSURL* url = [NSURL URLWithString:urlString];
            NSMutableURLRequest* urlRequest = [NSMutableURLRequest requestWithURL:url];
            [urlRequest setHTTPMethod:@"GET"];
            [urlRequest addValue:@"iphonekey" forHTTPHeaderField:@"key"];

            self.connection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
            [self.connection start];

        } else {

        }

    }

}

- (void)connectionDidFinishLoading:(NSURLConnection*)connection {

    NSString* jsonString = [[NSString alloc] initWithData:self.json encoding:NSUTF8StringEncoding];

    SBJsonParser* parser = [[SBJsonParser alloc] init];
    NSDictionary* holder = [parser objectWithString:jsonString error:nil];

    if ([[holder valueForKey: @"result"] boolValue]) {
    } else {    
    }

}

- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response {
    [self.json setLength:0];
}

- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data {
    [self.json appendData:data];
}

- (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error {
    NSLog(@"error %@", error);
}

今、私はいくつかの問題に直面しており、何をすべきかわかりません。複数のView Controllerで同じ接続を行う必要があるため、毎回同じことを繰り返すのではなく、接続を管理するクラスを作成する方がよいと思います。また、connectionDidFinishLoading で実行するアクションは Web サービスごとに異なるため、呼び出しごとにコールバックを構成する必要があります。どうやってやるの?

ps: 同時呼び出しはありません。

4

1 に答える 1

1

はい、接続を管理するクラスを作成する必要があります。接続ごとに、そのインスタンスを作成します -

@property (nonatomic,retain)NSURLConnection* loginConnection;
@property (nonatomic,retain)NSURLConnection* logoutConnection;
@property (nonatomic,retain)NSURLConnection* registerConnection;

ビューコントローラーで-

-(IBAction)signInTouchUpInside:(id)sender{
    [[NSNotification defaultCenter]addObserver:self selector:@selector(signInResponse:) name:@"login" object:nil];

    //do something

}

-(void)signInResponse:(NSNotification*)notification{
    [[NSNotification defaultCenter]removeObserver:self name:@"login" object:nil];
    //do the callback function
}

新しいクラスで、接続を割り当てます -

    self.loginConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
    [self.connection start];

次に、接続でDidFinishLoading -

-(void)connectionDidFinishLoading:(NSURLConnection *)connection{

    if (connection == loginConnection){
        [[NSNotification defaultCenter] postNotificationName:@"login" object:nil userInfo:someInfo];
    }
    if (connection == logoutConnection){
        [[NSNotification defaultCenter] postNotificationName:@"logout" object:nil userInfo:someInfo];
    }
    if (connection == registerConnection){
        [[NSNotification defaultCenter] postNotificationName:@"register" object:nil userInfo:someInfo];
    }
}

このようにして、その接続を対象とした操作を実行できます。

于 2012-10-15T01:10:24.790 に答える