ios - NSURLConnection 之前应该调用 connectionDidFinishLoading 吗?
<p><p>我正在开发一个触发几个 NSURL 请求的 iOS 应用程序。我创建了一个实现 didReceiveData 的 WebService 类,如下所示:</p>
<pre><code>- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// Append the new data to the instance variable you declared
;
}
</code></pre>
<p>其中 _responseData 是 NSMutableData* 属性。然后我创建了几个子类来创建它们的特定请求并实现 connectionDidFinishLoading。具体来说,我有一个 RegisterService 类,它像这样实现它:</p>
<pre><code>- (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 = ;
NSLog(@"register data: %@",_parsedData);
registerAnswer serverAnswer = [ integerValue];
if (serverAnswer == REGISTER_SUCCESS) {
// Give data to delegate to handle
;
}
else { // serverAnswer == REGISTER_FAIL
NSLog(@"register message: %@",[ stringValue]);
;
}
}
</code></pre>
<p>我有 2 个使用此服务的不同 ViewController 。其中一个是这样调用它的:</p>
<pre><code>self.personRegistering.username = self.txtUsername.text;
self.personRegistering.password = self.txtPassword.text;
if (self.personRegistering.isCustomer) {
// register customer
DGRegisterService* myRegisterService = [ initWithDelegate:self];
;
}
else {
// continue to get picture
;
}
</code></pre>
<p>而且效果很好。另一个(第四页)让用户在注册之前拍照。代码如下:</p>
<pre><code>- (IBAction)btnTakePicture:(id)sender {
if () {
// Camera is available
UIImagePickerController* myCamera = [ init];
myCamera.sourceType = UIImagePickerControllerSourceTypeCamera;
myCamera.delegate = self;
;
}
}
#pragma mark - UIImagePicker Delegate
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
;
// Register person first
DGRegisterService* myRegisterService = [ initWithDelegate:self];
;
// // Now send pic
// UIImage* picture = ;
// DGUploadService* myUploadService = [ initWithDelegate:self];
// ;
}
</code></pre>
<p>现在,第二个发送请求并且服务器正确处理它,发回这个 JSON 对象:</p>
<pre><code>{"success":1,"message":"Account successfully created."}
</code></pre>
<p>这是我所期望的。但是,两个 NSLog 语句都显示“(null)”。有时,应用程序崩溃是因为 objectForKey 被发送到不响应它的实例。有时,在我在 XCode 上点击“停止”后,控制台会显示 JSON 对象。其余时间它不会崩溃或显示对象。</p>
<p>由于此页面使用相机,我只能在 iPhone 上进行测试,而不是模拟器。另一个页面在模拟器和 iPhone 中每次都正确显示 JSON 对象。我使用的 iPhone 是 iOS7.1.1 的 4S。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>如果你的意思是<code>NSLog</code>在下面几行:</p>
<pre><code>NSError *error = nil;
_parsedData = ;
NSLog(@"register data: %@",_parsedData);
</code></pre>
<p>有时会打印“(null)”,这可能是由 <code>JSONObjectWithData</code> 调用中的错误引起的。</p>
<p>我建议添加</p>
<pre><code>NSLog(@"received data: %@", _responseData);
</code></pre>
<p>检查数据是否一切正常,并且</p>
<pre><code>NSLog(@"register data: %@ -- (error: %@)",_parsedData, );
</code></pre>
<p>查看是否返回错误。</p></p>
<p style="font-size: 20px;">关于ios - NSURLConnection 之前应该调用 connectionDidFinishLoading 吗?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/24460482/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/24460482/
</a>
</p>
页:
[1]