ios - Is it possible to fetch GPS location ios8 directly from appDelegate -


i working on app in ios8+ in have fetch location of user. able fetch location when use cllocationmanager in screen (viewcontroller). when try fetch location appdelegate directly, there seems problem , not getting location (the flow not entering delegate methods). can me this.

this code used :

cllocationmanager *locationmanager = [[cllocationmanager alloc] init]; locationmanager.delegate = self; locationmanager.desiredaccuracy = kcllocationaccuracybest; if ([locationmanager respondstoselector:@selector(requestwheninuseauthorization)]) {     [locationmanager requestwheninuseauthorization]; } if ([locationmanager respondstoselector:@selector(requestalwaysauthorization)]) {     [locationmanager requestalwaysauthorization]; } [locationmanager startupdatinglocation]; 

can me this. thanks

  1. add cllocationmanagerdelegate appdelegate.h

@interface appdelegate : nsobject < uiapplicationdelegate, cllocationmanagerdelegate> {

  1. add cllocationmanager object property appdelegate.m

you have implement cllocationmanager object property.

@interface appdelegate () @property (nonatomic, strong) cllocationmanager *locationmanager; @end

  1. replace locationmanager self.locationmanager in appdelegate.m

- (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions { self.locationmanager = [[cllocationmanager alloc] init]; self.locationmanager.delegate = self; self.locationmanager.desiredaccuracy = kcllocationaccuracybest; if ([self.locationmanager respondstoselector:@selector(requestwheninuseauthorization)]) { [self.locationmanager requestwheninuseauthorization]; } if ([self.locationmanager respondstoselector:@selector(requestalwaysauthorization)]) { [self.locationmanager requestalwaysauthorization]; } [self.locationmanager startupdatinglocation];

ref. https://developer.apple.com/library/ios/documentation/corelocation/reference/cllocationmanager_class/

keeping strong reference location manager object required until tasks involving object complete. because location manager tasks run asynchronously, storing location manager in local variable insufficient.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -