我的应用程序在前台和后台运行时可以正常运行信标监控,即使我退出应用程序它在后台启动应用程序..但是当我重新启动手机时它不会被唤醒..
//我也在使用背景模式进行定位
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
//do i have to do anything here
return true
}
//当应用程序命中背景波纹管方法将触发并且我正在开始区域监控
func applicationDidEnterBackground(_ application: UIApplication) {
do {
try DLGatewayInterface.getInstance().enableBackgroundWakeups()
try DLGatewayInterface.getInstance().startBackgroundScan(start: true)
} catch let error {
print(error)
}
}
//当应用程序启动区域监控时,这将触发我设置委托
var gatewayBridgeDelegate: DLGatewayBridgeDelegate?
private var locationManager:CLLocationManager
private var Region:CLBeaconRegion
var savedDevices = UserDefaults.standard
var major = ""
var minor = ""
override init() {
locationManager = CLLocationManager()
super.init()
locationManager.delegate = self
// required as "ALWAYS" for iBeacon
locationManager.requestAlwaysAuthorization()
}
//每当app遇到背景时都会触发
public func startRegionMonitoring()
{
if let beaconName = UserDefaults.standard.value(forKey: "beaconDeviceRegion") as? String{
print(beaconName)
self.decimalToHexForMajorMinor(value: beaconName)
danlawRegion = CLBeaconRegion(
proximityUUID: BeaconServiceId,
major: CLBeaconMajorValue(DLUtils.beaconMajor),
minor: CLBeaconMinorValue(DLUtils.beaconMinor),
identifier: "Danlaw")
// reset the regions...just in case
stopRegionMonitoring()
// only add it if you need to
if(!locationManager.monitoredRegions.contains(Region)) {
locationManager.startMonitoring(for: Region)
}
}
}
public func stopRegionMonitoring(){
danLogDebug()
locationManager.stopMonitoring(for: danlawRegion)
resetRegions() // clear all regions...
}
extension DLLocationManager: CLLocationManagerDelegate {
public func locationManager(_ manager: CLLocationManager,
didChangeAuthorization status: CLAuthorizationStatus) {
if status == .authorizedAlways || status == .authorizedWhenInUse {
}
}
public func locationManager(_ manager: CLLocationManager,
didFailWithError error: Error) {
danLogWarn(error.localizedDescription)
}
public func locationManager(_ manager: CLLocationManager,
didUpdateLocations locations: [CLLocation]) {
}
public func locationManager(_ manager: CLLocationManager,
didEnterRegion region: CLRegion) {
// in this bellow method i am calling scanning again
onRegionEnter(region: region.identifier)
}
public func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
onRegionExit(region: region.identifier)
}
public func locationManager(_ manager: CLLocationManager, monitoringDidFailFor region: CLRegion?, withError error: Error) {
}
public func locationManager(_ manager: CLLocationManager, didDetermineState state: CLRegionState, for region: CLRegion) {
onRegionDetermineState(region: region.identifier, state: state.rawValue)
}
public func locationManager(_ manager: CLLocationManager, didStartMonitoringFor region: CLRegion) {
onRegionMonitorStart(region: region.identifier)
}
}
答案 0 :(得分:1)
您必须设置信标监控并在didFinishLaunching
中设置CoreLocation委托。如果不这样做,您将无法获得自动启动行为。
var locationManager: CLLocationManager?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
locationManager = CLLocationManager()
locationManager?.delegate = self
let region = CLBeaconRegion(proximityUUID: Uuid, identifier: "my_region")
locationManager?.startMonitoring(for: region)
}