我想在用户触摸主页按钮时保存服务器中app中使用的所有数据(使用API调用)。我将观察者设置为
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveData) name:UIApplicationDidEnterBackgroundNotification object:nil];
saveData
方法用于将数据发送到服务器。 api调用不起作用。用户关闭应用时以任何方式调用API。
答案 0 :(得分:0)
我已经开始了后台任务
- (void)applicationDidEnterBackground:(UIApplication *)application
{
bgTask = [application beginBackgroundTaskWithName:@"MyTask" expirationHandler:^{
[self saveData];
// Clean up any unfinished task business by marking where you
// stopped or ending the task outright.
}];
// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Do the work associated with the task, preferably in chunks.
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
});
}
然后在saveData
方法
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;