【问题标题】:Implement updating location to server in the background mode- iOS在后台模式下实现向服务器更新位置 - iOS
【发布时间】:2015-06-10 06:14:55
【问题描述】:

我想做的是:

定期(每 10 分钟)将 iOS 设备的当前位置更新到我的服务器,甚至我的应用程序也处于后台模式。

我做了什么:

  1. 在plist中添加Required Background Modes;并询问 requestAlwaysAuthorization。

  2. 在设置中设置此应用的后台刷新。

  3. 设置一个 NSTimer 来定期获取位置(使用CLLocationManager)。

  4. 当locationManager: didUpdateLocations:delegate 调用时,更新位置使用HTTPGET 到服务器。 我的问题: App在前台运行正常,但是后台更新位置方法没有运行,服务器无法接收到我的位置数据。

谢谢。

【问题讨论】:

    标签: ios


    【解决方案1】:

    请检查您是否在项目设置 -> 目标 -> 功能中启用了后台模式中的位置。

    我建议您使用 startMonitoringVisits() 和 locationManager(_:didVisit:) 委托来获取用户位置,而不是每 10 分钟将位置发送到服务器。因为你的方法太耗电了。

    【讨论】:

    • 感谢 Stantu,我会尝试的,之前没有尝试过 startMonitoringVisits()。
    【解决方案2】:

    您必须使用以下代码注册您的后台任务,然后您的任务完成您必须结束后台任务

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

    }

    更多信息:apple document

    【讨论】:

    • 谢谢~我会试试的。
    猜你喜欢
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多