繁体   English   中英

Xcode 6 / iOS 8位置模拟不起作用

[英]Xcode 6/iOS 8 Location Simulation doesn't work

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

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

你在调用其中一种方法吗?

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

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

有关详细信息,请查看此帖子: 位置服务在iOS 8中不起作用

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

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

另外在info.plist文件中添加以下行

密钥: NSLocationWhenInUseUsageDescription值:使用当前位置

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

其他答案是正确的,但我还得在我获得一个位置之前重置模拟器,而它在设备上工作正常。 该应用程序最初是在iOS 8之前安装在该模拟器上的。

要重置模拟器:

https://stackoverflow.com/a/16195931

- (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.");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM