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)
}
}
}
如何更新位置,即使應用程序被終止?在我上面的代碼中有什麼不對?
我需要的位置更新,而應用程序被終止。如果您可以在這裏提供一些有關代碼和說明的幫助,那將非常有幫助。 –