【问题标题】:iBeacon ,iOS13 , BGAppRefreshTaskiBeacon ,iOS13 , BGAppRefreshTask
【发布时间】:2020-04-26 00:20:21
【问题描述】:

我需要每隔 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 实现

@UIApplicationMain 类 AppDelegate: 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】:

    在从事基于 Beacon 的项目超过 2 年之后,我可以说没有办法在后台检测 iBeacon,特别是在您的场景中

    在 iOS 后台,任务将在 3 分钟后终止如果您开始后台确定您启动的任务,以节省电量。唯一的方法是,当应用程序在信标测距模式下工作时,您可以每 5 分钟调用一次线程以确定信标最后状态,而无需终止应用程序。屏幕可以关闭,但应用程序必须仍然可以工作。 但这不是法律规定。Apple 审核团队不批准您的申请以保护用户的利益。

    Beacon Monitoring在后台没有问题,但你只知道Beacon信号是否仍然连接到设备,或者超过30秒没有连接。

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

    更多信息请阅读https://community.estimote.com/hc/en-us/articles/203914068-Is-it-possible-to-use-beacon-ranging-in-the-background-

    【讨论】:

    • 也许我错过了一些东西,但后台任务正是为它设计的,不是吗?刷新后台操作让您的应用程序在后台等待 30 秒以获取新数据并更新 UI。在我的情况下,我从信标周围而不是某些 Web 服务器获取新数据。
    猜你喜欢
    • 1970-01-01
    • 2021-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-10
    • 1970-01-01
    相关资源
    最近更新 更多