iOS startUpdatingLocation 从未调用过

标签 ios objective-c cllocationmanager

我一直在尝试获取位置坐标。在我调用 [locationManager startUpdatingLocation]; 之后没有任何反应。

它从不调用 - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations;

有什么想法吗?

注意:我已经设置了 CLLocationManagerDelegate 并使我的 CLLocationManager 类型为 strong。在我的 viewDidLoad 中,我设置了我的委托(delegate),我正在尝试使用 requestAlwaysAuthorization

ViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];


    NSLog(@"locationServicesEnabled: %@", [CLLocationManager locationServicesEnabled] ? @"YES":@"NO");
    NSLog(@"locationManager init");
    NSLog(@"geocoder init");
    locationManager = [[CLLocationManager alloc] init];
    geocoder = [[CLGeocoder alloc] init];

    //locationManager.delegate = self;
    [locationManager setDelegate:self];
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager requestAlwaysAuthorization];

    NSLog(@"locationManager startUpdatingLocation");
    [locationManager startUpdatingLocation];
}

#pragma mark - CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"locationManager:manager");
    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(@"locations %@", locations);

    CLLocation *newLocation = [locations lastObject];
    NSLog(@"didUpdateToLocation: %@", newLocation);
    CLLocation *currentLocation = newLocation;

    if (currentLocation != nil) {
        longitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
        latitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
    }

    // Stop Location Manager
    [locationManager stopUpdatingLocation];

    // Reverse Geocoding
    NSLog(@"Resolving the Address");
    [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
        NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
        if (error == nil && [placemarks count] > 0) {
            placemark = [placemarks lastObject];
            locationLabel.text = [NSString stringWithFormat:@"%@, %@",
                                 placemark.locality,
                                 placemark.administrativeArea];
        } else {
            NSLog(@"%@", error.debugDescription);
        }
    } ];

}

ViewController.h

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>

@interface ViewController : UIViewController <CLLocationManagerDelegate>

@property (strong, nonatomic) CLLocationManager *locationManager;
@property (strong, nonatomic) CLGeocoder *geocoder;
@property (strong, nonatomic) CLPlacemark *placemark;

日志

2014-09-24 08:18:39.783 appName[1201:130186] locationServicesEnabled: YES
2014-09-24 08:18:39.784 appName[1201:130186] locationManager init
2014-09-24 08:18:39.784 appName[1201:130186] geocoder init
2014-09-24 08:18:39.785 appName[1201:130186] locationManager startUpdatingLocation

我也试过删除 [locationManager stopUpdatingLocation]; 但它仍然没有做任何事情。

其他注意事项:如果我检查设备设置/隐私,则表明我的应用程序确实具有位置权限。我所有的 .h 属性都在 .m 文件中正确地 @synthesize

最佳答案

如果您使用的是 iOS 8,则需要调用以下任一方法: -[CLLocationManager requestWhenInUseAuthorization]-[CLLocationManager requestAlwaysAuthorization] 在调用 [locationManager startUpdatingLocation]; 之前。

您还需要在 .plist 中为这些键 NSLocationUsageDescriptionNSLocationWhenInUseUsageDescriptionNSLocationAlwaysUsageDescription

提供描述

关于iOS startUpdatingLocation 从未调用过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26016798/

相关文章:

ios - 在执行 UI 测试期间更新模拟器中的位置

iOS:确定另一个应用程序是否处于事件状态

ios - 将 mac 升级到 OSx Yosemite 后 SCplugin 停止工作

上传应用程序时忽略 Info.plist 中的 IOS 权限

ios - 提醒不起作用

ios - 无法完全隐藏 UIView

iphone - 为什么我的视网膜显示器特定图像显示得比屏幕大?

ios - 如何将本地通知设置为从存储在数组中的日期开始触发?

iphone - 如何将@selector 作为参数传递?

ios - 在固定点和我在 iOS 中的当前位置之间行走时计算距离