私のアプリの一部は、ユーザーに提供された一意のコードに基づいて、ユーザーのログイン チェックを作成します。アプリケーションを適切に構造化するために、すべてのネットワーク操作を処理するネットワーク ヘルパー クラスを作成しました。コントローラー クラス (ViewController.m) からヘルパー クラスを呼び出す方法を次に示します。
[[LoginNetworkHelper alloc] loginBasedOnPatientId:@"abc"];
私の LoginNetworkHelper クラスは次のタスクを実行します (LoginNetworkhelper.m)
- (BOOL)loginBasedOnPatientId:(NSString *)patientId
{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://test.php"]];
[request setHTTPMethod:@"POST"];
//creating json string to send to server
NSDictionary *patientData = [NSDictionary dictionaryWithObjectsAndKeys:@"002688727",@"value",@"prem_key_id",@"name", nil];
NSMutableArray *dataArr = [[NSMutableArray alloc] init];
[dataArr addObject:patientData];
NSError *error;
NSData *jsonData2 = [NSJSONSerialization dataWithJSONObject:dataArr options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData2 encoding:NSUTF8StringEncoding];
NSString *postData = [NSString stringWithFormat:@"message=%@",jsonString];
[request setHTTPBody:[postData dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]];
NSURLSessionDataTask *dataTask = [self.session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
NSLog(@"%@",json);
}];
[dataTask resume];
return TRUE;
}
したがって、私の基本的な質問は、完了ハンドラーNSURLSession
と、ログインのビューにリンクされているコントローラー クラスとの間で通信する方法です。非同期で実行される完了ハンドラーからのみアクセスできるサーバーから返されたデータに基づいて、メイン ビューで何かを変更する必要があります。呼び出されたメインコントローラーのオブジェクトを参照しながら、すべてのネットワークタスクを管理するより良い方法はありますか? 前もって感謝します