首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iBeacon、iOS13、BGAppRefreshTask

iBeacon、iOS13、BGAppRefreshTask
EN

Stack Overflow用户
提问于 2020-01-08 20:50:34
回答 1查看 571关注 0票数 1

我需要实现在后台检测iBeacons每例如5分钟,为此,我想使用新的苹果框架BackgroundTask

下面是我实现我的操作子类的方式

代码语言:javascript
复制
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 {

代码语言:javascript
复制
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 {

代码语言:javascript
复制
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)
}

}

EN

回答 1

Stack Overflow用户

发布于 2020-01-10 11:55:06

在基于信标的项目工作了两年之后,我可以说,没有办法在后台检测iBeacon,特别是在你的场景中。

在iOS后台,任务将在3分钟后终止如果您开始后台确定任务,以节省电池。唯一的办法是,在信标测距模式下工作app时,可以每5分钟调用一次线程来确定信标的最后状态,而不会终止应用程序。屏幕可以关闭,但应用程序仍必须正常工作。但这不是合法的情况。审查团队不会因为保护用户利益而批准你的应用程序。

在后台的信标监控没有问题,,但您只能知道信标信号是否仍与设备连接,或在30秒内未连接。

您必须深入了解监控和区域扫描之间的区别。在这种情况下,如果您的方案不是从Background Monitoring应用的,则无法使用信标测距每隔5分钟确定后台的信标状态。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59646277

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档