簡體   English   中英

CLLocationManager委托方法未被調用

[英]CLLocationManager Delegate methods are not getting called

我正在使用CLLocationManager類。 我有一個簡單的類方法來捕獲位置

+(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調用此方法

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

調用該方法但未調用委托方法。

我還有其他類方法,如果我再次嘗試調用該方法,則調用captureLocation方法,但不調用委托方法。 這是另一個類方法

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

請幫助我找出為什么委托方法沒有被調用,因為我是這個領域的新手。 提前致謝。

還要知道iOS 8的CoreLocation權限已更改。如果您未請求新的權限授權,則CoreLocation不會執行任何操作。 它安靜地失敗,因為從不調用委托方法。

我意識到這個問題是在2013年被問到的,但是如果你遇到類似的問題而沒有調用委托方法,那么這篇文章非常有幫助:

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

雖然文章非常詳細且篇幅很長,但實際的代碼修復可能與此相同:

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

並且您必須向info.plist添加一個值,這是要在權限警報中顯示的消息。 看屏幕抓取。

在info.plist中屏幕抓取新條目

密鑰:NSLocationWhenInUseUsageDescription

價值:需要位置才能找到你所在的位置(你可以根據需要改變它)

您正在類方法中設置CLLocationManagerdelegate (即一個前綴為+而不是- )。 所以,當你在那個類方法中引用self時,那就是類,而不是類的實例。 因此,您嘗試將delegate設置為類而不是類的實例。

那不行。 委托方法是實例方法,而不是類方法。 (這可能是為什么在分配delegate時必須使用CLLocationManagerDelegate原因。)

您必須實際實例化您已實現CLLocationManagerDelegate方法的任何類。 如果您不想將該實例綁定到特定視圖控制器,則可以使用單例模式。 無論如何,您可以將位置管理器的委托設置為指向該類的該實例。


例如,如果您希望它是單身人士:

//  MyClass.h

#import <Foundation/Foundation.h>

@interface MyClass : NSObject

+ (instancetype)sharedManager;
- (void)startCapture;
- (void)stopCapture;

@end

//  MyClass.m

#import "MyClass.h"
#import <CoreLocation/CoreLocation.h>

@interface MyClass () <CLLocationManagerDelegate>

@property (nonatomic, strong) CLLocationManager *locationManager;

@end

@implementation MyClass

+ (instancetype)sharedManager
{
    static id sharedMyManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedMyManager = [[self alloc] init];
    });
    return sharedMyManager;
}

- (void)startCapture
{
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [self.locationManager startUpdatingLocation];
}

- (void)stopCapture
{
    [self.locationManager stopUpdatingLocation];
    self.locationManager = nil;
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    // ...
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    // ...
}

@end

接着,

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[MyClass sharedInstance] startCapture];
}

在+方法中調用self會將您的委托設置為nil,因為它在[[ClassName alloc] init]表示ClassName

你需要:

mLocationManager.delegate = mLocationManager

代替

mLocationManager.delegate (id<CLLocationManagerDelegate>)self;

ios6 locationManager:didUpdateToLocation:fromLocation:已棄用,因此您需要在代碼中添加其他方法...

-(void)locationManager:(CLLocationManager *)manager
   didUpdateToLocation:(CLLocation *)newLocation
          fromLocation:(CLLocation *)oldLocation
{

      // this method get called in ios7 . 
}

在iOS -8中需要做一些更改:

請看一下: 獲取iOS-7和iOS-8中的當前位置

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM