gpt4 book ai didi

swift - iOS 应用程序区域监控的限制

转载 作者:行者123 更新时间:2023-11-30 13:04:18 26 4
gpt4 key购买 nike

我正在开发一个应用程序,我需要将设备位置更新到服务器,大致准确,我可以承受 500 的差异 |数据中有700米。

我成功地在后台和前台模式下实现了它。我主要关心的是应用程序终止时的位置更新,因为我确实在应用程序终止时实现了重大位置更改,但它以随机方式通知设备位置更改,并且非常不准确,或者有时我收到的位置更新存在巨大的延迟位置更新的间隔为 2-3 公里,有时甚至不到 7 公里。

因此,我的一位团队成员建议我在应用程序终止时实现区域监控,现在我已经实现了区域监控,但它也无法正常工作,并且我在某处读到最多只能监控 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)
}
}

}

即使应用程序终止,我如何更新位置?我上面的代码有什么问题吗?

最佳答案

启用后台刷新。您可以不断更新应用内的位置。

http://solutionowl.com/background-app-refresh-explained-in-laymans-terms/

关于swift - iOS 应用程序区域监控的限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39567249/

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