Comment puis-je obtenir l'autorisation de l'utilisateur pour accéder au service de localisation à partir d'une classe non UI (iOS 8 et supérieur)? Peu importe ce que je fais - requestWhenInUseAuthorization
n'alerte pas l'utilisateur. J'utilise l'objectif c.Autorisation de service de localisation iOS 8
J'ai également ajouté la clé dans le fichier plist.
LocationController.h
@interface LocationController : NSObject <CLLocationManagerDelegate>{
BOOL _debug;
}
@property (strong, nonatomic) CLLocationManager *locationManager;
@property (strong, nonatomic) CLLocation *location;
- (id)initWithDebug:(BOOL)debug_flag;
@end
LocationController.m
@implementation LocationController {
}
@synthesize debug, locationManager, location;
#define IOS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
- (id)initWithDebug:(BOOL)debug_flag {
self = [super init];
if (self) {
[self findLocation];
}
return self;
}
-(void) findLocation{
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
[self checkLocationPermissions];
self.location = self.locationManager.location;
}
-(void)checkLocationPermissions {
if ([CLLocationManager locationServicesEnabled]) {
if (IOS_8_OR_LATER) {
[self.locationManager requestWhenInUseAuthorization];
} else {
[self.locationManager startUpdatingLocation];
}
}
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager*)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
switch (status) {
case kCLAuthorizationStatusNotDetermined: {
[self.locationManager startUpdatingLocation];
} break;
case kCLAuthorizationStatusDenied: {
[self.locationManager stopUpdatingLocation];
} break;
case kCLAuthorizationStatusAuthorizedWhenInUse:
case kCLAuthorizationStatusAuthorizedAlways: {
[self.locationManager startUpdatingLocation];
} break;
default:
break;
}
}
@end
EDIT: Le code atteint jusqu'à [self.locationManager requestWhenInUseAuthorization]; mais ne montre jamais l'alerte.
Ce code est appelé à partir d'une opération blockOperation dans un SDK. Le SDK est initialisé en tant que singleton
Consultez cette réponse mai il aider: [http://stackoverflow.com/a/24063578/5575752]http://stackoverflow.com/a/24063578/5575752) –
Merci @ RonakChaniyara pour l'aide. J'ai regardé ça avant. Je devine que mon problème est dû à une classe de non-interface utilisateur que j'emploie pour obtenir des emplacements –
http://nshipster.com/core-location-in-ios-8/ – Sanju