我想在applicationDidEnterBackground
被调用时取消 url 连接。但我不知道如何在整个应用范围内保存连接。我在其他视图控制器中创建了一些 url 连接,但我想在AppDelegate
. 我怎样才能做到这一点?
问问题
676 次
3 回答
3
您可以UIApplicationDidEnterBackgroundNotification
在创建连接的类中添加观察者。尝试这个:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(enterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];
记得叫这个
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];
在你的课上dealloc
于 2012-11-21T16:49:06.477 回答
0
我相信当您的应用程序进入后台时,您的 NSURLConnections 将被杀死。
如果您想跟踪所有 NSURLConnections,您需要自己跟踪它们,方法是将它们添加到 NSSet、NSArray 或其他一些数据结构中,然后循环关闭它们。您还可以有一个继承或组成 NSURLConnection 的类。此数据结构将处理将自身添加到队列中,并且您可以在应用程序退出时进行清理。
于 2012-11-21T14:59:21.643 回答
0
最好在 app 变为非活动状态而不是applicationDidEnterBackground之前停止连接。
答案是使用通知:
-(void)init
{
//
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(closeConnection) name:UIApplicationWillResignActiveNotification object:nil];
//
}
-(void)closeConnections
{
// [urlConnection cancel];
}
于 2012-11-21T16:47:29.760 回答