我正在尝试在iOS应用中使用用户位置。该应用程序适用于iOS 7,但不适用于iOS 8。
我尝试了一切。阅读一堆教程,我知道在iOS 8中我们需要调用requestWhenInUseAuthorization方法并将NSLocationWhenInUseUsageDescription键添加到Info.plist中。
但它仍然无法正常工作,它没有得到用户的许可,我正在使用真实设备(运行iOS 8.1.x的iPhone 5c)进行测试。我尝试多次重新安装该应用程序。
这是我为测试这件事而创建的非常小的项目的代码。
我的ViewController.m
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController () <CLLocationManagerDelegate>
@property (strong, nonatomic) CLLocationManager *locationManager;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = kCLHeadingFilterNone;
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
NSLog(@"Pôde chamar o método"); // It could call the method
[self.locationManager requestWhenInUseAuthorization];
}
// [self.locationManager startUpdatingLocation];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
NSLog(@"Atualizou Localização para: %@", [locations lastObject]); // Updated the location to
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
NSLog(@"Location Manager falhou com erro: %@", error); // Location manager failed with error
}
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
if (status == kCLAuthorizationStatusAuthorized) {
[self.locationManager startUpdatingLocation];
}
}
@end
我的信息列表
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>wallace.$(PRODUCT_NAME:rfc1034identifier)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>BNDL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string></string>
</dict>
</plist>
有人知道如何解决这个问题吗?
答案 0 :(得分:1)
将NSLocationAlwaysUsageDescription
和NSLocationWhenInUseUsageDescription
添加到Info.plist
。然后,您可以为每个值提供AlertView文本。
位置管理员将查看这些字段中的文本以在AlertView中提示。
答案 1 :(得分:1)
将以下内容添加到locationManager:didChangeAuthorizationStatus
:
if (status == kCLAuthorizationStatusNotDetermined) {
[manager requestAlwaysAuthorization];
}
答案 2 :(得分:-1)
我不知道为什么,但出于某些原因,将NSLocationWhenInUseUsageDescription
添加到info.plist
并调用requestWhenInUseAuthorization
对我来说不起作用,经过几次尝试后我找到了以下解决方案。有线但为我工作。
首先在NSLocationWhenInUseUsageDescription
中添加NSLocationAlwaysUsageDescription
和info.plist
个密钥,然后调用方法requestAlwaysAuthorization
和requestWhenInUseAuthorization
。现在您应该收到通知。收到通知后,您可以删除不必要的代码和info.plist项目。
经过多次试验后,它仍然有效,希望它能为少数人提供帮助。