CLLocationManager委托方法未被调用 [英] CLLocationManager Delegate methods are not getting called

查看:134
本文介绍了CLLocationManager委托方法未被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 CLLocationManager 类。我有一个简单的类方法来捕获位置

I am using CLLocationManager class. I have a simple class method for capturing the location

+(void)captureLocation{
    mLocationManager = [[CLLocationManager alloc]init];
    mLocationManager.delegate = (id<CLLocationManagerDelegate>)self;
    mLocationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [mLocationManager startUpdatingLocation];
}

我有 CLLocationManager的委托方法

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation   *)newLocation fromLocation:(CLLocation *)oldLocation
{
}

现在我试图在我的 viewDidLoad 中调用此方法as

now i am trying to call this method in my viewDidLoad as

- (void)viewDidLoad
{
    [super viewDidLoad];
    [myclass captureLocation];
}

调用该方法但未调用委托方法。

the method is getting called but the delegate methods are not getting called.

我还有其他类方法,如果我再尝试再次调用该方法,那么 captureLocation 方法会被调用但是委托方法不会被调用。这是另一个类方法

I have other class method also and from there if I try to call the method again the captureLocation method is getting called but the delegate methods are not called. Here is the other class method

+(void)initialize{
    [self captureLocation];
}

请帮我找出为什么没有调用委托方法因为我是这个领域的新人。在此先感谢。

please help me to find out why delegate methods are not getting called as I am new to this field. Thanks in advance.

推荐答案

还要知道iOS 8的CoreLocation权限已更改。如果您未请求新的权限授权,CoreLocation什么都不做。它安静地失败,因为委托方法永远不会被调用。

Also know that CoreLocation permissions have changed with iOS 8. If you don't request the new permissions authorizations, CoreLocation doesn't do anything. It fails quietly because the delegate methods are never called.

我意识到这个问题是在2013年被问到的,但如果你遇到类似的问题,那么委托方法就不会被调用,这篇文章非常有帮助:

I realize this question was asked in 2013, but if you are having a similar problem with the delegate methods not getting called, this article is extremely helpful:

http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/

虽然文章非常详细且篇幅很长,但实际的代码修复可能与此一样小:

While the article is very detailed and long, the actual code fix can be as minor as this:

if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
    [locationManager requestWhenInUseAuthorization];
}

你必须在info.plist中添加一个值,这是消息显示在权限警报中。请参阅屏幕抓取。

And you have to add a value to info.plist, which is the message to display in the permissions alert. See screen grab.

密钥:NSLocationWhenInUseUsageDescription

Key: NSLocationWhenInUseUsageDescription

价值:需要位置才能找到你所在的位置(你可以改变你想要的任何东西)

Value: Location is required to find out where you are (You can change that to whatever you want)

这篇关于CLLocationManager委托方法未被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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