用户按主页按钮时,在服务器中保存日期

时间:2016-04-27 05:26:53

标签: ios background-process

我想在用户触摸主页按钮时保存服务器中app中使用的所有数据(使用API​​调用)。我将观察者设置为

[[NSNotificationCenter defaultCenter]  addObserver:self selector:@selector(saveData) name:UIApplicationDidEnterBackgroundNotification object:nil];

saveData方法用于将数据发送到服务器。 api调用不起作用。用户关闭应用时以任何方式调用API。

1 个答案:

答案 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;