1

我正在尝试制作一个应用程序,其中视图将检查位置服务是否已启用。如果未启用,则会弹出提示,但仍会继续搜索位置但不提示。一旦启用位置服务,它将继续其过程。

怎么做???

4

3 回答 3

1

如果位置服务被禁用,您将无法继续获取位置。

如果您想继续搜索位置,请确保通过检查启用该服务

[CLLocationManager locationServicesEnabled]

如果启用,开始更新位置

[locationManager startUpdatingLocation];

然后在

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation {
    [locationManager stopUpdatingLocation]; // This will stop to check the location
}

删除此代码以仍然检查位置[locationManager stopUpdatingLocation];,但这不是最好的方法,请务必阅读apple documentation for the policy of getting the location

于 2012-06-16T10:45:22.287 回答
0

添加 .h 文件 int 计数器;

在你的视图的 viewDidLoad 方法中添加这个,因为它会检查你可以增加计数器的每一秒:

[NSTimer scheduledTimerWithTimeInterval:1.0     
                             target:self
                           selector:@selector(checkLocationMangerEnabled:)     
                           userInfo:nil     
                            repeats:YES];

 counter = 0;

现在选择器将是:

-(void)checkLocationMangerEnabled:(id)sender
{
   if([CLLocationManager locationServicesEnabled])
   {
      //here location is enabled
   }
   else
   { //Not enabled
     if(counter == 60) // alert will showed in every 1 min u can increase or decrese
     {
       UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Location Service Disabled" 
                                                    message:@"To re-enable, please go to Settings and turn on Location Service for this app." 
                                                   delegate:nil 
                                          cancelButtonTitle:@"OK" 
                                          otherButtonTitles:nil];
      [alert show];
      [alert release];
    }
    counter++;
   }
}
于 2012-06-16T09:43:02.513 回答
0

您可以通过此代码检查位置服务的可用性:

MKUserLocation *userLocation = map.userLocation;
    BOOL locationAllowed = [CLLocationManager locationServicesEnabled];
    BOOL locationAvailable = userLocation.location!=nil;

    if (locationAllowed==NO) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Location Service Disabled" 
                                                        message:@"To re-enable, please go to Settings and turn on Location Service for this app." 
                                                       delegate:nil 
                                              cancelButtonTitle:@"OK" 
                                              otherButtonTitles:nil];
        [alert show];
        [alert release];
    } else {
        if (locationAvailable==NO) 
            [self.map.userLocation addObserver:self forKeyPath:@"location" options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld) context:nil];
    }
于 2012-06-16T09:30:07.177 回答