ios 网络怎么把nsurlsession请求到的数据拿出来用
发布网友
发布时间:2023-08-13 22:13
我来回答
共1个回答
热心网友
时间:2023-08-24 03:03
这样以异步方式,完成开机自检你 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) 我不将错误参数添加到块,你大概应该。检查,并调用与任一零块和错误,或与 json 输出和错误 = 零。(2) 您的代码假定的 json 结果分析作为一本字典。请确保之前您假定它在代码中,总是为 true。(3) 类名称通常以大写字母开头。