0
我正在开发一个应用程序,我需要将设备的位置更新到服务器大致准确,我可以承受500的差异|数据中有700米。区域监控iOS应用程序的限制
我成功地在背景和前景模式下实现它。 我的主要担心是应用程序终止时的位置更新,因为我在应用程序终止时实施了重大位置更改,但它以随机方式通知设备位置发生更改,并且非常不准确,或者位置更新中存在巨大延迟以2公里的距离接收位置更新,有时甚至不在7公里的距离。
所以我的一个团队成员建议我在app终止时实现区域监视,现在我已经实现了区域监视,但它也不能正常工作,并且我已经阅读了某处最多只能有20个可以监视的区域从一个应用程序。
这里是下面的示例代码,我想:
import UIKit
import CoreLocation
import XCGLogger
//import SwiftLocation
// MARK: - AppDelegate
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate
{
var window: UIWindow?
var locationManager: CLLocationManager!
var currentLocation: CLLocationCoordinate2D?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
{
if launchOptions != nil
{
if (launchOptions![UIApplicationLaunchOptionsLocationKey] != nil)
{
if let location = locationManager.location?.coordinate {
startRegionMonitoring(location)
}
}
}
setupLocalNotifications()
setupLocationManager()
return true
}
func setupLocationManager()
{
if (locationManager == nil)
{
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.pausesLocationUpdatesAutomatically = false
locationManager.distanceFilter = CLLocationDistance(100.0)
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
if #available(iOS 9.0, *) {
locationManager.allowsBackgroundLocationUpdates = true
}
}
}
func applicationWillTerminate(application: UIApplication)
{
if currentLocation == nil
{
return
}
// create a region around current location and monitor enter/exit
startRegionMonitoring(currentLocation!)
}
func startRegionMonitoring(location: CLLocationCoordinate2D)
{
let region = CLCircularRegion(center: location, radius: 100.0, identifier: "manish")
region.notifyOnExit = true
region.notifyOnEntry = true
locationManager.startMonitoringForRegion(region)
}
// MARK: - Local notifications
func setupLocalNotifications()
{
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
}
func showLocalNotification(message: String)
{
let localNotification = UILocalNotification()
localNotification.alertBody = message;
localNotification.fireDate = NSDate(timeIntervalSinceNow: 1.0)
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
}
}
extension AppDelegate
{
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus)
{
if status == .AuthorizedAlways {
locationManager.startUpdatingLocation()
}
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
currentLocation = locations.first?.coordinate
showLocalNotification("didUpdateLocations: \(currentLocation)")
}
func locationManager(manager: CLLocationManager, didEnterRegion region: CLRegion)
{
showLocalNotification("Entered region: \(region.identifier)")
}
func locationManager(manager: CLLocationManager, didExitRegion region: CLRegion)
{
showLocalNotification("Exited region: \(region.identifier)")
if let location = manager.location?.coordinate {
startRegionMonitoring(location)
}
}
}
如何更新位置,即使应用程序被终止?在我上面的代码中有什么不对?
我需要的位置更新,而应用程序被终止。如果您可以在这里提供一些有关代码和说明的帮助,那将非常有帮助。 –