经过数小时的搜索和阅读,我找到了适合我的解决方案。(注意:我正在进行地理围栏,需要在用户离开特定区域时通知/调用API)
第一步也是最重要的一步是,用户必须授权"始终"访问位置。
其次,我们需要使用startMonitoringSignificantLocationChanges()
来更新位置,即使应用程序已经终止。
locationManager.requestAlwaysAuthorization()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
locationManager.allowsBackgroundLocationUpdates = true
locationManager.startUpdatingLocation()
locationManager.startMonitoringSignificantLocationChanges()
以下是获取位置更新本地通知的代码。
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate {
var window: UIWindow?
let locationManager = CLLocationManager()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
locationManager.requestAlwaysAuthorization()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
locationManager.allowsBackgroundLocationUpdates = true
locationManager.startUpdatingLocation()
locationManager.startMonitoringSignificantLocationChanges()
application.registerUserNotificationSettings(UIUserNotificationSettings(types: [.sound, .alert, .badge], categories: nil))
UIApplication.shared.cancelAllLocalNotifications()
return true
}
func alertUserOnLeaving(region:CLRegion){
if UIApplication.shared.applicationState == .active {
let alert = UIAlertController(title: "Alert Title", message: "Alert Message", style = .Alert
window?.rootViewController?.present(alert, animated: true, completion: nil)
}
else{
let notification = UILocalNotification()
notification.alertBody = "You forgot to checkout"
notification.soundName = "Default"
UIApplication.shared.presentLocalNotificationNow(notification)
}
}
func alertUserOnArrival(region:CLRegion){
if UIApplication.shared.applicationState == .active {
let alert = UIAlertController(title: "Alert Title", message: "Alert Message", style = .Alert
window?.rootViewController?.present(alert, animated: true, completion: nil)
}
else{
let notification = UILocalNotification()
notification.alertBody = "Welcome Please checkin"
notification.soundName = "Default"
UIApplication.shared.presentLocalNotificationNow(notification)
}
}
func setUpGeofenceForJob() {
let geofenceRegionCenter = CLLocationCoordinate2DMake(-33.7513580322265, 151.242416381836)
let geofenceRegion = CLCircularRegion(center: geofenceRegionCenter, radius: 100, identifier: "GeoFence")
geofenceRegion.notifyOnExit = true
geofenceRegion.notifyOnEntry = true
self.locationManager.startMonitoring(for: geofenceRegion)
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if (status == CLAuthorizationStatus.authorizedAlways) {
self.setUpGeofenceForJob()
}
}
func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
alertUserOnArrival(region: region)
}
func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
alertUserOnLeaving(region: region)
}
希望这有所帮助。