应用程序唤醒更新

时间:2016-06-25 16:11:46

标签: ios swift crash cllocationmanager

虽然应用程序处于活动状态时工作正常但是当应用程序终止并因位置更新而唤醒时会崩溃

我处理应用的代码唤醒了didFinishLaunchingWithOptions

上的位置更新
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate {

    let locationManager = CLLocationManager()
    var glat : String = ""
    var glong : String = ""

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

         if launchOptions?[UIApplicationLaunchOptionsLocationKey] != nil {
  let locationManager = CLLocationManager() //or without this line, both crashes
                    locationManager.delegate = self
                    locationManager.desiredAccuracy = kCLLocationAccuracyBest
                    let status = CLLocationManager.authorizationStatus()
                    if (status == CLAuthorizationStatus.AuthorizedAlways) {
                        locationManager.startMonitoringSignificantLocationChanges()
                    }
                }
return true
    }

以下是AppDelegate上的locationmanager委托

 func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

            if  let lat = manager.location?.coordinate.latitude,
                let long = manager.location?.coordinate.longitude {
                print(glat + " " + glong)

                glat = String(lat)
                glong = String(long)

                //Line 339
                updateloc(String(lat), long: String(long))
            }
    }

将位置信息发送到服务器的功能

func updateloc(lat : String, long : String) {

        let session = NSURLSession.sharedSession()

        //Line 354
        let request = NSMutableURLRequest(URL: NSURL(string: "URLTO/updateloc.php")!)
        request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
        request.HTTPMethod = "POST"
        let data = "lat=\(lat)&long=\(long)"
        request.HTTPBody = data.dataUsingEncoding(NSASCIIStringEncoding)

        let task = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in
            if let error = error {
                print(error)
            }
            if let response = response {

                let res = response as! NSHTTPURLResponse
                dispatch_async(dispatch_get_main_queue(), {
                    if (res.statusCode >= 200 && res.statusCode < 300)
                    {
                        do{
                            let resultJSON = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions())

                            var success = 0


                            if let dictJSON = resultJSON as? [String:AnyObject] {
                                if let successInteger = dictJSON["success"] as? Int {
                                    success = successInteger

                                    if success == 1
                                    {
                                    print("ok")
                                    }


                                } else {
                                    print("no 'success' key in the dictionary, or 'success' was not compatible with Int")
                                }
                            } else {
                                print("unknown JSON problem")
                            }


                        } catch _{
                            print("Received not-well-formatted JSON")
                        }

                    }
                })
            }
        })
        task.resume()


    }

这是崩溃日志

Crashed: com.apple.main-thread
0                          0x10015d998 specialized AppDelegate.updateloc(String, long : String) -> () (AppDelegate.swift:354)
1                          0x10015ddf8 specialized AppDelegate.locationManager(CLLocationManager, didUpdateLocations : [CLLocation]) -> () (AppDelegate.swift:339)
2                          0x100159d0c @objc AppDelegate.locationManager(CLLocationManager, didUpdateLocations : [CLLocation]) -> () (AppDelegate.swift)
3  CoreLocation                   0x1893d08b8 (null) + 21836
4  CoreLocation                   0x1893ccaac (null) + 5952
5  CoreLocation                   0x1893c6e48 (null) + 880
6  CoreFoundation                 0x18262cf84 __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 20
7  CoreFoundation                 0x18262c8bc __CFRunLoopDoBlocks + 308
8  CoreFoundation                 0x18262ad04 __CFRunLoopRun + 1960
9  CoreFoundation                 0x182554c50 CFRunLoopRunSpecific + 384
10 GraphicsServices               0x183e3c088 GSEventRunModal + 180
11 UIKit                          0x18783e088 UIApplicationMain + 204
12                         0x10015a324 main (AppDelegate.swift:20)

应用程序在startMonitoringSignificantLocationChanges模式

中唤醒位置更新时应用程序崩溃

我真的不能在这里看到任何错误。有人可以帮我解决吗?

