简体   繁体   English

当app在后台时,我可以将用户的当前位置发送到服务器吗?

[英]Can I send user's current location to server, when app is in background?

Can I send user's current location to server when app is in background. 当app在后台时,我可以将用户的当前位置发送到服务器吗? For example: In my app i want to store the user's current location in every 2min and then send his current location to server. 例如:在我的应用程序中,我想每隔2分钟存储用户的当前位置,然后将其当前位置发送到服务器。 Can I do this when the app is in background? 我可以在应用程序处于后台时执行此操作吗? Can I send location updates to the server in background mode and how? 我可以在后台模式下向服务器发送位置更新吗?

You can since iOS7. 你可以从iOS7开始。 You basically need to ask for two background states: location and fetch. 你基本上需要要求两种背景状态:位置和提取。
In your app you'll register for gps location changes, and store them every time your callback is called. 在您的应用程序中,您将注册gps位置更改,并在每次调用回调时存储它们。 Then you'll register for: 然后你会注册:

 application:performFetchWithCompletionHandler:

This is usually used to download files in the background, but you can use it to POST your data to a webserver. 这通常用于在后台下载文件,但您可以使用它将数据发布到Web服务器。 Just note that you don't have control on the frequency of this call. 请注意,您无法控制此通话的频率。
To test this you can force a fetch operation in the iOS simulator, from the Xcode Debug menu. 要对此进行测试,您可以从Xcode Debug菜单强制iOS模拟器中的获取操作。
I was able to track the user position successfully this way. 我能够以这种方式成功跟踪用户位置。 You can find more info in the Apple doc and you'll find a tutorial on the background fetch here 您可以在Apple文档中找到更多信息,您将在此处找到有关后台获取的教程

I wrote an open source project that gets the location from ios devices periodically (and also for android, windows phone and java me cell phones). 我写了一个开源项目,定期从ios设备获取位置(还有android,windows phone和java me手机)。 It runs fine in the background. 它在后台运行良好。 I also have written the server software to store the locations and display the routes with google maps. 我还编写了服务器软件来存储位置并使用谷歌地图显示路线。 The project is complete and working and is MIT licensed. 该项目完整且有效,并获得麻省理工学院的许可。 It does exactly what you are trying to do: 它完全符合您的要求:

https://github.com/nickfox/GpsTracker https://github.com/nickfox/GpsTracker

Make sure you look in the phoneClients directory for ios to see how I get the location periodically. 请务必查看phoneClients目录中的ios,了解我如何定期获取该位置。 The ios client project also uses AFNetworking which easily allows an app to post updates to a webserver from the background using background tasks. ios客户端项目还使用AFNetworking,它可以轻松地允许应用程序使用后台任务从后台向Web服务器发布更新。

Covers it pretty well here: https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/RegionMonitoring/RegionMonitoring.html#//apple_ref/doc/uid/TP40009497-CH9-SW1 在这里介绍它很好: https//developer.apple.com/library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/RegionMonitoring/RegionMonitoring.html#//apple_ref/doc/uid/TP40009497-CH9-SW1

Depending on authorization your app can be woken up when regions are crossed. 根据授权,您可以在跨越地区时唤醒您的应用。 Rather than check every two minutes it is when you cross regions generally as the preferred. 而不是每两分钟检查一次,当你跨越区域时,通常是首选。 That saves on battery as it allows apps to be in sleep most of the time. 这节省了电池,因为它允许应用程序在大多数时间处于睡眠状态。

Also, seems to be spot on: How to wake up an app 此外,似乎是现场: 如何唤醒应用程序

Not my area of development, just googling. 不是我的发展领域,只是谷歌搜索。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 迅速,当应用程序被杀死时将用户的当前位置发送到服务器 - In swift, send User’s current location to server while app is killed 在iOS中后台运行应用程序时将用户位置发送到服务器 - Send user location to server while app in background in ios 当用户拒绝在Google地图中访问位置时,如何显示用户的当前国家/地区? - How can i show user's current country when user denies location access in Google map? 如何在 SwiftUI 中使用用户当前位置调用 API? - How can I call an API with user's current location on app launch in SwiftUI? 即使用户覆盖50米,当应用程序未在ios10的后台运行(暂停)时,如何每隔n秒获取当前的GPS位置? - How can I get current GPS location in every n seconds when app is not running in background (suspended) for ios10 even if user covers 50 meters? 当应用程序在后台时将用户的位置转发到服务器(Swift/iOS) - Forwarding user's location to server when application is in the background (Swift/iOS) 如何确定用户在GPS路径上的当前位置? - How can I determine a user's current location on a GPS path? 无法获取用户的当前位置 - Can't get user's current location 向应用程序发送静默推送通知,更新位置并在后台发送到服务器 - Send silent push notification to app, update location and send to server in background 应用程序终止/暂停时的后台用户位置 - Background user location when app is terminated/suspended
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM