【问题标题】:Core Location StartMonitoring Background Working核心位置 StartMonitoring 后台工作
【发布时间】:2020-04-20 16:51:01
【问题描述】:

当应用程序在前台工作时我可以扫描信标,但不能在后台工作应用程序。我在 İinfo.plist 和后台模式“位置更新”中添加了“隐私 - 始终位置和使用时的使用说明”和“隐私 - 使用时的位置使用说明”

func initScanBeacon() {

    locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.requestAlwaysAuthorization()
    locationManager.allowsBackgroundLocationUpdates = true
    locationManager.pausesLocationUpdatesAutomatically = false

  }

  func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
    if status == .authorizedAlways {

      if CLLocationManager.isMonitoringAvailable(for: CLBeaconRegion.self) {
        if CLLocationManager.isRangingAvailable() {
          startScanning()
        }
      }
    }
  }

  func startScanning() {
    let uuid = UUID(uuidString: "xxxxx")!
    let beaconRegion = CLBeaconRegion(proximityUUID: uuid, major: 0, minor: 0, identifier: "xxxxx")

    locationManager.startMonitoring(for: beaconRegion)
    locationManager.startRangingBeacons(in: beaconRegion)
  }

  func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) {
    if beacons.count > 0 {
      //print
    } else {

    }
  }

【问题讨论】:

    标签: swift core-location beacon


    【解决方案1】:

    默认情况下,iOS 应用在切换到后台后的几秒钟内不会继续探测信标。即使您从用户那里获得“始终”位置使用权限并设置locationManager.allowsBackgroundLocationUpdates = true,也是如此。

    您可以采取多种技巧将背景范围延长到几秒钟以上。具体可以看我的详细回答here

    【讨论】:

      猜你喜欢
      • 2012-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多