【问题标题】:Location Issue in iPhone DeviceiPhone 设备中的位置问题
【发布时间】:2015-04-17 19:16:27
【问题描述】:

我想在我的应用程序中检测用户的当前位置。我正在使用目标 c。它在模拟器中工作正常,但在设备上进行测试时出现以下错误。

didFailWithError: Error Domain=kCLErrorDomain Code=0 "The operation couldn’t be completed. (kCLErrorDomain error 0.)"

请帮我解决这个问题。我在我的应用程序中使用 Lat long 值来查找位置标记。

  if(version<8.0)
{
    locationManager = [[CLLocationManager alloc] init];


    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    [locationManager startUpdatingLocation];
}
else
{
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    // Check for iOS 8. Without this guard the code will crash with "unknown selector" on iOS 7.
    if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
        [locationManager requestWhenInUseAuthorization];
    }
    [locationManager startUpdatingLocation];
}

这里是委托方法

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
NSLog(@"didFailWithError: %@", error);
UIAlertView *errorAlert = [[UIAlertView alloc]
                           initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];}



- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
NSLog(@"didUpdateToLocation: %@", newLocation);
CLLocation *currentLocation = newLocation;


if (currentLocation != nil) {
    NSString *longitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
    NSString *latitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];

    CLGeocoder * geoCoder = [[CLGeocoder alloc] init];



    [geoCoder reverseGeocodeLocation: locationManager.location completionHandler:
     ^(NSArray *placemarks, NSError *error) {


         CLPlacemark *placemark = [placemarks objectAtIndex:0];


         NSString *placemark_str = [placemark locality];
         subAdminArea.text=placemark.subAdministrativeArea;

         NSString *are_str = [placemark subLocality];

            subAdminArea.text=placemark.subAdministrativeArea;


         NSString *location_str=[NSString stringWithFormat:@"%@,%@",are_str,placemark_str];

         [[NSUserDefaults standardUserDefaults]setValue:location_str forKey:@"Location"];

         NSLog(@"place mark str: %@",placemark_str);


     }];

}}


- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
NSLog(@"%@", [locations lastObject]);

CLGeocoder * geoCoder = [[CLGeocoder alloc] init];


[geoCoder reverseGeocodeLocation: [locations lastObject] completionHandler:
 ^(NSArray *placemarks, NSError *error) {


     CLPlacemark *placemark = [placemarks objectAtIndex:0];


     NSString *placemark_str = [placemark locality];
     NSString *are_str = [placemark subLocality];



     NSString *location_str=[NSString stringWithFormat:@"%@,%@",are_str,placemark_str];

     [[NSUserDefaults standardUserDefaults]setValue:location_str forKey:@"Location"];




 }];}




- (void)requestAlwaysAuthorization{
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 *alertView = [[UIAlertView alloc] initWithTitle:title
                                                        message:message
                                                       delegate:self
                                              cancelButtonTitle:@"Cancel"
                                              otherButtonTitles:@"Settings", nil];
    [alertView show];
}
// The user has not enabled any location services. Request background authorization.
else if (status == kCLAuthorizationStatusNotDetermined) {
    [locationManager requestAlwaysAuthorization];
}}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 1) {
    // Send the user to the Settings for this app
    NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
    [[UIApplication sharedApplication] openURL:settingsURL];
}}

我还使用 NSLocationAlwaysUsageDescription->String & NSLocationWhenInUseUsageDescription -> String 更新了我的 plist 文件。

谢谢。

【问题讨论】:

  • 我相信你正在 iOS 8 设备上测试这个......对吧?对于 iOS 8,有些东西发生了变化……请先谷歌,然后再问这里……
  • 没有 ios 7 设备,我知道 ios 8 位置问题。它在 ios 7 和 8 的模拟器中运行良好,但在设备中运行良好。谢谢法希姆重播我。
  • 将代码也添加到问题中。它可能有助于读者找出问题...:)
  • @user3418619 :向我们展示代码然后...会帮助我们...

标签: ios objective-c iphone ios7 ios8


【解决方案1】:

1) 检查您是否确实拥有有效的 WiFi 和 3G 连接

如果你这样做了

2) 转到设置并重置您的位置服务

3) 然后重置您的网络设置

【讨论】:

  • 我已经应用了所有这些步骤,但不适用于我的情况。感谢您重播我。告诉我您是否有其他解决方案或工作代码
猜你喜欢
  • 2011-10-13
  • 2011-09-27
  • 2011-10-18
  • 2011-06-23
  • 2021-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多