我正在尝试在我的 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 respondsToSelectorselector(requestWhenInUseAuthorization)]) {
NSLog(@"ô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)locationManagerCLLocationManager *)manager didUpdateLocationsNSArray *)locations {
NSLog(@"Atualizou Localização para: %@", [locations lastObject]); // Updated the location to
}
- (void)locationManagerCLLocationManager *)manager didFailWithErrorNSError *)error {
NSLog(@"Location Manager falhou com erro: %@", error); // Location manager failed with error
}
- (void)locationManagerCLLocationManager *)manager didChangeAuthorizationStatusCLAuthorizationStatus)status {
if (status == kCLAuthorizationStatusAuthorized) {
[self.locationManager startUpdatingLocation];
}
}
@end
我的信息.plist
<?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>
有人知道如何解决这个问题吗?
Best Answer-推荐答案 strong>
将 NSLocationAlwaysUsageDescription 和 NSLocationWhenInUseUsageDescription 添加到您的 Info.plist 。然后,您可以为每个值提供 AlertView 文本。
位置管理器将查看这些字段中的文本以在 AlertView 中进行提示。
关于iOS 8.1 CoreLocationManager requestWhenInUseAuthorization 没有提示 AlertView,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/28942516/
|