4

我已经尝试了一些在锁定屏幕时执行 NSURLConnection 的方法,但都不起作用。

我试过如下:

[self performSelectorInBackground:@selector(startConnection) withObject:nil];

我也试过:

dispatch_queue_t request_queue = dispatch_queue_create("com.app.download", NULL);
dispatch_async(request_queue, ^{
  [self startConnection];
});

在开始连接中:

- (void)startConnection{
  ... some URL processing

  responseData_ = [[NSMutableData alloc] init];
  connection_ =
  [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
}

NSURLConnection 委托方法不是通过这种方式调用的。使它工作的真正代码是什么?谢谢!

一个可能有帮助的小更新

它只调用这个委托方法:

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 

带有消息:

找不到具有指定主机名的服务器。

我很确定我的 wi-fi 已连接,但仍然不知道为什么叫它:(

4

1 回答 1

2

如果您锁定屏幕,您的应用程序将进入后台模式,而不是后台运行模式。 如果你想在用户锁定屏幕时下载,你应该检查这个方法 [UIApplication -beginBackgroundTaskWithExpirationHandler:]

于 2012-12-31T04:43:04.367 回答