从检测到的信标中检索对象

时间:2015-11-07 12:17:26

标签: ios swift ibeacon

我创建了一个扫描功能,可以注册Content个对象的所有区域。但是我似乎无法看到我如何在didRangeBeacons委托方法中检索此Content对象?该对象包含有关根据信标要处理的内容的信息?

StartScanning

func startScanning() {

    print(allContent[0].beacon?.UUID)

    for beacon in allContent {

        let uuid = NSUUID(UUIDString: beacon.beacon!.UUID)!
        let beaconRegion = CLBeaconRegion(proximityUUID: uuid, major: UInt16(beacon.beacon!.major), minor: UInt16(beacon.beacon!.minor), identifier: "MiniBeacon_10260")


        manager.startMonitoringForRegion(beaconRegion)
        manager.startRangingBeaconsInRegion(beaconRegion)

    }


}

didRangeBeacons

func locationManager(manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], inRegion region: CLBeaconRegion) {

    if beacons.count > 0 {
        let beacon = beacons[0]
        updateDistance(beacon.proximity)
    } else {
        updateDistance(.Near)
    }
}

以下是内容对象的示例

class Content: Object{
    dynamic var id: Int = 0
    dynamic var date: NSDate = NSDate()
    dynamic var thumbnail: NSData = NSData()
    //Customization
    dynamic var navTitle: String = ""
    dynamic var navBarColor: String = ""
    dynamic var navButtonsColor: String = ""
    dynamic var beacon: Beacon?
    //Flags
    dynamic var isPresented: Bool = false
    dynamic var favorite: Bool = false
    dynamic var favoriteDate: NSDate = NSDate()


    override class func primaryKey() -> String {
        return "id"
    }



}

1 个答案:

答案 0 :(得分:0)

应用程序中的某处必须有Content个对象的集合。在第一个代码段中,有一个变量allContent,可能代表了这个完整的对象列表。

您可以做的是遍历所有这些内容对象,以找到其信标标识符与远程信标匹配的内容对象。像这样:

func locationManager(manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], inRegion region: CLBeaconRegion) {
  for rangedBeacon in beacons {
    for content in allContent {
      if let contentBeacon = content.beacon {

        if rangedBeacon.proximityUUID.UUIDString == contentBeacon.proximityUUID.UUIDString &&
           rangedBeacon.major.integerValue == contentBeacon.major.integerValue &&
           rangedBeacon.minor.integerValue == contentBeacon.minor.integerValue {
            // content now contains the content for rangedBeacon
            // TODO: do something with the matching content here
        }
      }
    }
  }
}

上面的代码非常强大,如果您将Content对象存储在CoreData或其他可由信标UUID / major /查询的结构中,则可能有更有效的方法执行此操作。次要的。