iOS 8.3 Xcode 6.3.1中未调用核心位置委托方法 [英] Core Location delegate methods not getting called in iOS 8.3 Xcode 6.3.1

查看:117
本文介绍了iOS 8.3 Xcode 6.3.1中未调用核心位置委托方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Xcode 6.3.1中的Core Location Framework获取用户的当前位置,我做了以下事情:

I am trying to get the user's current location using the Core Location Framework in Xcode 6.3.1, I did following things:

  1. Target -> General -> Linked Frameworks&图书馆
  2. 我的 ViewController.h 文件如下所示,

  1. Added Core Location Framework under Target-> General-> Linked Frameworks & Libraries
  2. My ViewController.h file is as shown below,

#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

  • 我的ViewController.m文件如下所示,

  • My ViewController.m file is as shown below,

    - (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文件中添加了以下键

    I had also added the following keys in my info.plist file

    • NSLocationWhenInUseUsesDescription
    • NSLocationAlwaysUsageDescription

    此处

    Checked everything given here, here, here, here, and a lot more list

    那么,有谁对此问题有解决方案,请帮忙.一整天都在寻找这个东西.

    So, is anyone having a solution for this issue, kindly help. Have lost my mind searching for this for the whole day.

    推荐答案

    是的!得到了解决方案,这是我的完整代码&增加了使其正常工作的功能.特别感谢@MBarton的大力帮助.也感谢@ Vinh Nguyen投入宝贵的时间来解决我的问题.

    Yeah! Got the solution, Here is my whole code & things added to make it working. Special thanks to @MBarton for his great help. Also Thanks to @ Vinh Nguyen for investing his precious time in solving my issue.

    Target-> General-> Linked Frameworks&中添加了 Core Location Framework .图书馆

    Added Core Location Framework under Target-> General-> Linked Frameworks & Libraries

    添加到.plist文件

    Added in .plist file

    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];
    }
    

    U ...自从过去5天以来与许多代码战斗之后,得到了解决方案...

    Ufff...! Got the solution after fighting with many codes since last 5 days...

    这篇关于iOS 8.3 Xcode 6.3.1中未调用核心位置委托方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