我为iPad应用程序保留了Guided Access。当应用程序启动时,它会使用CLLocationManager
询问用户的当前位置。这在正常模式下工作并更新用户当前位置。但在引导访问权限下,popup ("Allow to access your location")
未显示,authorizationStatus
始终为kCLAuthorizationStatusNotDetermined
,并且不会更新用户的当前位置。无法理解可能是什么问题。搜索了很多但无法找到它。
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];
}
任何建议都会有所帮助。谢谢!
答案 0 :(得分:2)
首先设置NSLocationWhenInUseUsageDescription
中的NSLocationAlwaysUsageDescription
或.plist
。
添加CoreLocation.framework
并导入您的class.h文件 - &gt; #import <CoreLocation/CoreLocation.h>
然后将CLLocationManagerDelegate
设置为您的班级。
声明@property (strong, nonatomic) CLLocationManager *locationManager;
初始化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.....
}