在 iOS (Swift) 中使用后台位置更新的最佳方式 [英] Best way to use background location updates in iOS (Swift)

查看:35
本文介绍了在 iOS (Swift) 中使用后台位置更新的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用 Swift 3 在 iOS 中创建一个应用程序,该应用程序每 3 或 5 分钟通过 API 发送一次用户的位置,同时应用程序处于后台.我不知道哪种是最好的方法,或者由于 Apple 严格的背景规则,这是否可能.位置必须非常精确,应用程序需要在后台运行很长时间,可能不会消耗太多电池.如果不可能,很高兴知道在这种情况下哪种方法是最佳方法.非常感谢.

解决方案

每 3 或 5 分钟通过 API 发送用户的位置,而应用是后台的

意义不大.如果用户站在同一位置 5 分钟会怎样?你会在你的服务器中多次输入相同的位置吗?如果您可以在用户位置更改后使用用户位置更新您的服务器,这不是比每隔一定时间更新位置更好吗?

因此,请使用 locationManager,设置其委托,并在用户位置更改时(而不是定期更改)通过 API 开始更新您的服务器.

<块引用>

我不知道哪种方法最好,或者甚至可能是因为 Apple 严格的背景规则

绝对有可能.您所要做的就是在 Xcode 中选择 Location Updates 功能.

现在最好的方法是什么?它是一个相对术语.这取决于您的应用将如何使用用户位置信息.如果您不必要地开始观察用户位置,您将不必要地耗尽用户 iPhone 的电池电量,因此苹果将拒绝您的应用.

Apple 如何使用位置更新功能处理应用程序?

其实很简单.位置更新功能需要付出代价,如果您的应用在后台准确观察用户位置更新,则会耗尽设备电池.因为这是一个代价高昂的权衡,苹果希望你只在你的应用程序有必要时才使用它.

例如:如果您正在制作地图应用程序或跟踪用户位置变化的应用程序,然后在地图或其他东西上绘制并让用户知道他的移动(如运行应用程序),那么使用位置绝对没问题更新功能,因为您正在为用户增加价值.

如果你试图认为令人毛骨悚然!并尝试使用位置更新只是为了让您的应用程序保持活动状态,并且在后台应用程序中做一些与位置更新完全无关的事情会拒绝您的应用程序.就像很少有开发者试图过于聪明并使用位置更新来保持他们的应用与服务器同步或定期上传/下载文件或类似与位置更新无关的东西一样,苹果会拒绝此类应用.

所以没办法让人毛骨悚然?并使用位置更新来做一些与位置本身无关的有用的事情?

是的,你可以.苹果公司足够慷慨,允许这样做.例如:当您从一个位置移动到另一个位置时,Dropbox 会在后台上传您的图片.它所做的只是查找用户位置更改,一旦位置更改委托触发,创建带有后台会话的上传任务并开始上传文件.

怎么做?

您仍然可以使用位置管理器.你所要做的就是使用

locationManager.startMonitoringSignificantLocationChanges()

这只会在位置发生重大变化时触发您的位置委托 :)

另一方面,如果您的应用实际上利用了用户位置并使用了

startLocationUpdates()

确保您不会不必要地使用位置更新.确保您根据您的应用程序要求正确放置距离过滤器.不要贪婪,不要不必要地浪费iOS资源,苹果会很高兴,不会打扰你:)

结论:

如果您的应用实际上利用了位置信息并为用户增加了一些价值(例如地图应用/正在运行的应用/旅行应用),则仅使用 startLocationUpdates 并正确使用距离过滤器(可选,但最好使用它).

如果您感到毛骨悚然,请始终使用 startMonitoringSignificantLocationChanges 否则应用程序必然会被拒绝

I have to make an app in iOS using Swift 3 that sends the user's location via API every 3 or 5 minutes, while the app is backgrounded. I don't know which is the best way to do this, or if it's even possible because of Apple's strict background rules. The location has to be quite precise and the app needs to run for a long time backgrounded, possibly not consuming too much battery. If it isn't possible, it would be nice to know which would be the best approach in this case. Thank you very much.

解决方案

that sends the user's location via API every 3 or 5 minutes, while the app is backgrounded

Does not make much sense. What if user stands in same location for 5 minutes? you will make same location entry in your server multiple times? Isn't it better rather than updating location at certain interval, if you could update your server with user location once user's location changes??

So use locationManager, set its delegates and start updating your server via API when user location changes rather than at regular interval.

I don't know which is the best way to do this, or if it's even possible because of Apple's strict background rules

Its absolutely possible. All you have to do is to opt for Location Updates capability in Xcode.

Now whats the best way? Its a relative term. This depends on how your app will use the users location info. If you unnecessarily start observing users location, you will unnecessarily drain the users iPhone battery, hence apple will reject your app.

How apple process's app using location update capability?

Its pretty simple. Location updates capability comes with the cost, that if your app observes user location updates accurately in background, it will drain out the device battery. Because its a costly trade off, apple expects you to use this only if its necessary for your app.

For example : If you are making a map app or an app that tracks the users location changes and then later plots on a map or something and lets the user know about his movement (like running apps) its absolutely fine to use location update capability because you are adding value to user.

If you try to think creepy! and try to use the location update just to keep your app alive and do some thing completely unrelated to location update in background app will reject your app. Like few developers try to be over smart and use location updates to keep their app in sync with server or to upload/download files at regular interval or something like that which are no way related to location updates, apple will reject such apps.

So no way to be creepy? And use location updates to do something useful which is not related to location itself?

Yes, you can. Apple is generous enough to allow that. For example : Dropbox uploads your images in background, when you move from one location to another. All it does is, it looks for user location changes, once location changes delegates triggers, creates a upload task with background session and starts uploading files.

How to do that?

You can still use the location manager. All you have to do is to use

locationManager.startMonitoringSignificantLocationChanges()

This will trigger your location delegates only when there is a significant location changes :)

On the other hand, if your app actually makes use of user locations and use

startLocationUpdates()

make sure you dont consume location updates unnecessarily. Make sure u put distance filter properly according to your apps requirements. Dont be greedy and dont waste iOS resources unnecessarily and apple will be happy and will not trouble you :)

Conclusion:

If your app actually makes use of location and adds some value to user (like map app/running apps/travel apps) only then use startLocationUpdates and use distance filters properly (optional, but good to have it).

If you are being creepy always use startMonitoringSignificantLocationChanges else app is bound to get rejected

这篇关于在 iOS (Swift) 中使用后台位置更新的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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