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
- add
cllocationmanagerdelegate
appdelegate.h
@interface appdelegate : nsobject < uiapplicationdelegate, cllocationmanagerdelegate> {
- add
cllocationmanager
object property appdelegate.m
you have implement cllocationmanager
object property.
@interface appdelegate () @property (nonatomic, strong) cllocationmanager *locationmanager; @end
- 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];
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
Post a Comment