位置后台服务在Swift2中不起作用 [英] Location background service not working in Swift2

查看:82
本文介绍了位置后台服务在Swift2中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有一些奇怪的行为.

I have some odd behaviour in my app.

在我的应用程序委托文件中,我请求用户在使用应用程序时使用其位置的权限.这个工作正常,我可以获取他们的当前位置.得到他们的初始位置后,我将停止更新位置.伟大的.用户可以点击一个按钮来开始更新位置.

In my app delegate file I ask the user for permission to use their location while they use the app. This works fine and I can get their current location. After I get their initial location I stop updating location. Great. There is a button users can tap to start updating locations.

如果应用程序在前台运行,我可以看到位置每秒都被添加到字典中(稍后我将其更改为distanceFilter 10).如果用户将应用程序放到后台位置,则停止服务,并且我看不到屏幕顶部的蓝色条.

If the app is in the foreground I can see that locations are being added to my dictionary every second (I will change this to distanceFilter 10 later). If the user puts the app in to the background location services stop and I don't see the blue bar at the top of the screen.

我有:

  • 启用了位置更新的背景模式
  • NSLocationWhenInUseUsageDescription设置(我能够成功请求用户许可)
  • 使用startUpdatingLocation()开始位置更新
  • 试图将应用设置为始终跟踪位置(结果相同)
  • 从我的手机中删除该应用,然后重新安装

在我的应用程序的早期版本中(在Swift 2之前),它运行良好.即使在后台运行该应用程序,我仍然可以看到位置信息.

In the previous version of my app (before Swift 2) this was working perfectly. I could see location being tracked even when the app was in the background.

我在iPhone上而不是模拟器上进行测试.

I am testing on my iPhone not the simulator.

推荐答案

如果其他人遇到同样的问题,我已经将iPhone更新到iOS9,因此我需要添加

If anyone else has the same issue, I had updated my iPhone to iOS9 so it was required for me to add

locationManager.allowsBackgroundLocationUpdate = true 

给我的位置经理.

这篇关于位置后台服务在Swift2中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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