Как правильно организовать данный код, что бы location manager обрабатывал более 20 регионов?
Сейчас получаю сообщение при входе и выходе в заданную зону только по одному региону.
import UIKit
import UserNotifications
import CoreLocation
class ViewController: UIViewController, UNUserNotificationCenterDelegate, CLLocationManagerDelegate {
let locationManager = CLLocationManager()
var region : CLCircularRegion!
override func viewDidLoad() {
super.viewDidLoad()
getUserPermission()
locationManager.delegate = self
// must
locationManager.requestWhenInUseAuthorization()
// Simple repeatable way
let notCenter = CLLocationCoordinate2D(latitude: 43.0322, longitude: 44.6441)
let notRegion = CLCircularRegion(center: notCenter, radius: 25.0, identifier: "lhl")
notRegion.notifyOnEntry = true
notRegion.notifyOnExit = true
let trigger = UNLocationNotificationTrigger(region: notRegion, repeats: false)
showNotification(inpTrigger: trigger)
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = 50;
let center = CLLocationCoordinate2D(latitude: 43,
longitude: 44)
region = CLCircularRegion(center: center,
radius: 25.0,
identifier: "lhl")
if !CLLocationManager.isMonitoringAvailable(for: CLCircularRegion.self) {
print("Can't use this for monitorinh")
}
locationManager.startUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager,
didUpdateLocations locations: [CLLocation]) {
print(locations)
}
func locationManager(_ manager: CLLocationManager, monitoringDidFailFor region: CLRegion?, withError error: Error) {
print("failed to monitor")
}
func locationManager(_ manager: CLLocationManager, didStartMonitoringFor region: CLRegion) {
print("began monitoring for \(region)")
}
func locationManager(_ manager: CLLocationManager,
didEnterRegion region: CLRegion) {
print("Geofence Entered")
let trigger = UNLocationNotificationTrigger(region: region,
repeats: false)
showNotification(inpTrigger: trigger)
}
func locationManager(_ manager: CLLocationManager,
didExitRegion region: CLRegion) {
print("Выход Из Геозоны")
}
func showNotification (inpTrigger: UNLocationNotificationTrigger) {
let locationTrigger = inpTrigger
let locationContent = UNMutableNotificationContent()
locationContent.title = "Точка доступа Ледовая Арена"
locationContent.body = "Поздравляю! Вы добрались до ледовой Арены."
locationContent.sound = UNNotificationSound.default()
// Создание запроса уведомления
let request = UNNotificationRequest(identifier: "myId",
content: locationContent,
trigger: locationTrigger)
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
UNUserNotificationCenter.current().add(request) {(error) in
if let error = error {
print("ошибка с запросом: \(error)")
}
}
}
func getUserPermission() {
// предоставление пользователю доступа для получения уведомлений
UNUserNotificationCenter
.current()
.requestAuthorization(options: [.alert, .sound]) {
(accepted, error) in
var alert : UIAlertController;
if !accepted {
alert = UIAlertController(title: "Статус",
message: "Доступ к уведомлению запрещен.",
preferredStyle: UIAlertControllerStyle.alert);
} else {
alert = UIAlertController(title: "Статус",
message: "Доступ к уведомлениям предоставлен.",
preferredStyle: UIAlertControllerStyle.alert);
}
alert.addAction(UIAlertAction(title: "Ok",
style: UIAlertActionStyle.default,
handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
}