检查用户何时从 iOS 设置应用程序返回
Posted
技术标签:
【中文标题】检查用户何时从 iOS 设置应用程序返回【英文标题】:Check when user returns from Settings app 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];
用户从设置返回后如何再次获取位置
【问题讨论】:
【参考方案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];以上是关于检查用户何时从 iOS 设置应用程序返回的主要内容,如果未能解决你的问题,请参考以下文章