【问题标题】:Xcode 6/iOS 8 Location Simulation doesn't workXcode 6/iOS 8 位置模拟不起作用
【发布时间】:2025-11-23 16:10:01
【问题描述】:

我刚刚更新到 Xcode 6/iOS 8 SDK,我在模拟器中的位置服务模拟开始无法正常工作。在我更新之前很好(我目前无法在真实设备上测试)。现在,当我选择一个位置进行模拟时,什么也没有发生。未调用委托的-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 方法。我重新启动了 Xcode,清理了构建文件夹,没有任何改变。为什么会发生这种情况?

【问题讨论】:

    标签: ios xcode ios8 xcode6


    【解决方案1】:

    其他答案是正确的,但我还必须重置模拟器才能获得位置,而它在设备上运行良好。该应用最初安装在 iOS 8 之前的那个模拟器上。

    重置你的模拟器:

    https://*.com/a/16195931

    【讨论】:

      【解决方案2】:
      - (void)startLocationUpdates{
          geocoder = [[CLGeocoder alloc] init];
          locationManager = [[CLLocationManager alloc] init];
          locationManager.delegate = self;
          locationManager.distanceFilter = kCLDistanceFilterNone;
          locationManager.desiredAccuracy = kCLLocationAccuracyBest;
          if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
              [locationManager requestWhenInUseAuthorization];
          [locationManager startUpdatingLocation];
      }
      
      - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
      
            CLLocation *currentLocation = newLocation;
          if (currentLocation != nil) {
          }
      
          // Reverse Geocoding
          [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
      
              if (error == nil && [placemarks count] > 0) {
                  placemark = [placemarks lastObject];
                              fullAddress = [NSString stringWithFormat:@"%@,%@,%@,%@",
                                 placemark.thoroughfare,
                                 placemark.locality,
                                 placemark.administrativeArea,
                                 placemark.country];
      
                  subtitleLocation = [NSString stringWithFormat:@"PostalCode::%@",
                                       placemark.postalCode];
              } else {
                  //  NSLog(@"%@", error.debugDescription);
              }
          } ];
      }
      
      - (void)locationManager:(CLLocationManager *)manager
             didFailWithError:(NSError *)error{
      
          NSLog(@"Cannot find the location.");
      }
      

      【讨论】:

        【解决方案3】:

        使用 Xcode 6.3.1 我已经停止更新位置选择。修复方法是运行另一个项目,选择“模拟位置 > 不模拟位置”,然后再次构建原始项目以恢复正常的位置设置。

        【讨论】:

          【解决方案4】:

          从 IOS 8 开始,您需要在启动 CLLocationManager 之前请求授权。

          您正在调用这些方法之一吗?

          [self.locationManager requestWhenInUseAuthorization];  // For foreground access
          [self.locationManager requestAlwaysAuthorization]; // For background access
          

          如果您在 XCode 6 之前创建了项目,您可能还需要为新权限添加 info.plist 条目。

          更多详情请看这篇帖子:Location Services not working in iOS 8

          【讨论】:

            【解决方案5】:

            在您的方法中添加以下代码

            if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) 
            {
                [self.locationManager requestWhenInUseAuthorization];
            }
            

            在您的info.plist 文件中添加以下行

            Key:NSLocationWhenInUseUsageDescriptionvalue:使用当前位置

            【讨论】:

            • 任何人对此有疑问,我挣扎了一段时间,因为我在应用程序中使用[locationManager requestWhenInUseAuthorization];,但在.plist文件中声明了NSLocationAlwaysUsageDescription(注意'always'的区别和“使用时”。此解决方案有效,但请确保声明正确的使用描述类型NSLocationWhenInUseUsageDescriptionNSLocationAlwaysUsageDescription(或两者兼有)
            最近更新 更多