1

我保留了 iPad 应用程序的引导访问。当应用程序启动时,它使用 . 询问用户的当前位置CLLocationManager。这在正常模式下工作并更新用户当前位置。但是在 Guided Access 下,popup ("Allow to access your location")没有显示,并且authorizationStatus总是kCLAuthorizationStatusNotDetermined并且不会更新 user 的当前位置。无法理解可能是什么问题。搜索了很多但找不到。

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

}

任何建议都会有所帮助。谢谢!

4

1 回答 1

2

在第一组NSLocationWhenInUseUsageDescriptionNSLocationAlwaysUsageDescription在您的.plist.

CoreLocation.framework在您的 class.h 文件中添加并导入 ->#import <CoreLocation/CoreLocation.h>然后设置CLLocationManagerDelegate为您的类。

声明@property (strong, nonatomic) CLLocationManager *locationManager; Init locationManager 并为其设置默认值。

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

实现CLLocationMAnager委托方法

#pragma mark - Location Delegate

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    NSLog(@"updated coordinate are %@", [locations lastObject]);
    CLLocation *currentLocation =  [locations lastObject];
    // Your code.....
}
于 2015-06-18T10:48:00.937 回答