我的应用在iOS 7上运行良好,但在iOS 8 SDK上却无法运行。
CLLocationManager不返回位置,我没有看到我的应用程序下设置->位置服务。我在这个问题上做了谷歌搜索,但没有任何结果。会有什么问题呢?
我的应用在iOS 7上运行良好,但在iOS 8 SDK上却无法运行。
CLLocationManager不返回位置,我没有看到我的应用程序下设置->位置服务。我在这个问题上做了谷歌搜索,但没有任何结果。会有什么问题呢?
当前回答
我的解决方案可以在Xcode 5中编译:
#ifdef __IPHONE_8_0
NSUInteger code = [CLLocationManager authorizationStatus];
if (code == kCLAuthorizationStatusNotDetermined && ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)] || [self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])) {
// choose one request according to your business.
if([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationAlwaysUsageDescription"]){
[self.locationManager requestAlwaysAuthorization];
} else if([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationWhenInUseUsageDescription"]) {
[self.locationManager requestWhenInUseAuthorization];
} else {
NSLog(@"Info.plist does not contain NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription");
}
}
#endif
[self.locationManager startUpdatingLocation];
其他回答
- (void)startLocationManager
{
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone; //whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
[locationManager requestWhenInUseAuthorization]; // Add This Line
}
还有你的信息。plist文件
objective - c程序 遵循以下指示:
对于iOS-11 对于iOS 11,看看这个答案:iOS 11位置访问
需要在plist中添加两个键并提供如下图所示的消息:
1. NSLocationAlwaysAndWhenInUseUsageDescription
2. NSLocationWhenInUseUsageDescription
3. NSLocationAlwaysUsageDescription
对于iOS-10及以下版本:
NSLocationWhenInUseUsageDescription
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers;
if([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]){
[locationManager requestWhenInUseAuthorization];
}else{
[locationManager startUpdatingLocation];
}
委托方法
#pragma mark - Lolcation Update
- (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 didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
switch (status) {
case kCLAuthorizationStatusNotDetermined:
case kCLAuthorizationStatusRestricted:
case kCLAuthorizationStatusDenied:
{
// do some error handling
}
break;
default:{
[locationManager startUpdatingLocation];
}
break;
}
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
{
CLLocation *location = [locations lastObject];
userLatitude = [NSString stringWithFormat:@"%f", location.coordinate.latitude] ;
userLongitude = [NSString stringWithFormat:@"%f",location.coordinate.longitude];
[locationManager stopUpdatingLocation];
}
迅速的过程
遵循以下指示:
对于iOS-11 对于iOS 11,看看这个答案:iOS 11位置访问
需要在plist中添加两个键并提供如下图所示的消息:
1. NSLocationAlwaysAndWhenInUseUsageDescription
2. NSLocationWhenInUseUsageDescription
3. NSLocationAlwaysUsageDescription
对于iOS-10及以下版本:
import CoreLocation
class ViewController: UIViewController ,CLLocationManagerDelegate {
var locationManager = CLLocationManager()
//MARK- Update Location
func updateMyLocation(){
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers;
if locationManager.respondsToSelector(#selector(CLLocationManager.requestWhenInUseAuthorization)){
locationManager.requestWhenInUseAuthorization()
}
else{
locationManager.startUpdatingLocation()
}
}
委托方法
//MARK: Location Update
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
NSLog("Error to update location :%@",error)
}
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
switch status {
case .NotDetermined: break
case .Restricted: break
case .Denied:
NSLog("do some error handling")
break
default:
locationManager.startUpdatingLocation()
}
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations.last! as CLLocation
var latitude = location.coordinate.latitude
var longitude = location.coordinate.longitude
}
保持可可钥匙信息总是在你的指尖为这些更新,这里是链接:
https://developer.apple.com/library/ios/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html#//apple_ref/doc/uid/TP40009251-SW26
享受。
我最终解决了自己的问题。
显然,在iOS 8 SDK中,在开始位置更新之前需要调用CLLocationManager上的requestAlwaysAuthorization(用于后台位置)或requestWhenInUseAuthorization(仅用于前台位置)。
在Info中还需要NSLocationAlwaysUsageDescription或NSLocationWhenInUseUsageDescription键。Plist并在提示符中显示一条消息。加上这些就解决了我的问题。
更多详细信息,请查看:Core-Location-Manager-Changes-in-ios-8
我在InfoPlist中添加了这些键。字符串在iOS 8.4, iPad mini 2。这也很有效。我没有在Info.plist中设置任何键,比如NSLocationWhenInUseUsageDescription。
InfoPlist.strings:
"NSLocationWhenInUseUsageDescription" = "I need GPS information....";
基于这个线程,它说,就像在iOS 7中一样,可以本地化到InfoPlist.strings中。在我的测试中,这些键可以直接在InfoPlist.strings文件中配置。
因此,您需要做的第一件事是将>下面的一个或两个键添加到您的Info中。plist文件: NSLocationWhenInUseUsageDescription NSLocationAlwaysUsageDescription 这两个键都有一个字符串,用来描述你为什么 需要位置服务。您可以输入一个字符串,例如“Location is” 需要知道你在哪里”,这在iOS 7中是可以做到的 在InfoPlist中本地化。字符串文件。
更新:
我认为@IOS的方法更好。将键添加到信息。将本地化的字符串添加到InfoPlist.strings中。