Skip to content

Instantly share code, notes, and snippets.

@aurozhkov
Last active September 25, 2016 05:44
Show Gist options
  • Save aurozhkov/26cdbc2d26a0e875e552b910bb98e901 to your computer and use it in GitHub Desktop.
Save aurozhkov/26cdbc2d26a0e875e552b910bb98e901 to your computer and use it in GitHub Desktop.
iBeacons finding
import CoreLocation
let beaconUUIDs = ["f7826da6-4fa2-4e98-8024-bc5b71e0893e"]
let locationManager = CLLocationManager()
locationManager.delegate = self
for uuid in beaconUUIDs {
let beaconRegion = CLBeaconRegion(proximityUUID: NSUUID(UUIDString:uuid)!, identifier: uuid)
manager.startMonitoringForRegion(beaconRegion)
}
public func locationManager(manager: CLLocationManager, didDetermineState state: CLRegionState,
forRegion region: CLRegion) {
if let region = region as? CLBeaconRegion {
if state == .Inside {
manager.startRangingBeaconsInRegion(region)
} else if state == .Outside {
manager.stopRangingBeaconsInRegion(region)
//мы вышли из зоны всех beacons
}
}
}
public func locationManager(manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon],
inRegion region: CLBeaconRegion) {
var sortedBeacons = beacons.filter({ $0.proximity != .Unknown })
sortedBeacons.sortInPlace({
let firstBeaconProximity = $0.proximity.rawValue
let secondBeaconProximity = $1.proximity.rawValue
return firstBeaconProximity < secondBeaconProximity
|| (firstBeaconProximity == secondBeaconProximity && $0.accuracy < $1.accuracy)
})
//мы находимся ближе всего к sortedBeacons.first
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment