Nicoll
Nicoll

Reputation: 257

Cannot get requestWhenInUseAuthorization or requestWhenInUseAuthorization to work

This is my code, it works fine in IOS7 but in IOS8 I can't get authorisation alert to show. I already added the keys in the Info plist for 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];
}
}

Upvotes: 0

Views: 192

Answers (2)

Nicoll
Nicoll

Reputation: 257

This is my 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>

Upvotes: 0

Daniele Candotti
Daniele Candotti

Reputation: 156

For example if you need "WhenInUseAuthorization" in your viewDidLoad :

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






}

Check your status from authStatus

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

And remember to add in your info.plist

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

Upvotes: 1

Related Questions