Created
February 26, 2015 06:17
-
-
Save ankitthakur/9ba627abca14b76051af to your computer and use it in GitHub Desktop.
Geofence Core Location and Beacon Integration
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
// | |
// GeofenceViewController.swift | |
// AppleHomeKit | |
// | |
// Created by Ankit Thakur on 2/23/15. | |
// Copyright (c) 2015 Ankit Thakur. All rights reserved. | |
// | |
import UIKit | |
import CoreLocation; | |
@objc protocol GeofenceViewControllerDelegate:NSObjectProtocol { | |
optional func deviceDidUpdateSignificantLocation(center: CLLocationCoordinate2D) | |
optional func deviceDidUpdatePreciseLocation(center: CLLocationCoordinate2D) | |
optional func didEnterTagRegion(tagID: String) | |
optional func didExitTagRegion(tagID: String) | |
optional func didEnterBeaconRegion(); | |
optional func didExitBeaconRegion(); | |
} | |
class GeofenceViewController: NSObject,CLLocationManagerDelegate { | |
var locationManager:CLLocationManager = CLLocationManager(); | |
var geofenceBeacon:CLBeaconRegion?; | |
var isEnteredInBeaconRegion = false; | |
var geofenceDelegate:GeofenceViewControllerDelegate?; | |
// singleton | |
class var sharedInstance: GeofenceViewController { | |
struct Static { | |
static var instance: GeofenceViewController? | |
static var token: dispatch_once_t = 0 | |
} | |
dispatch_once(&Static.token) { | |
Static.instance = GeofenceViewController() | |
} | |
return Static.instance! | |
} | |
func initializeGeoFenceKit(){ | |
self.locationManager.delegate = self; | |
self.locationManager.requestWhenInUseAuthorization(); | |
self.locationManager.requestAlwaysAuthorization(); | |
self.locationManager.startUpdatingLocation() | |
if(NSUserDefaults.standardUserDefaults().objectForKey(iBeaconScan) != nil){ | |
self.startMonitoringBeaconRegion(NSUserDefaults.standardUserDefaults().objectForKey(iBeaconScan) as! String); | |
} | |
} | |
func startMonitoringBeaconRegion(beaconuuId:String){ | |
var beaconuId = "B9407F30-F5F8-466E-AFF9-25556B57FE6D";//"e2c56db5-dffb-48d2-b060-d0f5a71096e0"; | |
if(count(beaconuuId) == count(beaconuId)){ | |
self.geofenceBeacon = CLBeaconRegion(proximityUUID: NSUUID(UUIDString:beaconuuId)!, identifier: "Estimotes"); | |
} | |
if(self.geofenceBeacon == nil){ | |
var alertView:UIAlertView = UIAlertView(title: "", message:"Error, Please re-verify the entered UUID of the beacon.", delegate: nil, cancelButtonTitle: "Ok"); | |
alertView.show(); | |
self.geofenceBeacon = CLBeaconRegion(proximityUUID: NSUUID(UUIDString:beaconuId)!, identifier: "Estimotes"); | |
} | |
self.geofenceBeacon!.notifyOnEntry = true; | |
self.geofenceBeacon!.notifyOnExit = true; | |
self.geofenceBeacon!.notifyEntryStateOnDisplay = true; | |
self.locationManager.startMonitoringForRegion(self.geofenceBeacon); | |
} | |
func locationManager(manager: CLLocationManager!, didStartMonitoringForRegion region: CLRegion!) { | |
self.locationManager.startRangingBeaconsInRegion(self.geofenceBeacon); | |
manager.requestStateForRegion(self.geofenceBeacon) | |
println("Scanning..., \(region)"); | |
} | |
func locationManager(manager: CLLocationManager!, didDetermineState state: CLRegionState, forRegion inRegion: CLRegion!) { | |
println("didDetermineState CLRegion \(inRegion)"); | |
println("didDetermineState CLRegionState \(state)"); | |
manager.startRangingBeaconsInRegion(self.geofenceBeacon) | |
} | |
func locationManager(manager: CLLocationManager!, monitoringDidFailForRegion region: CLRegion!, withError error: NSError!) { | |
println("monitoringDidFailForRegion \(error) -> \(region)"); | |
} | |
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) { | |
println("didFailWithError \(error)") | |
} | |
func locationManager(manager: CLLocationManager!, didEnterRegion region: CLRegion!) { | |
manager.startRangingBeaconsInRegion(region as! CLBeaconRegion) | |
println("Possible Match"); | |
} | |
func locationManager(manager: CLLocationManager!, didExitRegion region: CLRegion!) { | |
println("didExitRegion"); | |
manager.stopRangingBeaconsInRegion(region as! CLBeaconRegion) | |
} | |
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) { | |
println("didUpdateLocations"); | |
} | |
func locationManager(manager: CLLocationManager!, didRangeBeacons beacons: [AnyObject]!, inRegion region: CLBeaconRegion!) { | |
println(beacons); | |
if(beacons.count == 0){ | |
if(isEnteredInBeaconRegion == true){ | |
AppleHomeKitUtilities.sharedInstance.didExitBeaconRegion(); | |
} | |
isEnteredInBeaconRegion = false; | |
} | |
else{ | |
let beacon:CLBeacon = beacons.last as! CLBeacon; | |
switch(beacon.proximity){ | |
case CLProximity.Unknown: | |
println("Unkown"); | |
break; | |
case CLProximity.Immediate: | |
println("Immediate"); | |
if(isEnteredInBeaconRegion == false){ | |
AppleHomeKitUtilities.sharedInstance.didEnterBeaconRegion(); | |
} | |
isEnteredInBeaconRegion = true; | |
break; | |
case CLProximity.Near: | |
println("Near"); | |
if(isEnteredInBeaconRegion == false){ | |
AppleHomeKitUtilities.sharedInstance.didEnterBeaconRegion(); | |
} | |
isEnteredInBeaconRegion = true; | |
break; | |
case CLProximity.Far: | |
println("Far"); | |
if(isEnteredInBeaconRegion == true){ | |
AppleHomeKitUtilities.sharedInstance.didExitBeaconRegion(); | |
} | |
isEnteredInBeaconRegion = false; | |
break; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment