如何从非UI类(iOS 8及更高版本)获取用户访问位置服务的权限?无论我做什么 - requestWhenInUseAuthorization
都不会提醒用户。我使用的是目标c。
我也在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
修改 代码到达[self.locationManager requestWhenInUseAuthorization];但从不显示警报。
此代码是从SDK中的blockOperation调用的。 SDK初始化为singleton
答案 0 :(得分:0)
显然在iOS 8 SDK中,在开始位置更新之前,需要requestAlwaysAuthorization(用于后台位置)或requestWhenInUseAuthorization(仅在前台时的位置)调用CLLocationManager。
Info.plist中还需要有NSLocationAlwaysUsageDescription或NSLocationWhenInUseUsageDescription键,并在提示中显示消息。添加这些解决了我的问题。
尝试这个
#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
//In ViewDidLoad
if(IS_OS_8_OR_LATER) {
[self.locationManager requestAlwaysAuthorization];
}
[self.locationManager startUpdatingLocation];
更多详情请参阅此链接-----&gt; http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/
答案 1 :(得分:0)
在.m文件中添加:
#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
#ifdef __IPHONE_8_0
if(IS_OS_8_OR_LATER) {
// Use one or the other, not both. Depending on what you put in info.plist
[self.locationManager requestWhenInUseAuthorization];
[self.locationManager requestAlwaysAuthorization];
}
#endif
[self.locationManager startUpdatingLocation];
NSLog(@"latitude: %f, longitude: %f",self.locationManager.location.coordinate.latitude,self.locationManager.location.coordinate.longitude);
}
答案 2 :(得分:0)
将框架添加到项目中并导入到viewcontroller:CoreLocation.framework
将此委托添加到viewcontroller:CLLocationManagerDelegate
在.plist文件中添加键值:NSLocationWhenInUseUsageDescription:“您的消息”
修改强>
+ (YourNSObjectClasssName*)sharedInstance {
@synchronized(self) {
if (location == nil) {
location = [[super allocWithZone:NULL] init] ;
}
}
return location;
}
- (id)init
{
self = [super init];
if (self) {}
return self;
}
#pragma mark - Start Location Updates
-(void)startLocationUpdate{
CLLocationManager *locationManager = [[CLLocationManager alloc]init];
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
locationManager.delegate = self;
locationManager.distanceFilter = 10.0f;
if(![CLLocationManager locationServicesEnabled] ||
[CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied){
UIAlertView* failedStatus=[[UIAlertView alloc]initWithTitle:APP_NAME
message:@"Would like to Use your location Please Enable Location Service in Setting"
delegate:self
cancelButtonTitle:@"Setting"
otherButtonTitles:@"Cancel", nil];
[failedStatus show];
}
else if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {
[locationManager requestWhenInUseAuthorization];
}
else{
[locationManager startUpdatingLocation];
[locationManager startMonitoringSignificantLocationChanges];
}
}
这就是你如何从另一个类调用startUpdate函数
[[LocationTracker sharedInstance] startLocationUpdate];
这里我没有为&lt;添加条件iOS 8.0。为此,你可以参考维杰的答案。 (如果用户移动超过10米,则distanceFilter = 10.0正在更新位置。)
如果此代码仍无效,请尝试以下案例:
这背后的主要原因是苹果将要求获得访问用户隐私的任何许可超过24小时。
希望这对你有所帮助。