我正在开发一个触发少量NSURL请求的iOS应用程序。我创建了一个实现didReceiveData这样一个WebService类:NSURLConnection在它应该调用connectionDidFinishLoading之前?
- (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个不同视图控制器。其中之一就像这样:
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。
谢谢你的输入Sergio。我添加了你建议的调试行,并发现数据实际上存在,至少在这种情况下(我不知道它是否正确,因为它是十六进制的,但是有些东西)。错误是_(可可错误3840。)_。你知道为什么JSON对象可能会被损坏吗?服务器如何能够有时候创建正确的答案?再次感谢您的帮助。 – gamda