samedi 25 juin 2016

LocationManager crash when app wakes for update


My code to handle app wakes up for location update on didFinishLaunchingWithOptions

if launchOptions?[UIApplicationLaunchOptionsLocationKey] != nil {
                    let locationManager = CLLocationManager()
                    locationManager.delegate = self
                    locationManager.desiredAccuracy = kCLLocationAccuracyBest
                    let status = CLLocationManager.authorizationStatus()
                    if (status == CLAuthorizationStatus.AuthorizedAlways) {
                        locationManager.startMonitoringSignificantLocationChanges()
                    }
                }

Here is the locationmanager delegate on AppDelegate

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

Function to send location info to server

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


    }

Here is the crash log

Crashed: com.apple.main-thread
0  Jemiyet                        0x10015d998 specialized AppDelegate.updateloc(String, long : String) -> () (AppDelegate.swift:354)
1  Jemiyet                        0x10015ddf8 specialized AppDelegate.locationManager(CLLocationManager, didUpdateLocations : [CLLocation]) -> () (AppDelegate.swift:339)
2  Jemiyet                        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 Jemiyet                        0x10015a324 main (AppDelegate.swift:20)

App crashes when app wakes for a location update in startMonitoringSignificantLocationChanges mode

I really can't see any mistake here. Anyone can help me to fix it ?


Aucun commentaire:

Enregistrer un commentaire