簡體   English   中英

iOS 8位置服務權限

[英]iOS 8 location service permission

如何獲得用戶的許可,以從非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初始化為單例

顯然,在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/

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

}

將框架添加到您的項目中,然后導入到您的視圖控制器中: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];

在這里,我沒有為<iOS 8.0添加條件。 為此,您可以參考Vijay的答案。 (如果用戶移動超過10米,則distanceFilter = 10.0正在更新位置。)

如果此代碼仍然不起作用,請嘗試以下情況:

  • 從iPhone刪除應用程序,然后重新啟動設備。
  • 更改您的iPhone日期和不同的日期應超過24小時,然后再次重新啟動iPhone。
  • 運行您的應用程序它會詢問您的許可。

這背后的主要原因是蘋果會要求任何超過24小時的訪問權限來保護用戶的隱私。

希望這對您有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM