gpt4 book ai didi

ios - 如何获取数据从 NSURLSessionDataTask 返回

转载 作者:可可西里 更新时间:2023-11-01 03:27:16 31 4
gpt4 key购买 nike

我有一个注册 View Controller ,它处理所有用户输入并调用传递给它的另一个类 (wsClass) 中的函数,该数据作为 NSDictionary。

调用 wsClass 中的函数并建立连接并从服务器返回数据并在 session 中可用。

我的问题是如何将该数据返回到最初调用该函数的注册 View Controller ,它总是空的。

这是registrationViewController中的调用:

        wsClass *ws = [[wsClass alloc] init];
NSDictionary *testDict = [[NSDictionary alloc] initWithObjectsAndKeys:username.text,@"username",email.text,@"email",password.text,@"password", nil];
NSDictionary *respDict = [ws sendData:testDict];

这是在 wsClass.m 中调用的函数:

- (NSDictionary *)sendData:(NSDictionary *)sendDict {
NSMutableString *sendStr = [[NSMutableString alloc] init];

[sendDict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
[sendStr appendFormat:@"&%@=%@", key, obj];
}];

NSLog(@"sendStr is: %@",sendStr);
NSString *noteDataString = [NSString stringWithFormat:@"%@%@",REQUIRED,sendStr];

NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlTest]];
request.HTTPBody = [noteDataString dataUsingEncoding:NSUTF8StringEncoding];
request.HTTPMethod = @"POST";

NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
// The server answers with an error because it doesn't receive the params
// handle response
if(error == nil)
{
[getReqAlert dismissWithClickedButtonIndex:0 animated:YES];

NSError *e = nil;
jsonArray = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableLeaves error: &e];

if (!jsonArray) {
NSLog(@"Error parsing JSON: %@", e);
} else {

NSLog(@"resp: %@ = %@",[jsonArray objectForKey:@"status"],[jsonArray objectForKey:@"msg"]);
NSLog(@"Dictionary count: %lu", jsonArray.count);
}
}
}];

[postDataTask resume];

return jsonArray;

最佳答案

帖子异步完成,因此您的 wsClass 需要在帖子完成后告知调用者有关完成的信息。一个很好的方法是用调用者提供的应该在完成时运行的代码块来扩充 sendData 方法:

sendData: 更改为如下所示:

// it doesn't return anything, because all it does is launch the post
// but when the post is done, it invokes completion

- (void)sendData:(NSDictionary *)sendDict completion:(void (^)(NSDictionary *))completion {

// the stuff you're already doing

NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
// the stuff you're already doing
// now tell the caller that you're done
completion(jsonArray);
}];
}

调用者现在看起来像这样:

// change the UI to say "I'm busy doing the post"
[ws sendData:testDict completion:^(NSDictionary *responseDict) {
NSLog(@"this runs later, after the post completes %@", responseDict);
// change the UI to say "The post is done"
}];

关于此的几点注意事项:(1) 我没有向 block 中添加错误参数,您可能应该这样做。检查并使用 nil 和错误调用该 block ,或者使用 json 输出和 error=nil。 (2) 您的代码假定 json 结果解析为字典。在代码中假定它之前,请确保它始终为真。 (3) 类名通常以大写开头。

关于ios - 如何获取数据从 NSURLSessionDataTask 返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20871506/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com