无法获取请求时,请在使用权限或请求时使用权限进行工作

时间:2014-11-29 16:57:56

标签: xcode6

这是我的代码,它在IOS7中工作正常但在IOS8中我无法获得授权警报。我已经在Info plist中为NSLocationWhenInUseUsageDescription&添加了密钥。 NSLocationAlwaysUsageDescription。

·H

#import <CoreLocation/CoreLocation.h>
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UILabel *LatitudeLabel;
@property (weak, nonatomic) IBOutlet UILabel *LongitudeLabel;
@property (weak, nonatomic) IBOutlet UILabel *GPSAccuracyLabel;
@property (weak, nonatomic) IBOutlet UILabel *AltitudeLabel;
@property (weak, nonatomic) IBOutlet UILabel *VerticalAccuracyLabel;

- (IBAction)getCurrentLocation:(id)sender;

@end

的.m

#import "ViewController.h"

@interface ViewController ()

// create a property to hold current time.
@property (nonatomic, strong) NSDate *lastUpdateTime;
@end

@implementation ViewController {
CLLocationManager *locationManager;

}

- (void)viewDidLoad {
[super viewDidLoad];

self.lastUpdateTime = [NSDate date];

locationManager = [[CLLocationManager alloc] init];

CLAuthorizationStatus authorizationStatus= [CLLocationManager authorizationStatus];

if (authorizationStatus == kCLAuthorizationStatusAuthorizedAlways ||
    authorizationStatus == kCLAuthorizationStatusAuthorizedWhenInUse) {

    [locationManager startUpdatingLocation];
}
}

2 个答案:

答案 0 :(得分:1)

例如,如果您在viewDidLoad中需要“WhenInUseAuthorization”:

-(void)viewDidLoad {

[super viewDidLoad];

self.lastUpdateTime = [NSDate date];

locationManager = [[CLLocationManager alloc] init];

CLAuthorizationStatus authorizationStatus= [CLLocationManager authorizationStatus];

NSLog(@"authStatus = %d",authorizationStatus);

if([locationManger respondsToSelector:@selector(requestWhenInUseAuthorization)])

  [locationManger requestWhenInUseAuthorization];

  [locationManager startUpdatingLocation];






}

从authStatus检查您的状态

typedef enum {
    kCLAuthorizationStatusNotDetermined  = 0,
    kCLAuthorizationStatusRestricted ,
    kCLAuthorizationStatusDenied ,
    kCLAuthorizationStatusAuthorized ,
    kCLAuthorizationStatusAuthorizedAlways  = kCLAuthorizationStatusAuthorized ,
    kCLAuthorizationStatusAuthorizedWhenInUse
} CLAuthorizationStatus;

请记得在info.plist中添加

<key>NSLocationWhenInUseUsageDescription</key>
<string>Your message</string>

答案 1 :(得分:0)

这是我的Info.Plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>com.nicoll55.$(PRODUCT_NAME:rfc1034identifier)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>BNDL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>Required to update your location in the background.</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Required to obtain your location.</string>
<key>UIApplicationExitsOnSuspend</key>
<false/>
</dict>
</plist>