经过几个小时的阅读和重新编码,我仍然找不到解决方案。
我想建立一个简单的应用程序,跟踪我的位置在后台。在第一次加载应用程序时,我在当前位置周围设置了一个区域,开始监控这个区域和stopUpdatingLocations。一旦应用程序获得didExitRegion事件,我再次获得当前位置,设置另一个区域(在清理所有其他受监控区域之后),然后所有操作再次启动。
这个应用有定位的alwaysAuthorization,设置了位置更新的后台模式,PList包含LocationAlwaysAndWhenInUseUsageDescription和LocationWhenInUseUsageDescription。
有一段时间,它工作得很好。但在长时间不移动之后,应用程序被挂起,不再接收任何位置更新,甚至不会收到监视器事件,这应该会在我正确理解this ()时重新启动应用程序。
有谁能给我点提示吗?
谢谢。
import UIKit
import CoreLocation
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
let locationManager = CLLocationManager()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let currentNotifications = UNUserNotificationCenter.current()
currentNotifications.getNotificationSettings { (settings) in
if settings.authorizationStatus == .notDetermined {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { (granted, error) in }
}
}
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
locationManager.allowsBackgroundLocationUpdates = true
locationManager.startUpdatingLocation()
return true
}
}
extension AppDelegate: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations[0]
let now = Date()
let formatter = DateFormatter()
formatter.timeZone = TimeZone.current
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
for region in locationManager.monitoredRegions {
locationManager.stopMonitoring(for: region)
}
var regionIdentifier = String(format: "%f", location.coordinate.latitude) + ", " + String(format: "%f", location.coordinate.latitude) + "( set on " + formatter.string(from: now) + ")"
var geofenceRegion = CLCircularRegion(center: CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude), radius: 250, identifier: regionIdentifier)
geofenceRegion.notifyOnExit = true
locationManager.startMonitoring(for: geofenceRegion)
locationManager.stopUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
locationManager.stopMonitoring(for: region)
sendNotification(title: "Event triggered", subtitle: "Region exit", body: region.identifier)
locationManager.startUpdatingLocation()
}
}
public func sendNotification(title: String, subtitle: String, body: String) {
let content = UNMutableNotificationContent()
content.title = title
content.subtitle = subtitle
content.body = body
content.sound = .default
let identifier = "geoStalker4_notification_" + String(format: "%f", NSDate().timeIntervalSince1970)
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 3, repeats: false)
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: nil)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}发布于 2020-11-23 21:10:56
尝试将此内容添加到您的locationManager设置:
locationManager.pausesLocationUpdatesAutomatically = falsehttps://stackoverflow.com/questions/62423356
复制相似问题