1

我的应用程序的一部分涉及根据提供给用户的唯一代码为用户创建登录检查。为了使我的应用程序结构合理,我创建了一个网络助手类来处理所有网络操作。这是我如何从控制器类 (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和与我的登录视图相关联的控制器类之间进行通信。我需要根据从服务器返回的数据更改主视图上的某些内容,这些数据只能从异步运行的完成处理程序中访问。有没有更好的方法来管理所有网络任务,同时仍然能够引用调用它们的主控制器的对象。提前致谢

4

1 回答 1

3

您可以通过在 LoginNetworkHelper.h 类中添加协议来实现通信。

@protocol LoginNetworkHelperResponseDelegate <NSObject>
@required
-(void)didFininshRequestWithJson:(NSDictionary *)responseJson;
@end

将 Delegate 变量添加到 LoginNetworkHelper.h 类

@property (nonatomic,strong) NSObject < LoginNetworkHelperResponseDelegate > *delegate;

现在将以下代码添加到您的完成处理程序中。

NSURLSessionDataTask *dataTask = [self.session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
       NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
       //Code Added Start
       [self.delegate didFininshRequestWithJson:json];
       //Code Added Ends
        NSLog(@"%@",json);
    }];

现在在 ViewController.h 中只要符合协议

@inteface ViewController:UIViewController<LoginNetworkHelperResponseDelegate>
…
@end

并在 ViewController.m 中实现协议方法 didFininshRequestWithJson:responseJson

-(void)didFininshRequestWithJson:(NSDictionary *)responseJson
{
  //Process the responseJson;
}

现在只需执行以下代码来启动 LoginRequest。

LoginNetworkHelper *loginNetHelper = [[LoginNetworkHelper alloc]init];
loginNetHelper.delegate = self;
[loginNetHelper loginBasedOnPatientId:@"abc"];

一旦调用了 Completion Handler,它将调用 Delegate 方法并接收到 json 响应。这也可以使用通知来实现。

如果您遇到任何问题,请告诉我。

问候,

阿米特

于 2014-08-25T12:45:27.963 回答