Created
May 27, 2015 10:00
-
-
Save gmertk/0c19f614f320296b99b6 to your computer and use it in GitHub Desktop.
Use sets for regions in didRangeBeacons
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func locationManager(manager: CLLocationManager!, didRangeBeacons beacons: [AnyObject]!, inRegion region: CLBeaconRegion!) { | |
var latestRegions = Set<CLBeaconRegion>() | |
// Create CLBeaconRegions from CLBeacons | |
if let beacons = beacons as? [CLBeacon] { | |
for beacon in beacons { | |
latestRegions.insert(regionFromBeacon(beacon)) | |
} | |
} | |
let enteredRegions = latestRegions.subtract(currentRegions) | |
let exitedRegions = currentRegions.subtract(latestRegions) | |
currentRegions = latestRegions | |
if enteredRegions.count > 0 { | |
println("Entered") | |
println(enteredRegions) | |
} | |
if exitedRegions.count > 0 { | |
println("Exited") | |
println(exitedRegions) | |
} | |
} | |
func regionFromBeacon(beacon: CLBeacon) -> CLBeaconRegion { | |
let major = CLBeaconMajorValue(beacon.major.integerValue) | |
let minor = CLBeaconMinorValue(beacon.minor.integerValue) | |
let identifier = "\(beacon.proximityUUID.UUIDString).\(major).\(minor)" // Used for "is equal" check in Sets | |
return CLBeaconRegion(proximityUUID: beacon.proximityUUID, major: major, minor: minor, identifier: identifier) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment