gpt4 book ai didi

ios - iBeacon - 可以使用 startRangingBeacons 进行检测,但不能用于 didEnterRegion

转载 作者:可可西里 更新时间:2023-11-01 01:42:17 30 4
gpt4 key购买 nike

我需要检测设备何时进入或离开某个区域并根据此执行一些操作。

使用“startRangingBeaconsInRegion”,我可以检测到最近的 iBeacon 并据此更改背景颜色,如果无法检测到 iBeacon,则更改为白色。

不过,我无法让它在“didEnterRegion”或“didExitRegion”上触发。

我知道如果设备已经在该区域中,则不会触发 enterRegion。我确保未检测到信标(白屏),然后检测到信标(彩色屏幕)- 但没有触发。

我试过使用estimote SDK,但我遇到了同样的问题。重新启动设备也无济于事。

我的代码在下面,有什么建议吗?

import UIKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

let locationManager = CLLocationManager()
let region = CLBeaconRegion(proximityUUID: NSUUID(UUIDString: "B9407F30-F5F8-466E-AFF9-25556B57FE6D"), identifier: "Estimotes")

let colors = [
3861: UIColor(red: 84/255, green: 77/255, blue: 160/255, alpha: 1),
19152: UIColor(red: 142/255, green: 212/255, blue: 220/255, alpha: 1),
40527: UIColor(red: 162/255, green: 213/255, blue: 181/255, alpha: 1)
]

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
locationManager.delegate = self
locationManager.pausesLocationUpdatesAutomatically = false
region.notifyEntryStateOnDisplay = true
region.notifyOnEntry = true
region.notifyOnExit = true

// Request authorisation to track location
if (CLLocationManager.authorizationStatus() != CLAuthorizationStatus.AuthorizedWhenInUse) {
locationManager.requestWhenInUseAuthorization()
}


if (CLLocationManager .isMonitoringAvailableForClass(CLBeaconRegion))
{
println("OK")
}
else {
println("Problem")

}

locationManager.startMonitoringForRegion(region)
locationManager.startRangingBeaconsInRegion(region)
locationManager.startUpdatingLocation()

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func locationManager(manager: CLLocationManager!, didRangeBeacons beacons: [AnyObject]!, inRegion region: CLBeaconRegion!) {

let knownBeacons = beacons.filter { $0.proximity != CLProximity.Unknown }
if (knownBeacons.count > 0) {
let closestBeacon = knownBeacons[0] as CLBeacon
self.view.backgroundColor = self.colors[closestBeacon.minor.integerValue]
println(beacons)

}
else {
self.view.backgroundColor = UIColor(red: 255/255, green: 255/255, blue: 255/255, alpha: 1)
}
}
func locationManager(manager: CLLocationManager!, didEnterRegion region: CLRegion!) {
println("Region entered")
}

func locationManager(manager: CLLocationManager!, didExitRegion region: CLRegion!) {
println("Region exited")
}
func locationManager(manager: CLLocationManager!, monitoringDidFailForRegion region: CLRegion!, withError error: NSError!) {
println("FAIL!")
}

}

最佳答案

您正在尝试更改位置管理器回调中的 UI - 这是一个很大的禁忌,因为这些回调通常不在主线程上。将每个 UI 更改包装到主线程的调度 block 中,看看是否有任何更改。

啊,我前几天遇到了同样的问题。您需要实现:

func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
println("STATUS CHANGED: \(status.rawValue)")
if status == .AuthorizedAlways && !monitoring {
println("YAY! Authorized!")
locationManager.startMonitoringForRegion(beaconRegion)
monitoring = true
}
}

只有在系统告诉您您有权限后,您才能开始监控。

此外,如果您需要后台通知,您的 Info.plist 中必须包含此字符串:NSLocationAlwaysUsageDescription , 你需要 App registers for location updatesRequired background modes 中定义.

关于ios - iBeacon - 可以使用 startRangingBeacons 进行检测,但不能用于 didEnterRegion,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28646654/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com