【问题标题】:continously check for location services enabled in ios不断检查 ios 中启用的位置服务
【发布时间】:2012-06-16 09:17:25
【问题描述】:

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

怎么做???

【问题讨论】:

  • 如果您觉得它有用并且能够解决您的问题,请接受它。

标签: iphone objective-c ios5


【解决方案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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 但是这段代码只会检查一次定位服务,之后就不会检查了。如果最初位置服务关闭,然后将其打开,然后用户返回应用程序,则应用程序将不会采取相应行动。
    • 但是你可以在每次你想检查应用程序中的服务时调用这个方法。
    【解决方案3】:

    在.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++;
       }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-15
      • 1970-01-01
      • 1970-01-01
      • 2021-03-07
      • 1970-01-01
      • 2021-12-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多