CLLocation Manager not updated when App is in Guided Access mode

我的未来我决定 提交于 2020-01-25 02:07:57

问题


I kept Guided Access for iPad app. When the app is launched it asks for user's current location using CLLocationManager.This is working under Normal mode and updates user current location. But under Guided Access, popup ("Allow to access your location") is not shown and authorizationStatus is always kCLAuthorizationStatusNotDetermined and doesn't update current location of user . Couldn't understand what could be the problem.Searched a lot but couldn't find it.

ViewController.m :

- (void)viewDidAppear:(BOOL)animated
{
    [appDelegate startLocation];
[self performSelector:@selector(CheckLocationManger) withObject:nil afterDelay:0.1];
}

-(void)CheckLocationManger
{
    AppAppDelegate *appDelegate=(AppAppDelegate*)[[UIApplication sharedApplication]delegate];


    if(![CLLocationManager locationServicesEnabled])
    {
        UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"Whoops we can’t find you!" message:@"Location services are disabled" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        if(activityIndictr)
            [activityIndictr stopAnimating];
        [alert1 show];


        return;
    }
    if([CLLocationManager locationServicesEnabled])
    {
        if([CLLocationManager authorizationStatus]==kCLAuthorizationStatusDenied)
        {

            UIAlertView  *alert1 = [[UIAlertView alloc] initWithTitle:@"Whoops we can’t find you!"message:@"Location services are disabled. You can fix this by going to Settings > Privacy > Location" delegate:nil cancelButtonTitle:@"OK"  otherButtonTitles:nil];
            if(activityIndictr)
                [activityIndictr stopAnimating];
            [alert1 show];
            return;
        }
        if([CLLocationManager authorizationStatus]==kCLAuthorizationStatusNotDetermined) //This is called 
        {

    [self performSelector:@selector(CheckLocationManger) withObject:self afterDelay:0.1];  

            return;
        }

    }

    if(![self connected])
    {

        UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"Network Error" message:@"Please verify that you have internet connectivity" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        if(activityIndictr)
            [activityIndictr stopAnimating];
        [alert1 show];
        [alert1 release];
        return;


    }
    else {
        //continue further process

    }

}


AppDelegate.m

- (void)startLocation
{

    self.locationManager = [[[CLLocationManager alloc] init]autorelease];
    self.locationManager.pausesLocationUpdatesAutomatically=NO;
    [self.locationManager setDelegate:self];
    if([[[UIDevice currentDevice ]systemVersion] floatValue]>=8.0)
        {

          if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
                [self.locationManager requestWhenInUseAuthorization]; //is executed but popup never displays 
            }

        }

        [self.locationManager startUpdatingLocation];

}

Any suggestions would be helpful.Thank you !


回答1:


At very first set NSLocationWhenInUseUsageDescription or NSLocationAlwaysUsageDescription in your .plist.

Add CoreLocation.framework and import in your class.h file -> #import <CoreLocation/CoreLocation.h> then set CLLocationManagerDelegate to your class.

Declare @property (strong, nonatomic) CLLocationManager *locationManager; Init locationManager and set default value to it.

self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.pausesLocationUpdatesAutomatically = NO;
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyBestForNavigation];
[self.locationManager setDistanceFilter:200.0f];
if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)])    // Or if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
    [self.locationManager requestAlwaysAuthorization];                                      // Or [self.locationManager requestWhenInUseAuthorization];
[self.locationManager startUpdatingLocation];

Implement CLLocationMAnager delegate method

#pragma mark - Location Delegate

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    NSLog(@"updated coordinate are %@", [locations lastObject]);
    CLLocation *currentLocation =  [locations lastObject];
    // Your code.....
}


来源:https://stackoverflow.com/questions/30912465/cllocation-manager-not-updated-when-app-is-in-guided-access-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!