简体   繁体   English

CLLocation在IOS中停止

[英]CLLocation stops in IOS

I'm trying to send my position with iOS and swift, but after a while the application is stopped and stops working. 我正在尝试通过iOS和Swift发送我的职位,但过了一会儿该应用程序停止运行并停止工作。

    class Location:NSObject, CLLocationManagerDelegate {

private var locationManager = CLLocationManager()
private var errorLocation: Bool = false
private var stuffLocation: CLLocation!

var outSocket: OutSocket!
var timer: NSTimer!
var stuff: Stuff!

override init() {
    super.init()
    outSocket = OutSocket()
    setupLocation()
}

func setupLocation() {
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestWhenInUseAuthorization()
    locationManager.activityType = CLActivityType.Fitness
    locationManager.allowsBackgroundLocationUpdates = true
    locationManager.startUpdatingLocation()
}

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    if !errorLocation {
        errorLocation = true
        locationManager.allowDeferredLocationUpdatesUntilTraveled(10, timeout: 50)
        stuffLocation = locations[0]
        if timer == nil {
            timer = NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: #selector(Location.sendDataToServer), userInfo: nil, repeats: true)
        }
    }
}
}
func locationManager(manager: CLLocationManager, didFinishDeferredUpdatesWithError error: NSError?) {
    errorLocation = false
}

This is my code, if anyone has done something similar or know how to prevent the APP stops will appreciate it. 这是我的代码,如果有人做过类似的事情或知道如何防止APP停止将不胜感激。

If you want your app to keep getting GPS location updates while it is in the background you have to add an entry to the UIBackgroundModes array in the app's info.plist, and then you need to ask the user for permission. 如果希望您的应用在后台运行时继续获取GPS位置更新,则必须在应用的info.plist中的UIBackgroundModes数组中添加一个条目,然后您需要征求用户的许可。 Do a search in the Xcode docs on UIBackgroundModes . UIBackgroundModes的Xcode文档中进行UIBackgroundModes Specifically look at the location value. 具体看一下location值。

You will also need to make a case to Apple that you app should be allowed to use the GPS from the background when you submit the app to the app store. 您还需要向Apple证明,当您将应用程序提交到应用商店时,应允许您的应用从后台使用GPS。

Note that you are going to drain the user's battery in a hurry if you keep the GPS on at all time, AND a TCP socket in order to upload the coordinates. 请注意,如果您始终保持GPS处于打开状态,并且要使用TCP套接字以上传坐标,则将急于消耗用户的电池。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM