gpt4 book ai didi

ios - 在 Swift 中每 30 分钟发送一次位置更新

转载 作者:IT王子 更新时间:2023-10-29 05:19:08 25 4
gpt4 key购买 nike

我为位置服务打开了后台模式,并打算每 30 分钟向服务器发送一次位置(纬度和经度)。现在我正在控制台中打印相同的内容。它似乎工作了一段时间,但我想知道在这种情况下如何使用 NSTimer。我应该从哪里调用它?

import UIKit
import CoreLocation

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate {

var window: UIWindow?
var locationManager = CLLocationManager()

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
return true
}

func applicationWillResignActive(application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

func applicationDidEnterBackground(application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

self.locationManager.delegate = self
self.locationManager.startUpdatingLocation() // I know i should be using signification location option here. this is just for testing now.
}

func locationManager(manager: CLLocationManager!, didUpdateToLocation newLocation: CLLocation!, fromLocation oldLocation: CLLocation!) {
self.sendBackgroundLocationToServer(newLocation);
}

func sendBackgroundLocationToServer(location: CLLocation) {
var bgTask = UIBackgroundTaskIdentifier()
bgTask = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler { () -> Void in
UIApplication.sharedApplication().endBackgroundTask(bgTask)
}

println(location.coordinate.latitude)

if (bgTask != UIBackgroundTaskInvalid)
{
UIApplication.sharedApplication().endBackgroundTask(bgTask);
bgTask = UIBackgroundTaskInvalid;
}
}

func applicationWillEnterForeground(application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
application.beginBackgroundTaskWithExpirationHandler{}
}

func applicationDidBecomeActive(application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
application.beginBackgroundTaskWithExpirationHandler{}
}

func applicationWillTerminate(application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
application.beginBackgroundTaskWithExpirationHandler{}
}


}

也许调用 application.beginBackgroundTaskWithExpirationHandler{} 是个坏主意?我在这里有哪些选择?

最佳答案

beginBackgroundTask... 的想法是启动一个有限长度的任务,这样如果用户离开应用程序,它会在后台任务中持续运行一段时间( 3 分钟,我相信)。在时间用完之前,您必须调用 endBackgroundTask 否则应用程序将被立即终止。

因此,遗憾的是,后台任务机制并不真正适合您想要的意图。但是,有一小部分特殊的后台模式专为在一小部分功能(VOIP、音频等)之外的持续后台操作而设计。有关详细信息,请参阅 App Programming Guide for iOS: Background Execution实现长时间运行的任务部分.

现在,其中一种后台模式用于“定位”服务。因此,如果这是您应用程序的核心功能,对于正常运行至关重要,那么您可以注册 location 后台模式,您的应用程序将继续在后台运行。从那里,您可以监视位置更新,如果经过了足够长的时间,则触发一些过程。但是,如果此后台定位模式不是您应用程序的基本功能,Apple 可能会拒绝您的应用程序请求其不需要的后台模式。


顺便说一下,您应该知道启动标准位置服务可能会耗尽设备电池。您可以考虑使用电池高效 "significant change" location service .这还有一个优点,即每次用户移动一定距离(例如以公里为单位;我相信它是通过移动到不同的手机信号塔触发的)时自动唤醒您的应用程序。

关于ios - 在 Swift 中每 30 分钟发送一次位置更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26269387/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com