以下代码应获取当前位置。但是会产生上述错误。函数didUpdateLocations永远不会被调用。
在设备(iPhone 5s)iOS 9.1上运行此功能。 Info.plist具有所需的设备功能和隐私 - 配置位置使用说明,如附图所示。请帮忙!
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
locationManager.requestLocation()
}
func locationManager(manager: CLLocationManager,
didFailWithError error: NSError)
print(error.description)
}
func locationManager(manager: CLLocationManager, didUpdateLocations
locations: [CLLocation]) {
print(“got location”)
}
答案 0 :(得分:44)
请试试这个:
转到:
编辑:正如评论中提到的那样,还要重新启动Xcode,你就完成了!
希望这有帮助。
答案 1 :(得分:3)
如果没有为您弹出位置授权警报,请尝试将NSLocationAlwaysUsageDescription作为类型字符串添加到plist中。另外,对NSLocationWhenInUseUsageDescription执行相同的操作。
答案 2 :(得分:3)
搜索谷歌真的答案这似乎是一个问题,因为IOS4.0。
在Project =>下的XCODE 7.1中的scheme =>编辑方案。
在Run =>下选项=>核心位置/禁用允许位置模拟。
<=>在您的IOS模拟器中,在Debug =&gt;下位置选择一个位置。这解决了我的问题。答案 3 :(得分:2)
我遇到了与我从8.0移动的应用程序相同的问题 - &gt; 9.0 / 9.1。我尝试了一些事情,比如只在Swift中编写CLLocationManager部分,以及一个新的Obj-C项目。我还在不同的设备上测试过它。为了解决设备上的问题,我删除了didFailWithError func。这完全阻止了错误。这似乎不是最好的主意,但在IOS9中,您可以将应用程序限制为具有GPS或位置服务的设备。 Android已经做了很长时间了。我还注意到,在.plist中,您没有为NSLocationALwaysUsageDescription或NSLocationWhenInUseUsageDescrtiption属性启用权限。
仅供参考我已经附加了我当前的.plist看起来不会触发此错误以及obj-c控制器文件中的代码块。代码从上一个函数的最后开始,然后注释掉didFailWithError委托和didUpdateLocations。目前,这在IOS 9.1中已成功运行
//Get map authorization
[CLLocationManager locationServicesEnabled];
locationManager = [[CLLocationManager alloc]init];
locationManager.delegate = self;
locationManager.allowsBackgroundLocationUpdates = YES;
if(nil == locationManager) locationManager = [[CLLocationManager alloc]init];
if ([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[locationManager requestAlwaysAuthorization];
}
//locationManager.distanceFilter=10;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[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 didUpdateLocations:(NSArray *)locations{
NSLog(@"LocationUpdated: %@",[locations lastObject]);
[self updateMapMarkers];
// NSDate* eventDate = location.timestamp;
// NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
// if(fabs(howRecent) < 15.0){
// [self updateMapMarkers];
// }
}
答案 4 :(得分:2)
答案 5 :(得分:1)
答案 6 :(得分:1)
我正在使用XCode14。这是我为解决此问题所做的事情
答案 7 :(得分:0)
我对在他们的应用中使用苹果地图的人有这些建议
答案 8 :(得分:0)
在我的例子中,我将 CLLocationManager() 的返回值分配给了 ViewControllers viewDidLoad 覆盖中的一个变量。
一旦生命周期方法完成,对象引用就会被释放;在用户有机会响应之前关闭对话框,也会导致其他逻辑问题,这可能取决于该引用。
我只需要将生命周期方法覆盖中的赋值移到我使用它的类的根目录中。