UNLocationNotificationTrigger-不在模拟器中工作 [英] UNLocationNotificationTrigger- Not working in simulator

查看:450
本文介绍了UNLocationNotificationTrigger-不在模拟器中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用iOS 10中提供的用户通知框架我尝试在用户使用UNLocationNotificationTrigger输入特定地理位置时触发通知。
当我通过模拟地理位置尝试通过模拟器进行测试时,通知不会被触发,但位置管理器会返回更新的地理位置。这应该在真实设备中测试而不是在模拟器中运行吗?

Using User Notification framework available in iOS 10 i tried to trigger notification whenever User enters specific geo location using UNLocationNotificationTrigger. When I tried test it through simulator by simulating the Geo location, the notification is not getting triggered, but Location manager returns the updated geo location. Should this be tested in real device instead of running it in simulator?

推荐答案

根据 Apple文档


应用必须请求访问位置服务,并且必须具有使用该权限的使用权限。要请求使用位置服务的权限,请在调度任何基于位置的触发器之前调用CLLocationManager的requestWhenInUseAuthorization()方法。

Apps must request access to location services and must have when-in-use permissions to use this class. To request permission to use location services, call the requestWhenInUseAuthorization() method of CLLocationManager before scheduling any location-based triggers.

但是我的模拟器/设备使用中权限不够,权限必须设置为始终。

However with my emulators/devices "when-in-use" permissions are not enough, permissions must be set to "Always".

因此,将此密钥添加到pinfo.list

Thus, add this key to your pinfo.list

<key>NSLocationAlwaysUsageDescription</key>
<string>We use your location to warn you when there are adorable cats nearby</string>

然后激活位置。只有在您确定总是被授权时才定义您的触发器,例如我在didChangeAuthorizationStatus中执行此操作:

Then activate location. Define your trigger only once you're certain you're authorized always, for example I did it here in didChangeAuthorizationStatus:

class myClass : CLLocationManagerDelegate {

var locationManager: CLLocationManager() 

func init() {
   // Note: defining the location manager locally in this function won't work
   //    var locationManager: CLLocationManager() 
   // as it gets gargabe collected too early.

   locationManager.delegate = self
   locationManager.requestAlwaysAuthorization()
   UNUserNotificationCenter.current().delegate = self
   UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) {(accepted, error) in
   if !accepted {
         logger.info("Notification access denied.")
   }

}

// MARK CLLocationManagerDelegate:
func locationManager(manager: CLLocationManager,
                     didChangeAuthorizationStatus status: CLAuthorizationStatus)
{
    if status == .AuthorizedAlways  {

        let region = CLCircularRegion(center:    CLLocationCoordinate2D(latitude: 61.446812, longitude: 23.859914),
                  radius: 1000, identifier: "test")
        logger.info("Notification will trigger at \(region)")
        region.notifyOnEntry = true
        region.notifyOnExit = false

        let trigger = UNLocationNotificationTrigger(region: region, repeats:true)

        let content = UNMutableNotificationContent()
        content.title = "Oh Dear !"
        content.body = "It's working!"
        content.sound = UNNotificationSound.default()

        let request = UNNotificationRequest(identifier: "textNotification", content: content, trigger: trigger)

        UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
        UNUserNotificationCenter.current().add(request) {(error) in
           if let error = error {
               print("Uh oh! We had an error: \(error)")
           }
        }

    }
}
}

这篇关于UNLocationNotificationTrigger-不在模拟器中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