我正在开发一个触发几个 NSURL 请求的 iOS 应用程序。我创建了一个实现 didReceiveData 的 WebService 类,如下所示:
- (void)connectionNSURLConnection *)connection didReceiveDataNSData *)data {
// Append the new data to the instance variable you declared
[_responseData appendData:data];
}
其中 _responseData 是 NSMutableData* 属性。然后我创建了几个子类来创建它们的特定请求并实现 connectionDidFinishLoading。具体来说,我有一个 RegisterService 类,它像这样实现它:
- (void)connectionDidFinishLoadingNSURLConnection *)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)btnTakePictureid)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) imagePickerControllerUIImagePickerController *)picker didFinishPickingMediaWithInfoNSDictionary *)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。
Best Answer-推荐答案 strong>
如果你的意思是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/
|