當進入此危險之地時,我需要有個推播告知自己
func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
let conten = UNMutableNotificationContent()
conten.title = "進來吧"
conten.body = "大喔"
conten.sound = .default
let request = UNNotificationRequest(identifier: "big", content: conten, trigger: nil)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}
當離開此危險之地時,我需要有個推播告知自己
func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
let conten = UNMutableNotificationContent()
conten.title = "已離開"
conten.body = "請回來"
conten.sound = .default
let request = UNNotificationRequest(identifier: "back", content: conten, trigger: nil)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}
這時,你需要先創建一個監控區間,否則他根本不知道要監控哪裡
func setupData() {
// 1. 檢查系統是否能夠監視 region
if CLLocationManager.isMonitoringAvailable(for: CLCircularRegion.self) {
// 2.準備 region 會用到的相關屬性
let title = "Lorrenzillo's"
let coordinate = CLLocationCoordinate2DMake(24.000000090700725,120.00000083064541)
let regionRadius = 10.0
// 3. 設置 region 的相關屬性
let region = CLCircularRegion(center: CLLocationCoordinate2D(
latitude: coordinate.latitude,
longitude: coordinate.longitude),
radius: regionRadius,
identifier: title)
locationmanager.startMonitoring(for: region)
}
}