【问题标题】:iOS: How to call webservice when app is terminated by the useriOS:当用户终止应用程序时如何调用网络服务
【发布时间】:2015-08-27 15:43:08
【问题描述】:

我正在尝试在应用未运行时将位置更新到服务器。当用户位置更改但未调用 Web 服务时,我正在获取位置更新...非常感谢任何帮助。

【问题讨论】:

  • 我认为您的术语不正确?终止意味着没有运行,因此如果应用程序没有运行,则完全不可能做任何事情。但是,由于您说:“我正在获取位置更新”,也许您实际上是指当应用程序处于后台时?后台的应用程序和终止的应用程序是两个完全不同的东西。如果应用程序在后台,则 Vanson 答案中的代码适用,但如果应用程序终止,显然不适用。

标签: ios iphone location


【解决方案1】:

你需要实现这样的后台任务

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    bgTask = [application beginBackgroundTaskWithName:@"MyTask" expirationHandler:^{
        // Clean up any unfinished task business by marking where you
        // stopped or ending the task outright.
        [application endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;
    }];

    // 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;
    });
}

【讨论】:

    猜你喜欢
    • 2019-09-08
    • 2019-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多