【问题标题】:iOS 8 location service permissioniOS 8 位置服务权限
【发布时间】:2016-09-17 05:17:02
【问题描述】:

如何从非 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初始化为单例

【问题讨论】:

标签: objective-c ios8 cllocationmanager cllocation


【解决方案1】:

将框架添加到您的项目并导入到您的视图控制器:CoreLocation.framework

将此委托添加到您的视图控制器:CLLocationManagerDelegate

在 .plist 文件中添加键值: NSLocationWhenInUseUsageDescription : "Your message"

编辑

+ (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];

这里我没有为

如果此代码仍然不起作用,请尝试以下情况:

  • 从 iPhone 中删除您的应用程序并重新启动您的设备。
  • 更改您的 iPhone 日期和日期不同应大于 24 几个小时后重新启动你的 iPhone。
  • 运行您的应用程序它会请求您的许可。

这背后的主要原因是苹果会要求任何访问用户隐私的权限超过 24 小时。

希望这对你有帮助。

【讨论】:

  • 我正在开发一个想要获取位置详细信息的 SDK。 SDK LocationController 类是 NSObject 类型。我在 SDK 中没有 ViewController
  • 我也在使用 NSObject 类,我只在这个类中完成了所有这些事情。做一件事,创建一个共享实例方法并基于此调用您的位置更新。我正在更新答案,请检查它。
  • 不要忘记添加 delagate 方法来更新位置。 :) 要停止位置更新,您可以创建方法并调用与 startUpdate 方法相同的方法。
  • @armor-- 这个解决方案对你有帮助吗?
  • 它不工作。我不明白您为什么要在 sharedInstance 中初始化位置
【解决方案2】:

将此添加到您的 .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);

}

【讨论】:

  • @armor--...请检查您的 info.plist 一次...并确认您在其中添加 NSLocationWhenInUseUsageDescription 或 NSLocationAlwaysAuthorization..?
  • 我的 SDK plist 文件中有 NSLocationWhenInUseUsageDescription
  • 我没有遇到任何错误。我只是没有收到警报。
  • 检查我的更新答案兄弟.....在你的 .m 文件中添加 #define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) 这个
  • 问题不在于我发布的代码,因为如果我将它直接添加到应用程序中,该代码可以正常工作。
【解决方案3】:

显然在 iOS 8 SDK 中,在开始位置更新之前,需要对 CLLocationManager 调用 requestAlwaysAuthorization(用于后台位置)或 requestWhenInUseAuthorization(仅在前台时定位)。

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];

更多详情请参考此链接----->http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/

【讨论】:

  • 我已经准备好了一切。只是我没有在 UIViewController 类中使用此代码,根据需要保留我添加的所有内容。我根本没有看到警报:(
  • 是的。我会在几分钟内发布代码
  • 我已经更新了代码。我从 ViewController 类调用 LocationController
  • 请在查找位置方法 locationManager = [[CLLocationManager alloc] init]; locationManager.delegate = self; locationManager.distanceFilter = kCLDistanceFilterNone; //每当我们移动 locationManager.desiredAccuracy = kCLLocationAccuracyBest; [locationManager startUpdatingLocation]; [locationManager requestWhenInUseAuthorization]; // 添加这一行
猜你喜欢
  • 2014-11-17
  • 2013-02-15
  • 2015-02-10
  • 2016-06-22
  • 2017-09-20
  • 2018-03-26
  • 1970-01-01
  • 2023-03-29
  • 2017-11-02
相关资源
最近更新 更多