如何获得有关特定坐标(纬度)目标 C 到达的本地通知



我正在开发具有以下功能的应用程序。

有许多餐厅在申请中注册。如果用户到达任何喜欢的餐厅附近,则应用程序将显示该餐厅的本地通知。

我正在寻找有效的 Objective C 代码来将其集成到我的项目中。

为所有喜爱的餐厅创建地理围栏。

您可以参考本指南

https://spin.atomicobject.com/2015/07/16/geofencing-ios-objectivec/

适用于 iOS 10 及更高版本

如果要发送带有UNNotificationRequest的本地通知,可以执行以下操作:

  1. 要求requestAlwaysAuthorization并在info.plist文件中添加NSLocationAlwaysUsageDescription。此外,将背景模式添加到位置。

    var locationManager:CLLocationManager
    

    在里面viewWillAppear这个来请求AlwaysAuthorization权限

    locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.requestAlwaysAuthorization()
    
  2. 然后,使用 startMonitoringSignificantLocationChangesstartUpdatingLocation 来监视位置更改。

  3. 实现 CLLocationManagerDelegate locationManager:didEnterRegion:,并在进入特定位置时显示本地通知。

    func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion){
        //Show local notification when entered a desired location
        showLocalNotification("Entered (region.identifier)")
    }
    func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion){
        //Show local notification when exit from a desired location
         showLocalNotification("Exited (region.identifier)")
    }
    

对于早期版本

如果要使用 UILocalNotification ,可以在 UILocalNotification 对象中添加CLRegion。 当用户进入/退出地理区域 CLRegion 时,iOS 将自动显示本地通知。

let localNotification = UILocalNotification()
localNotification.alertTitle = "Hi there"
localNotification.alertBody = "you are here"
let region = CLCircularRegion(center: CLLocationCoordinate2D(latitude: 4.254, longitude: 88.25), radius: CLLocationDistance(100), identifier: "")
region.notifyOnEntry = true
region.notifyOnExit = false
localNotification.region = region       
localNotification.timeZone = NSTimeZone.localTimeZone()
localNotification.soundName = UILocalNotificationDefaultSoundName
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)

这是来自Apple的链接,关于获取用户的位置

最新更新