[iBeacon,iOS13,BGAppRefreshTask

问题描述 投票:1回答:1

我需要实现每5分钟在后台检测一次iBeacons的工具,为此,我想使用新的Apple框架BackgroundTask

这是我实现操作子类的方式

import CoreLocation

class BeaconsRecord: AsyncOperation {

    private let locationManager: CLLocationManager
    private let clBeaconIdentityConstraint: CLBeaconIdentityConstraint
    var beacons = [CLBeacon]()

    init (locationManager: CLLocationManager, uuid: UUID) {
        self.locationManager = locationManager
        self.clBeaconIdentityConstraint = CLBeaconIdentityConstraint(uuid: uuid)
        super.init()
    }

    override func main() {
        locationManager.delegate = self
        locationManager.startRangingBeacons(satisfying: clBeaconIdentityConstraint)
    }
}

extension BeaconsRecord: CLLocationManagerDelegate {

    func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) {
        self.locationManager.stopRangingBeacons(satisfying: clBeaconIdentityConstraint)
        self.beacons = beacons
        self.state = .Finished
    }
}

如果我从前台使用它,它可以完美工作,然后我尝试从后台使用它,如wwdc演示中所示,它不起作用。我想念吗?我的appdelegate实施

@ UIApplicationMainAppDelegate类:UIResponder,UIApplicationDelegate {

var window: UIWindow?
var locationManager : CLLocationManager?
let uuid = UUID(uuidString: "B9407F30-F5F8-466E-AFF9-25556B57FE6D")!

lazy var detectBeaconsQueue : OperationQueue = {
    var queue = OperationQueue()
    queue.name = "detectBeaconsQueue"
    queue.maxConcurrentOperationCount = 1
    return queue
}()

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    locationManager = CLLocationManager()
    locationManager?.requestAlwaysAuthorization()

    registerBackgroundTaks()
    registerLocalNotification()

    return true
}

//MARK: Register BackGround Tasks
func registerBackgroundTaks() {
    BGTaskScheduler.shared.register(forTaskWithIdentifier: "xxx.xxxxxx.xxxxxxxxxxx", using: nil) { task in
         self.scheduleLocalNotification()
         self.handleAppRefresh(task: task as! BGAppRefreshTask)
      }
}

func applicationDidEnterBackground(_ application: UIApplication) {
    cancelAllPendingBGTask()
    scheduleAppRefresh()
}

}

扩展名AppDelegate {

func cancelAllPendingBGTask() {
    BGTaskScheduler.shared.cancelAllTaskRequests()
}

func scheduleAppRefresh() {
       let request = BGAppRefreshTaskRequest(identifier: ""xxx.xxxxxx.xxxxxxxxxxx"")

       // Fetch no earlier than 1 minutes from now
    request.earliestBeginDate = Date(timeIntervalSinceNow: 1 * 60)
       do {
          try BGTaskScheduler.shared.submit(request)
       } catch {
          print("Could not schedule app refresh: \(error)")
       }
    }

func handleAppRefresh(task: BGAppRefreshTask) {
    // Schedule a new refresh task
    scheduleAppRefresh()

    let operation = BeaconsRecord(locationManager: locationManager!,
                                  uuid: uuid)

    task.expirationHandler = {
        operation.cancel()
    }

    operation.completionBlock = {
        task.setTaskCompleted(success: !operation.isCancelled)
    }

    detectBeaconsQueue.addOperation(operation)
}

}

background core-location ios13 operation
1个回答
0
投票

在基于Beacon的项目工作了2年后,我可以说没有办法在您的方案中专门在后台检测iBeacon

在iOS后台,该任务将在3分钟后终止如果您开始进行后台确定,则可以启动任务,以节省电池。唯一的方法是,在信标测距模式下运行应用程序时,可以每5分钟调用一次线程以确定信标的最后状态,而无需终止应用程序。屏幕可以关闭,但该应用程序仍然可以运行。 但是这不是合法情况。 Apple审核小组不批准您的应用程序为了保护用户的利益。

Beacon Monitoring在后台没有问题,,但您只知道信标信号是否仍连接到设备,或在30秒钟内未连接。

您必须深入了解监视和区域扫描之间的区别。并且,如果在这种情况下未从Background Monitoring应用场景,则无法每隔5分钟使用Beacon Ranging确定后台的信标状态。”>

有关更多信息,请阅读

https://community.estimote.com/hc/en-us/articles/203914068-Is-it-possible-to-use-beacon-ranging-in-the-background-
© www.soinside.com 2019 - 2024. All rights reserved.