gpt4 book ai didi

ios - NSURLConnection 在它应该之前调用 connectionDidFinishLoading?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:22:32 25 4
gpt4 key购买 nike

我正在开发一个 iOS 应用程序,它会触发一些 NSURL 请求。我创建了一个实现 didReceiveData 的 WebService 类,如下所示:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// Append the new data to the instance variable you declared
[_responseData appendData:data];
}

其中 _responseData 是一个 NSMutableData* 属性。然后我创建了几个子类来创建它们的特定请求并实现 connectionDidFinishLoading。具体来说,我有一个像这样实现它的 RegisterService 类:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// The request is complete and data has been received
// You can parse the stuff in your instance variable now
NSError *error = nil;
_parsedData = [NSJSONSerialization JSONObjectWithData:_responseData options:kNilOptions error:&error];
NSLog(@"register data: %@",_parsedData);
registerAnswer serverAnswer = [[_parsedData objectForKey:@"success"] integerValue];

if (serverAnswer == REGISTER_SUCCESS) {
// Give data to delegate to handle
[self.delegate successRegister];
}
else { // serverAnswer == REGISTER_FAIL
NSLog(@"register message: %@",[[_parsedData objectForKey:@"message"] stringValue]);
[self.delegate failedRegister];
}
}

我有 2 个不同的 View Controller 使用此服务。其中一位这样调用它:

self.personRegistering.username = self.txtUsername.text;
self.personRegistering.password = self.txtPassword.text;
if (self.personRegistering.isCustomer) {
// register customer
DGRegisterService* myRegisterService = [[DGRegisterService alloc] initWithDelegate:self];
[myRegisterService registerPerson:self.personRegistering];
}
else {
// continue to get picture
[self performSegueWithIdentifier:@"RegisterPageThreeToFour" sender:self];
}

而且效果很好。另一个(第四页)让用户在注册前拍照。这是代码:

- (IBAction)btnTakePicture:(id)sender {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
// Camera is available
UIImagePickerController* myCamera = [[UIImagePickerController alloc] init];
myCamera.sourceType = UIImagePickerControllerSourceTypeCamera;
myCamera.delegate = self;
[self presentViewController:myCamera animated:YES completion:NULL];
}
}

#pragma mark - UIImagePicker Delegate

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissViewControllerAnimated:YES completion:NULL];
// Register person first
DGRegisterService* myRegisterService = [[DGRegisterService alloc] initWithDelegate:self];
[myRegisterService registerPerson:self.personRegistering];

// // Now send pic
// UIImage* picture = [info objectForKey:UIImagePickerControllerOriginalImage];
// DGUploadService* myUploadService = [[DGUploadService alloc] initWithDelegate:self];
// [myUploadService uploadImage:picture forUsername:self.personRegistering.username];
}

现在,第二个发送请求,服务器正确处理它,发回这个 JSON 对象:

{"success":1,"message":"Account successfully created."}

这是我所期望的。但是,两个 NSLog 语句都显示“(null)”。有时,应用程序崩溃是因为 objectForKey 被发送到一个不响应它的实例。有时,在我点击 XCode 上的“停止”后,控制台将显示 JSON 对象。其余时间它不会崩溃或显示对象。

由于此页面使用相机,我必须只在 iPhone 上进行测试,而不是在模拟器上进行测试。另一个页面在模拟器和 iPhone 中每次都正确显示 JSON 对象。我用的 iPhone 是 iOS7.1.1 的 4S。

最佳答案

如果你的意思是下面几行中的NSLog:

NSError *error = nil;
_parsedData = [NSJSONSerialization JSONObjectWithData:_responseData options:kNilOptions error:&error];
NSLog(@"register data: %@",_parsedData);

有时会打印“(null)”,这可能是 JSONObjectWithData 调用错误造成的。

我建议添加

NSLog(@"received data: %@", _responseData);

检查数据是否一切正常,以及

NSLog(@"register data: %@ -- (error: %@)",_parsedData, [error localizedDescription]);

查看是否返回错误。

关于ios - NSURLConnection 在它应该之前调用 connectionDidFinishLoading?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24460482/

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