IOS - 使用swift运行更新用户位置的后台任务 [英] IOS - Running background task for update user location using swift

查看:86
本文介绍了IOS - 使用swift运行更新用户位置的后台任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是这里的新用户,但我已经多次使用stackoverflow:)...我是android开发人员,但现在我正在使用swift在ios应用程序中工作。我是全新的使用xcode,也许我的问题非常简单很多......很棒,我真的需要帮助。
我正在尝试实施一个每十分钟更新一次用户位置的背景任务。我需要获取lat和long数据,然后将此信息发送到API Web服务。我一直在讨论背景文档,所有文档以及有关用户位置服务。有很多信息,相当的信息,但是,也许我非常想知道android信息,也有很多信息,但是很多简单的例子对我来说也很容易。和之前的stackoverflow问题可能会有很大帮助。

i'm new user here, but I have used stackoverflow a lot of times :)... I'm android developer, but now i'm working in an ios app using swift. I'm totally new using xcode, maybe my question es very simple for a lot... Great, I really need help. I'm trying to implement a backgroud task that updates the user location each ten minutes. I need to get the lat and long data and after send this info to an API web service. I have been rading the backgroud docs, all documentation, and all the documentation about user location services. There is a lot of information, pretty information, but, maybe i'm very wonted to the android information, with s lot of information too, but a lot of easy examples also, then, is very easy for me. and an previous stackoverflow questionmaybe helpme a lot.

我理解了很多概念,并且我按照文档说的那样配置了我的info.plist,但是......如何运行后台任务来获取用户位置?我必须在哪里编写后台任务代码?什么是开始?我有一个项目有三个不同的viewcontrollers,我需要用一个按钮启动服务...非常感谢你,我真的需要了解所有这一切的起点。

I understand a lot of concepts, and I cofigured my info.plist like the documentation says, but... how can I run a background task for get the user location? Where I must write the background task code? What is the beginning? I have a project with three diferent viewcontrollers and I need to start the service with a button... Thank you so much, I really need to understand the start point of all this.

推荐答案

我目前正在做与我的第一个iOS项目相同的事情 - 帮助我的是:
定期iOS背景位置更新

I am currently doing kind of the same thing as my first iOS-project - what helped me was this : Periodic iOS background location updates

从那时起,我发现了一个Swift- https://github.com/voyage11/Location 的端口: https://github.com/silento/Location 。在voyage11的项目中,还有一个博客文章链接了一些澄清正在发生的事情,并链接到第二个,告诉如何使用重要的位置更改重新启动位置服务。我目前正试图通过区域位置监控来实现这一点,但不确定它是否可行。

And from there on I found a Swift-Port of https://github.com/voyage11/Location: https://github.com/silento/Location. In the project from voyage11, there is also linked a blog post that a bit of clarifies what is happening and that also links to a second one that tells how to restart location services using "significant location changes". I am currently trying to implement this with "region location monitoring", but not sure if it will work.

哦,我将触发新后台任务的代码从didUpdateLocation-part移到了restartLocationUpdates-part--这样你就可以确保你的任务重启了如果你没有得到一个位置。

Oh, and I moved the code that triggers a new background task out of the didUpdateLocation-part to the restartLocationUpdates-part - so you can be sure your task is restarted even if you do not get a location in time.

它从一开始就不起作用,但当我从 https://stackoverflow.com/a/19085518/3085985 作为stopUpdatingLocation()和startUpdatingLocation() - 部分的替代品。

It did not work from the start, but when I added the code from https://stackoverflow.com/a/19085518/3085985 as a replacement for the stopUpdatingLocation() and startUpdatingLocation() - parts.

它实际上比应该更复杂,我在Android中使用的是什么 - 但事实就是如此。如果您正在为Iphone5 +开发,您也可以考虑这篇文章(我现在无法测试它,因为我只有iPhone 4S并且模拟器不支持该功能):https://stackoverflow.com/a/24666487/3085985

It is really more complicated than it should be and what I am used from Android - but it is how it is. If you are developing for Iphone5+ you can also consider this post (I could not test it right now as I only have an iPhone 4S and the feature is not supported in the Simulator): https://stackoverflow.com/a/24666487/3085985.

我希望你有一些起点,我我目前仍在测试我的解决方案,但它似乎现在我可以看到;)

