【问题标题】:CLLocation current location not workingCLLocation 当前位置不起作用
【发布时间】:2017-01-13 14:57:05
【问题描述】:

我的课堂上有这个代码ViewController

CLLocationManager *locationManager;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    locationManager = [[CLLocationManager alloc] init];
    [locationManager requestWhenInUseAuthorization];
}


- (IBAction)getCurrentLocation:(id)sender {
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    [locationManager startUpdatingLocation];
}

#pragma mark - CLLocationManagerDelegate

-(void) locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    NSLog(@"Did finish with error - %@", error);
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Failed to get your location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

    NSLog(@"did Update Location - %@", newLocation);
    CLLocation *currentLocation = newLocation;

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

但我没有获得允许访问的位置或弹出窗口。

我正在使用核心位置框架。

点击按钮时,我在标签中打印纬度、经度和地址。

我正在模拟器上测试这个。

【问题讨论】:

  • 我提醒您,在plist中添加授权请求并检查您的设备可以获得。
  • 你的.plist中添加授权请求说明了吗?
  • KAR 检查上面的链接它为您提供解决方案
  • 我在 info plist 文件中添加了。谢谢@user3182143

标签: ios objective-c xcode core-location


【解决方案1】:

有时模拟器无法使用启用位置的服务使用Apple Device 进行完美测试。

在您的 info.plist 文件中添加以下键以获得允许访问弹出窗口。

或将它们添加为更新info.plist文件源代码。

<key>NSLocationAlwaysUsageDescription</key>
<string>message for location uses</string>

<key>NSLocationWhenInUseUsageDescription</key>
<string>message for location uses</string>

【讨论】:

    【解决方案2】:

    在 viewDidLoad 中试试这个代码...

    //---- For getting current gps location
        locationManager = [CLLocationManager new];
        locationManager.delegate = self;
    
        if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])  {
    
            [locationManager requestWhenInUseAuthorization];
        }
    
        locationManager.distanceFilter = kCLDistanceFilterNone;
        locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        [locationManager startUpdatingLocation];
        //------
    

    您还必须将 NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescription 键的字符串添加到应用的 Info.plist。

    【讨论】:

      【解决方案3】:
      locationManager =[[CLLocationManager alloc] init];
          [locationManager requestWhenInUseAuthorization];
          locationManager.delegate=self;
      
          locationManager.desiredAccuracy=kCLLocationAccuracyBest;
          [locationManager startUpdatingLocation];
      

      您可以在您的 plist 中添加:

      <key>NSLocationAlwaysUsageDescription</key>
      
      
      <key>NSLocationWhenInUseUsageDescription</key>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-10-30
        • 1970-01-01
        • 2012-12-28
        • 1970-01-01
        • 2017-01-21
        • 2022-12-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多