【问题标题】:Check when user returns from Settings app iOS检查用户何时从 iOS 设置应用程序返回
【发布时间】:2016-09-16 12:36:38
【问题描述】:

如果用户禁用了设备位置服务,我需要在我的应用程序中找到用户的位置,我会像这样检查。

if([CLLocationManager locationServicesEnabled])
{
    if([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
    {
        [locationManager requestWhenInUseAuthorization];
    }
}
else if(![CLLocationManager locationServicesEnabled])
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Location Services are disabled please enable location services to enjoy nearby experience" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Settings", nil];
    alert.tag = 103;
    [alert show];
}

在我的警报视图中,我将用户引导至这样的位置设置

 else if(alertView.tag == 103)
{
    if(buttonIndex == 1)
    {

        NSURL*url=[NSURL URLWithString:@"prefs:root=LOCATION_SERVICES"];
        [[UIApplication sharedApplication] openURL:url];
    }
}

用户从设置返回后如何再次获取位置

【问题讨论】:

    标签: ios objective-c core-location cllocationmanager


    【解决方案1】:

    您可以在applicationWillEnterForeground 中管理您的东西,因为当您来自设置app to your app 时会调用此方法。

    您可以使用这种方法编写代码,例如,

     if([CLLocationManager locationServicesEnabled])
    {
    if([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
    {
        [locationManager requestWhenInUseAuthorization];
    }
    }
    

    【讨论】:

      【解决方案2】:

      使用Location Delegate 方法:-

      if([CLLocationManager locationServicesEnabled])
      {
          if([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
          {
              [locationManager requestWhenInUseAuthorization];
               //Set Location Delegate
              locationManager.delegate = self;
      
              locationManager.distanceFilter = kCLDistanceFilterNone;
              locationManager.desiredAccuracy = kCLLocationAccuracyBest;
               //Update Location start
              [locationManager startUpdatingLocation];
          }
      }
      else if(![CLLocationManager locationServicesEnabled])
      {
          UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Location Services are disabled please enable location services to enjoy nearby experience" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Settings", nil];
          alert.tag = 103;
          [alert show];
      }
      
      #pragma mark CLLocationManager Delegate
      
      - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
      
          //NSLog(@"OldLocation %f %f", oldLocation.coordinate.latitude, oldLocation.coordinate.longitude);
          //NSLog(@"NewLocation %f %f", newLocation.coordinate.latitude, newLocation.coordinate.longitude);
      
           [locationManager stopUpdatingLocation];
      }
      

      【讨论】:

      • 我在用那个函数会不会被调用?
      • 是的,当然,但首先你设置代理并开始更新位置...
      • 是的,但是在我得到位置后,一旦我停下来更新。我正在使用方法 didUpdateToLocations
      • 添加这一行 [locationManager stopUpdatingLocation];
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-07
      • 1970-01-01
      • 2020-11-25
      • 1970-01-01
      • 2013-04-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多