获取用户位置警报不停留在屏幕上的iOS 8
问题描述:
我想在获取用户位置之前获得用户授权。我使用此代码为:获取用户位置警报不停留在屏幕上的iOS 8
self.locationManager = [[CLLocationManager alloc] init];
[self.locationManager setDelegate:self];
if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
// iOS8+
// Sending a message to avoid compile time error
[self.locationManager requestAlwaysAuthorization];
}
[self showIndicatorView];
self.getCityService = [[GetCitiesService alloc] initServiceWithDelegate:self isLocalCall:NO];
[self.getCityService fetchCities];
我看到屏幕上的警报,但之前,我允许它与否,它消失形式的屏幕和应用程序没有被授权。
我想我的代码停止,直到用户给予权限。
答
您的代码看起来很奇怪,因为您似乎要拨打requestAlwaysAuthorization
两次。一旦登录self.locationManager
,一次登录sendAction
。
你的代码应该是这样的:
self.locationManager = [[CLLocationManager alloc] init];
[self.locationManager setDelegate:self];
if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
// iOS8+
// Sending a message to avoid compile time error
[self.locationManager requestAlwaysAuthorization];
}
答
显然,在iOS的8 SDK,requestAlwaysAuthorization(背景位置)或requestWhenInUseAuthorization(位置仅在前台)上CLLocationManager调用起始位置更新之前是必要的。
还需要在Info.plist中使用NSLocationAlwaysUsageDescription或NSLocationWhenInUseUsageDescription项,并在提示中显示一条消息。添加这些解决了我的问题。
希望它可以帮助别人。
编辑:为了更广泛的信息,看看:http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/
我做任何输入 – iBug 2014-11-24 11:18:34
那么你所呼叫的方法两次可能会导致意外的行为之前,在此之前,但警报GET消失。 – rckoenes 2014-11-24 11:19:45
我使用你的代码,但仍然是同样的问题。 – iBug 2014-11-24 11:27:31