Skip to content

Instantly share code, notes, and snippets.

@zewuchen
Created August 30, 2022 13:32
Show Gist options
  • Save zewuchen/7bfc996acaf6a9812da7e7b36fe20ef9 to your computer and use it in GitHub Desktop.
Save zewuchen/7bfc996acaf6a9812da7e7b36fe20ef9 to your computer and use it in GitHub Desktop.
import UIKit
import Foundation
import CoreLocation
final class ViewController: UIViewController {
private let locationManager: CLLocationManager = {
let manager = CLLocationManager()
return manager
}()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
if locationManager.authorizationStatus != .authorizedAlways ||
locationManager.authorizationStatus != .authorizedWhenInUse {
locationManager.requestAlwaysAuthorization()
} else {
startMonitoringArea()
}
}
private func startMonitoringArea() {
let centerLocation = CLLocationCoordinate2D(latitude: CLLocationDegrees(-23.5614),
longitude: CLLocationDegrees(-46.6559))
let area = CLLocationDistance(1000)
let region = CLCircularRegion(center: centerLocation,
radius: area,
identifier: "monitoringRegion")
region.notifyOnExit = false
region.notifyOnEntry = true
locationManager.startMonitoring(for: region)
}
}
extension ViewController: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
print("Entrou na região")
}
func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
print("Saiu da região")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment