ios - Background Location Update Not Working -
i trying implement background fetch location, works perfect inside of ios simulator, when build on phone, not appear work. here current code:
import uikit import corelocation class currentconditonsviewcontroller: uiviewcontroller, cllocationmanagerdelegate { lazy var locationmanager: cllocationmanager! = { let manager = cllocationmanager() manager.desiredaccuracy = kcllocationaccuracyhundredmeters manager.delegate = self manager.requestalwaysauthorization() manager.distancefilter = 2000 if #available(ios 9.0, *) { manager.allowsbackgroundlocationupdates = true } else { // fallback on earlier versions }; return manager }() var seenerror : bool = false var locationfixachieved : bool = false var locationstatus : nsstring = "not started" var userlocation : string! var userlatitude : double! var userlongitude : double! var usertemperaturecelsius : bool! override func viewdidload() { locationmanager.startupdatinglocation(); refresh() } func refresh(){ initlocationmanager() } func initlocationmanager() { seenerror = false locationfixachieved = false locationmanager = cllocationmanager() locationmanager.delegate = self locationmanager.desiredaccuracy = kcllocationaccuracynearesttenmeters locationmanager.requestalwaysauthorization() locationmanager.startupdatinglocation() } func displaylocationinfo(placemark: clplacemark?) { if let containsplacemark = placemark { //stop updating location save battery life locationmanager.stopupdatinglocation() let locality = (containsplacemark.locality != nil) ? containsplacemark.locality : "" let administrativearea = (containsplacemark.administrativearea != nil) ? containsplacemark.administrativearea : "" //println(locality) //println(postalcode) //println(administrativearea) //println(country) } } // mark: - cllocationmanagerdelegate func locationmanager(manager: cllocationmanager, didupdatetolocation newlocation: cllocation, fromlocation oldlocation: cllocation) { // add annotation map. let coords = newlocation.coordinate userlatitude = coords.latitude userlongitude = coords.longitude if uiapplication.sharedapplication().applicationstate == .active { nslog("app in foreground. new location %@", newlocation) clgeocoder().reversegeocodelocation(newlocation, completionhandler: {(placemarks, error) -> void in if error != nil { print("reverse geocoder failed error" + error!.localizeddescription) return } if placemarks!.count > 0 { let pm = placemarks![0] self.displaylocationinfo(pm); } else { print("problem data received geocoder") } }) } else { nslog("app backgrounded. new location %@", newlocation) let coord = newlocation.coordinate let lat = coord.latitude.description; let lng = coord.longitude.description; let localnotification:uilocalnotification = uilocalnotification() localnotification.alertaction = "project rainman" localnotification.alertbody = "location updated: " + lat + ", " + lng localnotification.firedate = nsdate(timeintervalsincenow: 8) uiapplication.sharedapplication().schedulelocalnotification(localnotification) } } func locationmanager(manager: cllocationmanager, didchangeauthorizationstatus status: clauthorizationstatus) { var shouldiallow = false var locationstatus = "" switch status { case clauthorizationstatus.restricted: locationstatus = "restricted access location" case clauthorizationstatus.denied: locationstatus = "user denied access location" case clauthorizationstatus.notdetermined: locationstatus = "status not determined" default: locationstatus = "allowed location access" shouldiallow = true } nsnotificationcenter.defaultcenter().postnotificationname("labelhasbeenupdated", object: nil) if (shouldiallow == true) { nslog("location allowed") // start location services if #available(ios 9.0, *) { locationmanager.allowsbackgroundlocationupdates = true } else { // fallback on earlier versions }; locationmanager.startupdatinglocation() } else { nslog("denied access: \(locationstatus)") } } } // end of class
i not receive notification on actual device, , worth noting morning code updates app when app running. have spent couple days on code , sure it's simple, appreciated!
Comments
Post a Comment