3 个答案:

答案 0 :(得分:2)

作为一个起点,我建议将您的位置管理器功能移动到一个单独的类,并让您的位置类订阅UIApplicationDidFinishLaunchingNotification通知,以处理重新启动应用程序时发生的情况。

执行此操作的方法是让您的类成为Singleton并让它通过NSNotificationCenter中继位置更新。

startMonitoringSignificantLocationChanges会在应用程序被操作系统终止时启用,在后台唤醒您的应用程序。

根据您想要实现的目标,您可以订阅两个不同的事件,并根据应用委托事件开始停止相关的位置服务。

作为一个广泛的(在黑暗中拍摄)的例子:

class LocationCommander {

    let locationManager = CLLocationManager()
    let defaultCenter = NSNotificationCenter.defaultCenter()
    //- NSUserDefaults - LocationServicesControl_KEY to be set to TRUE when user has enabled location services.
    let UserDefaults = NSUserDefaults.standardUserDefaults()
    let LocationServicesControl_KEY = "LocationServices"

    init(){
        defaultCenter.addObserver(self, selector: #selector(self.appWillTerminate), name: UIApplicationWillTerminateNotification, object: nil)
        defaultCenter.addObserver(self, selector: #selector(self.appIsRelaunched), name: UIApplicationDidFinishLaunchingNotification, object: nil)
    }

    func appIsRelaunched (notification: NSNotification) {

        //- Stops Significant Location Changes services when app is relaunched
        self.locationManager.stopMonitoringSignificantLocationChanges()

        let ServicesEnabled = self.UserDefaults.boolForKey(self.LocationServicesControl_KEY)

        //- Re-Starts Standard Location Services if they have been enabled by the user
        if (ServicesEnabled) {
            self.updateLocation()
        }
    }

    func appWillTerminate (notification: NSNotification){

        let ServicesEnabled = self.UserDefaults.boolForKey(self.LocationServicesControl_KEY)

        //- Stops Standard Location Services if they have been enabled
        if ServicesEnabled {

            self.locationManager.stopUpdatingLocation()

            //- Start Significant Location Changes to restart the app
            self.locationManager.startMonitoringSignificantLocationChanges()
        }

        NSUserDefaults.standardUserDefaults().synchronize()
    }

    func updateLocation () {

        if (CLLocationManager.authorizationStatus() == CLAuthorizationStatus.Authorized){

            self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
            self.locationManager.distanceFilter = kCLDistanceFilterNone

            self.locationManager.startUpdatingLocation()

            //- Save Location Services ENABLED to NSUserDefaults
            self.UserDefaults.setBool(true, forKey: self.LocationServicesControl_KEY)
        } else {
            //- Unauthorized, requests permissions
        }
    }
}

请记住,此代码尚未经过测试,我已经提取了一个旧项目的片段,由于语言更新,可能会出现一些语法错误。

总的来说,我们这样做:

  • 订阅相关通知
  • 当应用程序即将终止时,请检查我们是否正在获取位置更新,如果有,请停止标准服务并启动重要更改服务以在新更新可用时唤醒应用程序。
  • 当应用程序唤醒时,它会通知班级,该班级将决定是否继续跟踪通知更新(基于存储在NSUserDefaults上的值)。

由于实例化新的位置管理器,然后尝试重新启动可能已在运行的位置服务,您的应用可能会崩溃。

我希望这有帮助!

答案 1 :(得分:2)

文档说

  

对于重新启动应用程序的服务,系统会将UIApplicationLaunchOptionsLocationKey键添加到在启动时传递给应用程序委托的选项字典中。如果存在此密钥,您应立即重新启动应用程序的位置服务。选项字典不包含有关位置事件本身的信息。您必须配置新的位置管理器对象,并再次委派和启动您的位置服务以接收任何待处理事件。

此外它说:

  

连续几次调用此方法[意味着startMonitoringSignificantLocationChanges]并不会自动导致生成新事件。但是,在两者之间调用stopMonitoringSignificantLocationChanges会导致在下次调用此方法时发送新的初始事件。

这意味着乍一看可能更清楚。首先,位置更新触发了您的应用重新启动这一事实并不意味着您可以通过代理的locationManager:didUpdateLocations:方法立即获得该位置。其次,如果您处于后台(因此没有重新启动,但只是变得活跃!),的内容再次不同。事实上,我遇到的一个常见问题是他们不知道他们是在后台(不活动)还是终止/只是重新启动。你甚至无法轻易地测试/看到这一点,因为当按下主页按钮两次时,系统终止的应用程序仍会显示。检查XCode Attach to Process...菜单中Debug下显示的列表,看看您的应用是否仍在运行(并且在后台)(顺便说一下,通过XCode结果中的停止按钮终止应用)在这种不一致的情况下,如果你想玩各种方式,你可以变得活跃或重新启动)。我只是在这里提到这一点,因为很多问题都在询问&#34;当我的应用再次激活时,X会怎样?&#34;并且回答的人假设的东西不同于提问者的意思。

无论如何,如果您的应用实际上已经重新启动,那么您的locationManager变量现在包含一个全新的实例,您应该在致电startMonitoringSignificantLocationChanges后立即获得更新。我将解决您在下面可能会遇到的问题。 如果你的应用程序刚刚再次激活,你应该再次呼叫stopMonitoringSignificantLocationChanges,然后再呼叫startMonitoringSignificantLocationChanges以立即获得更新(简而言之:始终停止然后开始正常重新启动,即使是,它也不会受到伤害你还没有跑步。

现在我认为这个问题会导致苹果公司在位置更新后重新启动的方式(部分)。我不完全确定在没有玩你的代码的情况下发生了什么,但我希望这有一些帮助: 在application:didFinishLaunchingWithOptions:方法中,您可以使用let locationManager = CLLocationManager()设置本地常量。 然后重新启动此,并触发位置更新。但是,在调用委托的locationManager:didUpdateLocations:方法之前,可能被取消分配,毕竟,回调是异步发生的(并且application:didFinishLaunchingWithOptions:可能在调用之前返回) 。我不知道这个甚至是如何或为什么会起作用并且尽可能地说明它的运气,或者它与CLLocationManager内部的工作方式有关。无论如何,我认为然后发生的是locationManager:didUpdateLocations:,你不再获得有意义的位置数据,因为负责该调用的CLLocationManager实例无效了。 latlong为空或甚至为零,您的请求的网址(您尚未显示)无效。

我建议先:

  1. 确保您的班级常量locationManager已在application:didFinishLaunchingWithOptions:中正确设置(在您检查UIApplicationLaunchOptionsLocationKey标志的存在之后)。摆脱同名的本地常量。
  2. 停止然后重新启动位置更新。这应该立即使用最新的位置数据(触发您的应用重新启动或再次激活的位置数据)调用您的代理locationManager:didUpdateLocations:方法。
  3. 确保您在locationManager:didUpdateLocations:方法中获得有意义的数据。如果情况不是这样,请不要致电updateloc(在将类的变量设置为新值后,您可能还想将行print(glat + " " + glong)移至{};}所以你在控制台上看到你得到的东西是否有意义。)
  4. 我能告诉你的一件小事:如果你的应用程序刚刚重新启动(而不是重新启动),我不确定是否{{1甚至都设置了。你可能也想调查那个。如果变得活跃则不会重新启动&#34;但是,从这个意义上讲,你的UIApplicationLaunchOptionsLocationKey仍然应该感到高兴,至少这是我从文档中得到的东西(自从我尝试过以来已经有一段时间了)我自己)。让我/我们知道,如果您仍然遇到问题,我很好奇它是如何结果的。 :)

答案 2 :(得分:1)

试试这个

dispatch_async(dispatch_get_global_queue(Int(QOS_CLASS_DEFAULT.rawValue), 0)) {
          updateloc(String(lat), long: String(long))   
}
相关问题