gpt4 book ai didi

ios - 如何在应用程序未运行(终止/终止)时保持 Core Location 和 Core Bluetooth 运行?

转载 作者:行者123 更新时间:2023-11-28 21:06:56 26 4
gpt4 key购买 nike

如果我终止应用程序,我在尝试保持我的功能运行时卡住了。

是否可以在应用程序未运行时保持核心位置(地理围栏/地理定位)和核心蓝牙运行?如果可能如何解决我的问题?我已经检查了背景模式,并实现了核心定位方法。这是我的代码:

    class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate {

var viewController = ViewController()
var window: UIWindow?

var locationManager = CLLocationManager()

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

locationManager = CLLocationManager()
locationManager.requestAlwaysAuthorization()

locationManager.delegate = self
locationManager.distanceFilter = kCLDistanceFilterNone
locationManager.desiredAccuracy = kCLLocationAccuracyBest

locationManager.pausesLocationUpdatesAutomatically = false

if #available(iOS 9.0, *) {
locationManager.allowsBackgroundLocationUpdates = true
}

beaconRegion.notifyEntryStateOnDisplay = true
beaconRegion.notifyOnEntry = true
beaconRegion.notifyOnExit = true

locationManager.startMonitoring(for: beaconRegion)
locationManager.stopRangingBeacons(in: beaconRegion)
locationManager.startRangingBeacons(in: beaconRegion)
locationManager.startUpdatingLocation()

return true
}

func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
if (region.identifier == beaconRegionIdentifier) {
manager.requestState(for: region)
goBackground()
}
}

func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
print("you exited region")
}

func locationManager(_ manager: CLLocationManager, didDetermineState state: CLRegionState, for region: CLRegion) {
if (region.identifier == beaconRegionIdentifier) {
manager.stopUpdatingLocation()

switch state {
case .inside:
manager.startRangingBeacons(in: region as! CLBeaconRegion)
manager.startUpdatingLocation()
case .outside:
let delay = DispatchTime.now() + .seconds(3)
DispatchQueue.main.asyncAfter(deadline: delay) {
manager.requestState(for: region)
}
case .unknown:
let delay = DispatchTime.now() + .seconds(3)
DispatchQueue.main.asyncAfter(deadline: delay) {
manager.requestState(for: region)
}
}
}
}

func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) {

let state = UIApplication.shared.applicationState
switch(state){
case.active,
.inactive,
.background:

let mainView = UserDefaults.standard.bool(forKey: "toMainView")

var isWakeUpRunning = UserDefaults.standard.bool(forKey: "isWakeUpRunning")

if isWakeUpRunning {
if mainView {
for aBeacon in beacons{
if aBeacon.rssi > WAKE_UP_THRESHOLD && aBeacon.rssi != 0{
UserDefaults.standard.set(false, forKey: "isWakeUpRunning")
self.viewController.startFunction()
manager.stopRangingBeacons(in: region)
}else if aBeacon.rssi != 0 && aBeacon.rssi < WAKE_UP_THRESHOLD {
manager.stopRangingBeacons(in: region)
manager.requestState(for: region)
}
}
}
}else{
manager.stopRangingBeacons(in: region)
manager.requestState(for: region)
}
}
}

func goBackground() {
let app = UIApplication.shared
var bgTask: UIBackgroundTaskIdentifier = 0
bgTask = app.beginBackgroundTask(expirationHandler: {
NSLog("Expired: %lu", bgTask)
app.endBackgroundTask(bgTask)
})
NSLog("Background: %lu", bgTask)
}

}

从我的代码来看,它可以在前台和后台运行。但是当我从任务切换器向上滑动应用程序时,它无法工作。而且我也无法从 xcode 中看到日志。有什么想法可以帮助我吗?

最佳答案

一旦您从任务切换器向上滑动应用程序,您的应用程序将被终止,但 CoreLocation 仍将在操作系统级别为您工作,寻找与您所在地区匹配的信标:

locationManager.startMonitoring(for: beaconRegion)

一旦该区域发生变化(didEnter 或 didExit),您的应用将重新启动到后台。此时将按顺序调用以下方法:

  1. application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?)

  2. locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion)

届时您的应用将继续在后台运行。因为您的应用启动后台任务,所以这将持续 180 秒,然后被挂起。

但是,您的应用不会在从屏幕滑出到下一次区域转换发生之间运行。这是您能做的最好的事情。

关于ios - 如何在应用程序未运行(终止/终止)时保持 Core Location 和 Core Bluetooth 运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45317641/

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