Created
June 19, 2018 22:28
-
-
Save 4taras4/772b9e20185ad5d9bde71e4d6fcff9f8 to your computer and use it in GitHub Desktop.
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
import UserNotifications | |
import CoreLocation | |
class LocationManager: NSObject, CLLocationManagerDelegate { | |
let locationManager = CLLocationManager() | |
var locationServicesStatus = CLAuthorizationStatus.notDetermined | |
override init() { | |
super.init() | |
locationManager.delegate = self | |
} | |
func resetLocationServices() { | |
// identifiers must be uniqu not uniqu id will be overrited | |
// load you setting for monitoring | |
let uuid = UUID().uuidString | |
let region1 = CLBeaconRegion(proximityUUID: uuid, identifier: uuid.uppercased()) | |
region1.notifyOnEntry = true | |
region1.notifyOnExit = true | |
locationManager.startMonitoring(for: region1) | |
let uuid2 = UUID().uuidString | |
let region2 = CLBeaconRegion(proximityUUID: uuid2, identifier: uuid2.uppercased()) | |
region2.notifyOnEntry = true | |
region2.notifyOnExit = true | |
locationManager.startMonitoring(for: region2) | |
} | |
func locationManager(_ manager: CLLocationManager, didStartMonitoringFor region: CLRegion) { | |
print(region.debugDescription) | |
} | |
func locationManager(_ manager: CLLocationManager, monitoringDidFailFor region: CLRegion?, withError error: Error) { | |
DDLogError("Monitoring failed for region with identifier: \(String(describing: region)) \(error.localizedDescription)") | |
} | |
func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) { | |
for beacon in beacons { | |
print(beacon.proximityUUID.uuidString) | |
print(beacon.major.stringValue) | |
print(beacon.minor.stringValue) | |
print(region.identifier) | |
print(beacon.rssi.description) | |
let content = UNMutableNotificationContent() | |
content.title = "Show local push" | |
content.body = "You are in beacon range" | |
content.sound = UNNotificationSound.default() | |
content.categoryIdentifier = "LocationServices" | |
DispatchQueue.main.async { | |
let req = UNNotificationRequest(identifier: "LocationServices", content: content, trigger: nil) | |
UNUserNotificationCenter.current().add(req) | |
manager.stopRangingBeacons(in: beacon) | |
} | |
} | |
} | |
func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) { | |
print("Located region") | |
if let localRegion = region as? CLBeaconRegion { | |
print("Beacon region") | |
manager.startRangingBeacons(in: localRegion) | |
} | |
} | |
func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) { | |
UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: ["LocationServices"]) | |
print("Leave Beacon region") | |
} | |
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) { | |
self.locationServicesStatus = status | |
if (status == CLAuthorizationStatus.denied) { | |
print("[Location]: The user denied authorization") | |
} else if (status == CLAuthorizationStatus.authorizedAlways) { | |
if UserDefaults.standard.bool(forKey: "location_services") { | |
resetLocationServices() | |
} | |
} else if (status == CLAuthorizationStatus.notDetermined) { | |
print("[Location]: Not determined") | |
} else { | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment