swift - Trying to start MapKit location updates without prompting for location authorization -
in app use mkmapkit , mkusertrackingbarbuttonitem locate user on tap. when tap button, output console returns error:
trying start mapkit location updates without prompting location authorization. must call -[cllocationmanager requestwheninuseauthorization] or -[cllocationmanager requestalwaysauthorization] first.
according me, error caused because requestwheninuseauthorization() not invoked yet. in fact, mkusertrackingbarbuttonitem tap calls func mapviewwillstartlocatinguser presupposes cllocationmanager requestwheninuseauthorization first:
func mapviewwillstartlocatinguser(mapview: mkmapview!) { println("**** mapviewwillstartlocatinguser ****") // servizi di localizzazione sono abilitati? if (cllocationmanager.locationservicesenabled()) { // setto il locationmanager ed il delegate locationmanager = cllocationmanager() locationmanager.delegate = self // abbiamo l'autorizzazione ad accedere ai servizi di localizzazione? switch cllocationmanager.authorizationstatus(){ case .denied: // no displayalerttoenablelocationservicesapp() //displayalertwithtitle("denied", message: "location services not allowed app") case .restricted: // no displayalerttoenablelocationservicesapp() case .notdetermined: // bisogna chiedere all'utente println("not determined") if (locationmanager != nil) { locationmanager.requestwheninuseauthorization() } default: // si println("authorized") if (locationmanager != nil) { locationmanager.desiredaccuracy = kcllocationaccuracybest locationmanager.startupdatinglocation() } } } else { println("location services not enabled") displayalertwithtitle("location services turned off", message: "please open settings , turn on location services") } } // funzione della mappa func mapview(mapview: mkmapview!, didfailtolocateuserwitherror error: nserror!) { println("**** didfailtolocateuserwitherror **** ", error) } // funzione della mappa func mapview(mapview: mkmapview!, didchangeusertrackingmode mode: mkusertrackingmode, animated: bool) { println("**** didchangeusertrackingmode ****") } // funzione del corelocation che setta la visuale in base alla localizzaizone dell'utente func locationmanager(manager: cllocationmanager!, didupdatelocations locations: [anyobject]!) { println("**** didupdatelocations ****") //self.mapview.showsuserlocation = true // aggiorno le coordinate dell'utente posizioneutente = (locations[0] cllocation).coordinate //posizioneutente = manager.location.coordinate println("posizione utente aggiornata (lat: \(posizioneutente.latitude) long: \(posizioneutente.longitude))") // setto la camera sulla posizione dell'utente var camera = mkmapcamera(lookingatcentercoordinate: posizioneutente, fromeyecoordinate: posizioneutente, eyealtitude: 500) // utilizzo un'animazione piĆ¹ lenta uiview.animatewithduration(1.8, animations:{ self.mapview.camera = camera }) locationmanager.stopupdatinglocation() // cambio l'icona del bottone della localizzazione //locationoutlet.setimage(uiimage(named: "localizzazioneempty"), forstate: uicontrolstate.normal) } // funzione del corelocation func locationmanager(manager: cllocationmanager!, didfailwitherror error: nserror!) { println("**** didfailwitherror ****") println("error: \(error.localizeddescription)") } // funzione del corelocation func locationmanager(manager: cllocationmanager!, didchangeauthorizationstatus status: clauthorizationstatus) { print("the authorization status of location " + "services changed to: ") switch cllocationmanager.authorizationstatus(){ case .denied: println("denied") case .notdetermined: println("not determined") case .restricted: println("restricted") default: println("authorized") } }
i added naturally info.plist key: nslocationwheninuseusagedescription .
my question is: how can call cllocationmanager requestwheninuseauthorization on mkusertrackingbarbuttonitem tap before launch of mapviewwillstartlocatinguser. want user should prompt when tap button , no in viewdidload
thanks sorry english
when invoke requestwheninuseauthorization
system prompts user permission, when permission not authorized yet. once user gave permission, question not appear again.
to react on user's response have implement locationmanager(_:didchangeauthorizationstatus:)
cllocationmanagerdelegate
protocol , start startupdatelocations()
if user gave permission.
Comments
Post a Comment