I hope you got some starting points, I am currently still testing my solution but it seems to work as far as I can see now ;)

PS:我在某处读到有关睡眠时间超过5分钟的后台任务被取消 - 因为我只等了30秒我没有这个问题,但你可能有。但是,保持位置服务运行的距离非常远,可以防止这种情况发生。

P.S.: I somewhere read something about background tasks sleeping for more than 5 minutes being canceled - as I only wait 30 seconds I do not have this issue, but you might have. But maybe keeping location services running with a really high distance prevents this from happening.

P.P.S。:由于Swift是新的,你应该习惯阅读Objective C-Code并尝试将其转换为Swift。它通常是找到好教程的唯一方法。

P.P.S.: As Swift is new you should get used to reading Objective C-Code and trying to translate it to Swift. It often is the only way to find good tutorials.

还有一件事 - 对于iOS8,你需要这样的东西:

One more thing - for iOS8 you need something like :

if (self.locationManager.respondsToSelector(Selector("requestWhenInUseAuthorization"))) {
            // on iOS8+, we need to request the location authorization ourselves -.-

            self.locationManager.requestAlwaysAuthorization()
        }

并且不要忘记将NSLocationAlwaysUsageDescription添加到您的信息.plist以及允许项目功能中的后台位置更新。
很抱歉这一堆文字,但这是一个非常复杂的话题;)

And don't forget to add the "NSLocationAlwaysUsageDescription" to your Info.plist as well as allowing background location updates in your project capabilities. Sorry for this mess of text, but it is a really complicated topic ;)

编辑:关于你的第一个评论:
你应该创建一个Swift项目,例如在一个窗口和主要。故事板添加一个开关。然后,您应该实现启用或禁用侦听器的ToggleActionChanged事件,具体取决于交换机的新状态。我无法解释所有的基础知识,你应该看一个基本的教程,我喜欢 https:// www。 youtube.com/playlist?list=PL_4rJ_acBNMHa_RGZigG2nQI3aL1kTa4r 并特别使用第7个视频作为如何构建基本应用程序的示例。唯一可以比在那里显示更容易的是使用从故事板到代码视图的右键单击拖动而不是手动实现某些方法。有一个方便的助手。

edit: Regarding your first comment : You should create a Swift project, e.g. with a single window and in Main. storyboard add a switch. Then you should implement a ToggleActionChanged-event enabling or disabling the listeners, depending on the new state of the switch. I cannot explain all the basics, you should watch a basic tutorial, I like https://www.youtube.com/playlist?list=PL_4rJ_acBNMHa_RGZigG2nQI3aL1kTa4r and used especially the 7th video as an example of how to build a basic app. The only thing that can be made easier than shown there is using right-click-dragging from the storyboard to the code view instead of implementing some methods by hand. There is a handy assistant.

对于您的位置事件监听器,您可以添加一个新的swift类并命名它,例如LocationDelegate.swift。它使它更干净。我使用Singleton模式,你可以查找如何做到这一点。或者你可以使用我上面发布的github项目的结构。我无法提供完整的来源,因为您需要更改它以满足您的个人需求;)

For your location event listeners you can add a new swift class and name it e.g. LocationDelegate.swift . It makes it a bit more clean. I use a Singleton-pattern for that, you can look up how to do this. Or you could use the structure of the github-project I posted above. I can't provide the complete source as you need to change it so it fits your individual needs ;)

哦,还有一件事:要记住你的开关是否是在应用程序启动之间启用,您可以使用以下内容:

Oh, and one more thing : To remember if your switch is enabled between application launches, you can use something like this :

   @IBAction func toggleTrackingChanged(sender: UISwitch) {


      // This is your method for when the switch changed... put your location activation/deactivation here



      let userDefaults = NSUserDefaults.standardUserDefaults()

      userDefaults.setBool(sender.on, forKey: "ODLTrackingActive")
    }

 }

并在你的viewDidLoad-Method中:

and in your viewDidLoad-Method :

let userDefaults = NSUserDefaults.standardUserDefaults()

    var trackingActive = userDefaults.boolForKey("ODLTrackingActive")

    toggleTrackingSwitch.setOn(trackingActive, animated: false)

    // reactivate location tracking if necessary

祝你好运!

这篇关于IOS - 使用swift运行更新用户位置的后台任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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