【问题标题】:iOS Location Services - App is checking for location before allowediOS 定位服务 - 应用程序在允许之前检查位置
【发布时间】:2013-05-19 12:14:57
【问题描述】:

只是试图确定用户的位置。代码如下:

self.locationManager = [[CLLocationManager alloc] init];
//self.locationManager.delegate = self;
self.locationManager.distanceFilter = 80.0;
[self.locationManager startUpdatingLocation];

self.geoCoder = [[CLGeocoder alloc] init];
[self.geoCoder reverseGeocodeLocation: self.locationManager.location completionHandler:

 ^(NSArray *placemarks, NSError *error) {

     CLPlacemark *placemark = [placemarks objectAtIndex:0];

     UIAlertView *locationAlert = [[UIAlertView alloc] initWithTitle:@"Location" message:[NSString stringWithFormat:@"The app has determined that your country is: %@ (%@)", placemark.country, placemark.ISOcountryCode] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
     [alert show];


 }];

我第一次运行应用程序时,会弹出一个双重警报。 locationAlert 首先弹出,通知用户一个(空)国家,然后几乎同时弹出默认弹出窗口,表明应用程序想要使用我的位置。如果我单击“是”,下次我运行该应用程序时,我将获得用户正确国家/地区的单一弹出窗口。

所以我的问题是,我怎样才能首先获得访问用户位置的权限,然后运行此代码来查找他们的位置,而不会因为我在未经许可的情况下查询他们的位置而得到空响应?

【问题讨论】:

    标签: ios geolocation cllocationmanager clgeocoder


    【解决方案1】:

    反向地理定位代码正在尝试使用来自locationManagerlocation 属性,但在调用CLLocationManager 委托方法locationManager:didUpdateToLocation:fromLocation: 之前它是无效的。

    您应该有一个委托方法like this one 来处理位置更新。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-10
      • 1970-01-01
      • 2011-06-09
      • 2012-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-15
      相关资源
      最近更新 更多