Skip to content

Instantly share code, notes, and snippets.

@MickaelCruzDB
Created July 11, 2015 09:59
Show Gist options
  • Save MickaelCruzDB/f60b62c26f99a87d5685 to your computer and use it in GitHub Desktop.
Save MickaelCruzDB/f60b62c26f99a87d5685 to your computer and use it in GitHub Desktop.
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
@IBOutlet var distanceReading: UILabel!
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
view.backgroundColor = UIColor.grayColor()
startScanning()
}
func startScanning() {
let uuid = NSUUID(UUIDString: "B9407F30-F5F8-466E-AFF9-25556B57FE6D")
let beaconRegion = CLBeaconRegion(proximityUUID: uuid, major: 48347, minor: 55103, identifier: "Table1")
locationManager.startMonitoringForRegion(beaconRegion)
locationManager.startRangingBeaconsInRegion(beaconRegion)
}
func updateDistance(distance: CLLocationAccuracy) {
UIView.animateWithDuration(0.8) { [unowned self] in
switch distance {
case 1.1...4:
self.view.backgroundColor = UIColor.orangeColor()
self.distanceReading.text = "BYE BEACON!"
case 0...1.1:
self.view.backgroundColor = UIColor.greenColor()
self.distanceReading.text = "HI BEACON!"
default:
self.view.backgroundColor = UIColor.orangeColor()
self.distanceReading.text = "BYE BEACON!"
}
}
}
func locationManager(manager: CLLocationManager!, didRangeBeacons beacons: [AnyObject]!, inRegion region: CLBeaconRegion!) {
if beacons.count > 0 {
let beacon = beacons[0] as! CLBeacon
updateDistance(beacon.accuracy)
} else {
updateDistance(3)
}
}
func locationManager(manager: CLLocationManager!, didExitRegion region: CLRegion!) {
println("You quit the region")
}
func locationManager(manager: CLLocationManager!, didEnterRegion region: CLRegion!) {
println("You enter the region")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment