ios - How to keep Core Location and Core Bluetooth running when app is not running (killed/ terminated)? -
i got stuck when trying keep function running if terminate app.
is possible keep core location (geofencing / geolocating) , core bluetooth running when app not running ? if possible how solve issue? check background modes, , implement core location method. code :
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, 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 { 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) } }
from code, can run in both in foreground , background. when swipe app task switcher, cannot work. , cannot see log xcode. ideas me ?
once swipe app task switcher app killed, corelocation still working @ os level, looking beacons match region:
locationmanager.startmonitoring(for: beaconregion)
once there change on region (either didenter or didexit), app re-launched background. @ time following methods called in order:
application(_ application: uiapplication, didfinishlaunchingwithoptions launchoptions: [uiapplicationlaunchoptionskey: any]?)
locationmanager(_ manager: cllocationmanager, didenterregion region: clregion)
at time app continue running in background. because app starts background task, continue 180 seconds before being suspended.
however, app not run between time swiped off screen , when next region transition happens. best can do.
Comments
Post a Comment