【问题标题】:Core Location delegate methods not getting called in iOS 8.3 Xcode 6.3.1在 iOS 8.3 Xcode 6.3.1 中未调用核心位置委托方法
【发布时间】:2015-07-29 01:57:44
【问题描述】:

我正在尝试使用 Xcode 6.3.1 中的核心位置框架获取用户的当前位置,我做了以下事情:

  1. 在Target-> General-> Linked Frameworks & Libraries下添加了Core Location Framework
  2. 我的ViewController.h文件如下图,

    #import <UIKit/UIKit.h>
    #import <CoreLocation/CoreLocation.h>
    
    @interface ViewController : UIViewController<CLLocationManagerDelegate>
    @property (weak, nonatomic) IBOutlet UILabel *lblLatitude;
    @property (weak, nonatomic) IBOutlet UILabel *lblLongitude;
    @property (weak, nonatomic) IBOutlet UILabel *lblAddress;
    @property (strong, nonatomic) CLLocationManager *locationManager;
    
    @end
    
  3. 我的ViewController.m文件如下图,

    - (void)viewDidLoad
    {
    
    [super viewDidLoad];
    self.locationManager = [[CLLocationManager alloc] init];
    
    self.locationManager.delegate = self;
    if(IS_OS_8_OR_LATER){
        NSUInteger code = [CLLocationManager authorizationStatus];
        if (code == kCLAuthorizationStatusNotDetermined && ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)] || [self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])) {
        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");
        }
    }
    }
    [self.locationManager startUpdatingLocation];
    }
    
    #pragma mark - CLLocationManagerDelegate
    
    - (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 didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
    {
        NSLog(@"didUpdateToLocation: %@", newLocation);
        CLLocation *currentLocation = newLocation;
    
        if (currentLocation != nil) {
            lblLatitude.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
            lblLongitude.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
        }
    }
    @end
    

我还在 info.plist 文件中添加了以下键

  • NSLocationWhenInUseUsageDescription
  • NSLocationAlwaysUsageDescription

检查了here、here、here、here 和更多列表的所有内容

那么,有没有人有解决这个问题的方法,请帮忙。一整天都在寻找这个而失去理智。

【问题讨论】:

  • 那些使用说明的值为空。你确定你把它们放进去了?还有为什么你有两个用法说明?
  • @VinhNguyen 我也尝试过将值放入其中,但这没有任何影响,并且只保留 NSLocationAlwaysUsageDescription 对我也不起作用。也试过了...
  • 尝试删除 if 块 if (code == kCLAuthorizationStatusNotDetermined &amp;&amp;.... 并简单地在您的 locationManager 实例上调用 requestAlwaysAuthorization 或 requestWhenInUseAuthorization
  • 试过了,但仍然没有调用,甚至没有得到提示消息。
  • 您的IS_OS_8_OR_LATER 宏中有什么内容?尝试删除它。

标签: ios core-location ios8.3 xcode6.3.1


【解决方案1】:

是的!得到了解决方案,这是我的整个代码和添加的东西以使其正常工作。特别感谢@MBarton 的大力帮助。也感谢@Vinh Nguyen 投入宝贵的时间来解决我的问题。

在Target->General->Linked Frameworks & Libraries

下添加了Core Location Framework

添加到 .plist 文件中

NSLocationAlwaysUsageDescription

查看截图:

在我的ViewController.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <MapKit/MKAnnotation.h>

// #define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)

@interface ViewController : UIViewController  <MKMapViewDelegate,  CLLocationManagerDelegate>
{
    __weak IBOutlet UINavigationItem *navigationItem;
}

 @property (weak, nonatomic) IBOutlet MKMapView *mapView;
 @property(nonatomic, retain) CLLocationManager *locationManager;

@end

然后在ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize mapView;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self setUpMap];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)setUpMap
{
    mapView.delegate = self;
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
#ifdef __IPHONE_8_0
   // if(IS_OS_8_OR_LATER) {
    if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) { 
        // Use one or the other, not both. Depending on what you put in info.plist
        [self.locationManager requestAlwaysAuthorization];
    }
#endif
    [self.locationManager startUpdatingLocation];

    mapView.showsUserLocation = YES;
    [mapView setMapType:MKMapTypeStandard];
    [mapView setZoomEnabled:YES];
    [mapView setScrollEnabled:YES];
}

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:YES];

    self.locationManager.distanceFilter = kCLDistanceFilterNone;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [self.locationManager startUpdatingLocation];
    NSLog(@"%@", [self deviceLocation]);

    //View Area
    MKCoordinateRegion region = { { 0.0, 0.0 }, { 0.0, 0.0 } };
    region.center.latitude = self.locationManager.location.coordinate.latitude;
    region.center.longitude = self.locationManager.location.coordinate.longitude;
    region.span.longitudeDelta = 0.005f;
    region.span.longitudeDelta = 0.005f;
    [mapView setRegion:region animated:YES];

}

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 800, 800);
    [self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];
}
- (NSString *)deviceLocation {
    return [NSString stringWithFormat:@"latitude: %f longitude: %f", self.locationManager.location.coordinate.latitude, self.locationManager.location.coordinate.longitude];
}

呜呜……!自过去 5 天以来与许多代码战斗后得到了解决方案...

【讨论】:

  • 不要使用IS_OS_8_OR_LATER。有适当的方法来检查 API 是否可用。
  • @rmaddy 更新了我的代码,请查看并提供您宝贵的建议。感谢您让我知道这件事。
  • 这样更好。我会从您的答案中完全删除对这些宏的所有引用。
猜你喜欢
  • 2017-01-07
  • 1970-01-01
  • 2016-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多