【问题标题】:CLLocationManagerDelegate delegate not called iOS in 8.1CLLocationManagerDelegate 委托未在 8.1 中调用 iOS
【发布时间】:2015-03-27 08:50:24
【问题描述】:

当我在 iOS 7 设备上运行时,我在我的静态库中使用 CLLocationManagerDelegate 一切正常,但是当我在另一台使用 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];

请帮助我找出为什么没有调用委托方法! 为什么我没有收到位置更新??

【问题讨论】:

    标签: ios objective-c location static-libraries cllocationmanager


    【解决方案1】:

    我希望它可以帮助你。在if().....内拨打[locationManager requestWhenInUseAuthorization]

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

    必须添加 NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescription 在您的 Plist 文件中,并带有一些消息“此应用需要您的位置”

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

    【讨论】:

    • 你把代码从requestWhenInUseAuthorization改成你的requestAlwaysAuthorization
    • mark BreakPoint in - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation delegate is this called by [locationManager requestWhenInUseAuthorization];
    • 什么都没有发生,因为委托方法根本没有被调用
    • 只需在接口CLLocationManager *locationManager;中声明
    • 在.h文件@property (nonatomic, retain)CLLocationManager *locationManager;或.m文件接口CLLocationManager *locationManager;
    【解决方案2】:
    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
    

    【讨论】:

      【解决方案3】:

      您能在控制台输出中看到类似以下消息的内容吗?

      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。

      希望对你有帮助 亲切的问候, 彼得

      【讨论】:

      • 感谢彼得的回答,但我在控制台中没有收到任何错误消息:/ 我已经在 plist 中添加了这个键,并且在 appDelegate 中添加了 CLLocation,但我总是有一个空值CLLocationManager 并且永远不会获得位置..
      【解决方案4】:
      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];
      

      【讨论】:

      • 这将调用 CLLocationManager 的所有委托方法。
      【解决方案5】:

      对于 iOS 8,您需要在 Info.plist 中定义“隐私 - 位置使用说明”。

      例如。隐私 - 位置使用说明 = “使用您的位置显示附近的商店”。

      此键指定访问用户位置信息的原因。

      【讨论】:

      • 这对医疗有帮助。有问题的关键是NSLocationWhenInUseUsageDescription
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-16
      • 2020-02-14
      • 2016-05-07
      • 2017-02-09
      • 1970-01-01
      • 1970-01-01
      • 2015-11-15
      相关资源
      最近更新 更多