ios - App not asking for permission to access location, popup stays in background -


i'm using mapkit in swift ios app. thing i'm requesting permission access user location when app in use, first time run app in iphone, stays frozen in splash screen, because permission request don't popup, then, if press home button, popup appears ask permission. , if accept then, next run app works properly, shouldn't work this. in code, debugger crashes here because cannot permission:

let initiallocation:cllocation = cllocation(latitude: (locationmanager.location?.coordinate.latitude)!, longitude: (locationmanager.location?.coordinate.longitude)!) 

indicating next issue: thread 1: exc_breakpoint (code=1, subcode=0x1000b5d00)

so, i'm asking permissions in viewwillappear method:

let locationmanager = cllocationmanager()      // ask authorisation user.     // locationmanager.requestalwaysauthorization()      // use in foreground     locationmanager.requestwheninuseauthorization()     //locationmanager.requestalwaysauthorization()      if cllocationmanager.locationservicesenabled() {         locationmanager.delegate = self         //locationmanager.desiredaccuracy = kcllocationaccuracynearesttenmeters         locationmanager.desiredaccuracy = kcllocationaccuracybest         locationmanager.startupdatinglocation()         mapview.showsuserlocation = true     } 

and have entry in info.plist: privacy - location when in use usage description.

why popup not showing in foreground in background?

thanks in advance help.

cheers

edit:

i have splash screen logo before map view. can problem?

edit 2 in answer @dan clark

ok, i've added check in viewdidload below:

edit 3

    let locationmanager = cllocationmanager()     override func viewdidload() {     super.viewdidload()      locationmanager.delegate = self     print("viewdidload")     if cllocationmanager.authorizationstatus() != .authorizedwheninuse    // check authorization location tracking     {         print("requestingautorization")         locationmanager.requestwheninuseauthorization()         print("afterrequestingauthorization")        // locationmanager callbackdidchange... once user responds     } else {         print("startupdatinglocation")         addpins(locationmanager)      }   } 

but popup requesting authorization not appearing :( i've got both prints before , after popup not showing.

i added function wrote me, in same class.

    @nonobjc func locationmanager(manager: cllocationmanager!, didchangeauthorizationstatus status: clauthorizationstatus) {     print("instatuscheck")     switch status     {     case .authorizedwheninuse:         print("statusauthorized")         addpins(manager)      default:         print("statusdefault")         manager.requestwheninuseauthorization()         // user denied access, handle appropriate      } } 

but don't have clear... function called automatically when authorization status changes?

thanks again :)

the problem can take while authorized locationmanager after make request. therefore, on first try don't have authorization before reaching closure after request. i've addressed testing authorization and, if don't have it, putting in request , waiting callback didchangeauthorizationstatus before starting location updates. if have authorization, start location updates.

by second time run app, have authorization delay doesn't occur , you're ok go.

to try approach, include section in viewdidload (i'm assuming don't need run whenever view appears, when first starts):

    if cllocationmanager.authorizationstatus() != .authorizedalways     // check authorization location tracking     {         locationmanager.requestalwaysauthorization()                    // locationmanager callbackdidchange... once user responds     } else {         locationmanager.startupdatinglocation()     } 

and add delegate function class called locationmanager once you're authorized:

// if we've been authorized use location, start processes, otherwise abort operation // since can't proceed without locations  @nonobjc func locationmanager(manager: cllocationmanager!, didchangeauthorizationstatus status: clauthorizationstatus) {     switch status     {     case .authorizedalways:         locationmanager.startupdatinglocation()      default:         // user denied access, handle appropriate      } } 

here's code use instantiate / configure locationmanager:

lazy var locationmanager: cllocationmanager = {     [unowned self] in     var _locationmanager = cllocationmanager()     _locationmanager.delegate = self     _locationmanager.desiredaccuracy = [a user setting in app]     _locationmanager.allowsbackgroundlocationupdates = true     _locationmanager.pauseslocationupdatesautomatically = false  // doesn't shut off if user stops rest     _locationmanager.activitytype = .fitness     _locationmanager.distancefilter = double([a user setting in app])     return _locationmanager     }() 

this has been working me help.


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -