Last active
January 21, 2016 18:09
-
-
Save caseycoding/1e88a651d26222c75752 to your computer and use it in GitHub Desktop.
Swift BCZoneMonitor example
This file contains hidden or 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
// | |
// CCZoneMonitor.swift | |
// SwiftExamples | |
// | |
// Created by Casey Billman on 1/18/16. | |
// Copyright © 2016 BlueCats. All rights reserved. | |
// | |
import Foundation | |
import BlueCatsSDK | |
class CCZoneMonitor: NSObject, BCZoneMonitorDelegate { | |
var zoneMonitor: BCZoneMonitor! | |
let zoneIdentifierKeys = ["CATEGORY_ZONE_KEY", "ZONE_ID_KEY"] | |
/* | |
Zones are defined by adding a custom value to a beacon, category, or site. The custom value key must match the key passed to the BCZoneMonitor. | |
Useful notes when working with zones: | |
* Using your beacons in Secure Mode with and setting "Uses Bluetooth LE accessories" enabled in Background Modes will improve background zone recognition. | |
* If using 0.7.X and above increasing the default background ranging to suit your use case. E.g.: | |
BCOptionMaximumDailyBackgroundUsageInMinutes : 240 | |
BCOptionBackgroundSessionTimeIntervalInSeconds : 72000 | |
*/ | |
let shortTimeFormatter = NSDateFormatter.init() | |
func startCCZoneMonitor() { | |
shortTimeFormatter.timeStyle = NSDateFormatterStyle.MediumStyle | |
self.zoneMonitor = BCZoneMonitor.init(delegate: self, queue: nil, zoneIdentifierKeys: zoneIdentifierKeys) | |
self.zoneMonitor.startMonitoringZones() | |
print("Started zone monitoring!") | |
} | |
func zoneMonitor(monitor: BCZoneMonitor!, didEnterZone zone: BCZone!) { | |
zoneEventNotification(zone, zoneEvent: "Device entered zone '\(zone.identifier)' at \(self.shortTimeFormatter.stringFromDate(NSDate())) ") | |
print("Device entered zone '\(zone.identifier)' at \(self.shortTimeFormatter.stringFromDate(NSDate())) ") | |
} | |
func zoneMonitor(monitor: BCZoneMonitor!, didReEnterZone zone: BCZone!) { | |
zoneEventNotification(zone, zoneEvent: "Device re-entered zone '\(zone.identifier)' at \(self.shortTimeFormatter.stringFromDate(NSDate())) ") | |
print("Device re-entered zone '\(zone.identifier)' at \(self.shortTimeFormatter.stringFromDate(NSDate())) ") | |
} | |
func zoneMonitor(monitor: BCZoneMonitor!, didDwellInZone zone: BCZone!, forTimeInterval dwellTimeInterval: NSTimeInterval) { | |
zoneEventNotification(zone, zoneEvent: "Hit dwell time interval: \(dwellTimeInterval) for zone '\(zone.identifier)' at \(self.shortTimeFormatter.stringFromDate(NSDate()))") | |
print("Hit dwell time interval: \(dwellTimeInterval) for zone '\(zone.identifier)' at \(self.shortTimeFormatter.stringFromDate(NSDate()))") | |
} | |
func zoneMonitor(monitor: BCZoneMonitor!, didExitZone zone: BCZone!) { | |
zoneEventNotification(zone, zoneEvent: "Device just left zone '\(zone.identifier)' at \(self.shortTimeFormatter.stringFromDate(NSDate()))") | |
print("Device left zone '\(zone.identifier)' at \(self.shortTimeFormatter.stringFromDate(NSDate()))") | |
} | |
func zoneEventNotification(zone: BCZone, zoneEvent: String) { | |
let uiLocalNotification = UILocalNotification() | |
uiLocalNotification.alertBody = zoneEvent | |
uiLocalNotification.alertAction = "OK!" | |
uiLocalNotification.soundName = UILocalNotificationDefaultSoundName | |
UIApplication.sharedApplication().presentLocalNotificationNow(uiLocalNotification) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment