检查用户何时从设置app iOS返回



我需要用户的位置在我的应用程序中,如果在情况下,用户有设备位置服务禁用,我检查像这样。

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

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

你可以在applicationWillEnterForeground中管理你的东西,因为这个方法会在你设置完app to your app后被调用。

您可以在这个方法中编写代码,如

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

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

最新更新