我在我的静态库中使用CLLocationManagerDelegate当我在iOS 7设备上运行时,一切都运行良好但是当我在另一台使用iOS 8.1.3的设备上测试它时,委托方法没有被调用。 我做了一个强大的财产 @property(非原子,强)CLLocationManager locationManager 我还在 info.plist 中添加了相应的键和字符串值 (NSLocationAlwaysUsageDescription,requestAlwaysAuthorization) 这是我的实例
self.locationManager = [[CLLocationManager alloc] init];
[self.locationManager setDelegate:self];
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.locationManager startUpdatingLocation];
[self.locationManager requestWhenInUseAuthorization];
[self.locationManager requestAlwaysAuthorization];
if([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
}
[self.locationManager startUpdatingLocation];
请帮助我找出为什么委托方法没有被调用!! 为什么我没有获得位置更新?
答案 0 :(得分:2)
我希望它会帮助你。在[locationManager requestWhenInUseAuthorization]
if().....
if ([CLLocationManager locationServicesEnabled])
{
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
{
[locationManager requestWhenInUseAuthorization];
}
[locationManager startUpdatingLocation];
}
else{
UIAlertView *servicesDisabledAlert = [[UIAlertView alloc] initWithTitle:@"Location Services Disabled" message:@"You currently have all location services for this device disabled. If you proceed, you will be showing past informations. To enable, Settings->Location->location services->on" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:@"Continue",nil];
[servicesDisabledAlert show];
[servicesDisabledAlert setDelegate:self];
}
- (void)requestWhenInUseAuthorization
{
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
// If the status is denied or only granted for when in use, display an alert
if (status == kCLAuthorizationStatusAuthorizedWhenInUse || status == kCLAuthorizationStatusDenied) {
NSString *title;
title = (status == kCLAuthorizationStatusDenied) ? @"Location services are off" : @"Background location is not enabled";
NSString *message = @"To use background location you must turn on 'Always' in the Location Services Settings";
UIAlertView *alertViews = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Settings", nil];
[alertViews show];
}
// The user has not enabled any location services. Request background authorization.
else if (status == kCLAuthorizationStatusNotDetermined) {
[locationManager requestWhenInUseAuthorization];
}
}
if ([alertView.message isEqualToString:@"To use background location you must turn on 'Always' in the Location Services Settings"])
{
if (buttonIndex == 1)
{
// Send the user to the Settings for this app
NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
[[UIApplication sharedApplication] openURL:settingsURL];
}
}
你必须添加 Plist文件中的 NSLocationAlwaysUsageDescription 或 NSLocationWhenInUseUsageDescription 包含一些消息"此应用需要您的位置"
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
CLLocation *location;
location = [manager location];
CLLocationCoordinate2D coordinate = [location coordinate];;
globalObjects.longitude = [NSString stringWithFormat:@"%f",coordinate.longitude];
globalObjects.latitude = [NSString stringWithFormat:@"%f",coordinate.latitude];
}
答案 1 :(得分:2)
对于iOS 8,您需要在Info.plist中定义“隐私 - 位置使用说明”。
EG。隐私 - 位置使用说明=“使用您的位置在商店附近显示”。
此键指定访问用户位置信息的原因。
答案 2 :(得分:1)
locationManager = [[CLLocationManager alloc] init];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager setDistanceFilter:5];
[locationManager setHeadingFilter:5];
[locationManager setDelegate:self];
if ([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[locationManager requestAlwaysAuthorization];
}
[locationManager startUpdatingLocation];
答案 3 :(得分:0)
您能在控制台输出中看到类似以下消息的内容吗?
Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.
当我第一次将我的应用程序移植到iOS8时,我遇到了这个问题。定义以下键有一个新的限制。
/* Localized versions of Info.plist keys */
"NSLocationWhenInUseUsageDescription" = "for some reason, your app will use your location whenever the app is in foreground";
"NSLocationAlwaysUsageDescription" = "for some reason, your app will use your location whenever the app is in background";
这些键必须在项目根目录中名为 InfoPlist.strings 的可本地化字符串文件中定义。我把这个文件放在错误的位置一次,这花了我很长时间才弄明白问题出在哪里。
接下来,您应该检查,当您应该处理用户的确认时,您已请求授权的生命周期CLLocationManager 尚未超过。例如。在AppDelegate中定义全局CLLocationManager。
我希望有帮助, 亲切的问候, 彼得
答案 4 :(得分:0)
if (![CLLocationManager locationServicesEnabled])
{
[[UtilityClass sharedObject]showAlertWithTitle:@"Location Services disabled" andMessage:@"App requires location services to find your current city weather.Please enable location services in Settings."];
}
else{
[self stopLocationUpdating];
if (locationManager==nil) {
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
#ifdef __IPHONE_8_0
if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8"))
{
// Use one or the other, not both. Depending on what you put in info.plist
//[self.locationManager requestWhenInUseAuthorization];
[locationManager requestAlwaysAuthorization];
}
#endif
}
[locationManager startUpdatingLocation];
[locationManager startUpdatingLocation];
}
并将以下代码添加到.plist文件中
NSLocationAlwaysUsageDescription=Application would like to use your location