NSURLSessionDataTask得到响应后返回nil值

时间:2019-01-30 19:12:48

标签: objective-c asynchronous nsurlsessiondatatask jsonresponse no-response

我创建了这个api端点,该端点发送回有关某个人的数据,并且我正尝试从我的应用程序访问该端点。我能够连接到端点,但是在调用该方法后,它将返回nil。

我尝试使用block语句来收集该数据并返回该数据。我还创建了一个模型,当您在下面看到响应时,该模型将存储该数据,但是由于这是一个异步任务,因此看来我返回的数据仍然为零。我好像没有捕获任何数据。

- (NSString *) verifyCode:(NSString*) code{
    NSString* ID = self.potentialPerson[@"ID"];

    [self validateInput:code :ID];//nil upon return
    return self->inputModel.CodeResponse;//returns nil
}

- (void) validateInput:(NSString *)code :(NSString *) ID {
    NSString *URL = [[inputModel api] stringByAppendingFormat:@"action=validate&code=%@&inputID=%@",
                          code, ID];
    NSLog(@"%@", URL);

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:URL]];
    NSURLSession *sesh = [NSURLSession sharedSession];
    NSURLSessionDataTask *dataTask = [sesh dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if([data length] >0 && error == nil) {
            NSLog(@"DATA WAS RECEIVED");

            NSMutableDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

            self->inputModel.CodeResponse = [result objectForKey:@"messages"];
            //This outputs to console with the data I want
            NSLog(@"message: %@", self->inputModel.CodeResponse);
        }
        else {
            NSLog(@"Data Error = %@", error);
        }}];
    [dataTask resume];//When it gets here the data in CodeResponse is nil

    return;
}
自上述代码发布以来,我进行了

EDIT 更改。我遵循了罗布的建议,并尝试遵循此How to get NSDictionary from NSURLSessionDataTask

存储到控制台时,我的存储为空


- (NSString *) verifyCode:(NSString*) code{

    NSString* ID = self.potentialPerson[@"ID"];
    NSString *URL = [[inputModel api] stringByAppendingFormat:@"action=validate&code=%@&inputID=%@",
                          code, ID];

    NSURLSessionDataTask *storage = [self validateTypedCode:URL withDictionary: ^(NSDictionary *data) {
        self.CodeResponse = data;
        NSLog(@"message %@",data);
    }];
    NSLog(@"message %@",[self.CodeResponse objectForKey:@"message"]);
}

- (NSURLSessionDataTask *) validateInput:(NSString *)URL withDictionary: (void (^)(NSDictionary* data)) completionHandler {
    NSURLRequest *request = [NSURLRequest  requestWithURL:[NSURL URLWithString:URL]];
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request
                                            completionHandler:
                                  ^(NSData *data, NSURLResponse *response, NSError *error) {
                                      NSDictionary *result = [NSJSONSerialization JSONObjectWithData:data
                                                                                              options:0
                                                                                                error:NULL];
                                      if (completionHandler)
                                          dispatch_async(dispatch_get_main_queue(), ^{
                                              completionHandler(result);
                                          });
                                  }];
    [task resume];
    return task;
}


0 个答案:

没有答案