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

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

问题描述

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

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.

推荐答案


每3或5分钟通过API发送用户的位置,而
应用程序是背景的

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

没有多大意义。如果用户站在同一位置5分钟怎么办?您将多次在服务器中输入相同的位置?如果您可以在用户位置发生变化后使用用户位置更新您的服务器,那么不是更好而不是更新位置吗?

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??

所以使用locationManager,设置其代理并在用户位置更改时开始通过API更新服务器,而不是定期更新。

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


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

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

绝对可能。您所要做的就是在Xcode中选择位置更新功能。

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

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

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.

Apple进程的应用如何使用位置更新功能?

非常简单。位置更新功能带有成本,如果您的应用程序在后台准确地观察用户位置更新,则会耗尽设备电池。因为它是一个代价高昂的折衷,苹果希望你只有在你的应用程序必要时才使用它。

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.

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

是的,你可以。 Apple慷慨地允许这样做。例如:当您从一个位置移动到另一个位置时,Dropbox会在后台上传您的图像。所有这一切都是,它会查找用户位置更改,一旦位置更改委托触发器,创建带后台会话的上传任务并开始上传文件。

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.

如何那样做?

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

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()

确保不必要地消耗位置更新。确保根据应用程序要求正确放置距离过滤器。不要贪婪,不要浪费不必要的iOS资源,苹果会很高兴,不会给你带来麻烦:)

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 :)

结论:

如果你的应用实际上利用了位置并为用户增加了一些价值(比如地图应用/正在运行的应用/旅行应用),那么只需使用startLocationUpdates并正确使用距离过滤器(可选,但很好拥有它。)

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).

如果你感到毛骨悚然总是使用startMonitoringSignificantLocationChanges其他应用必然会被拒绝

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

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

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