[英]CLLocationManager not calling delegate in an NSObject
我正在嘗試創建一個幫助程序類,以便輕松地在任何其他類中獲取手機的坐標。 我遵循了一個教程,其中UIViewController
實現了<CLLocationManagerDelegate>
並且它工作正常。 我試圖在一個簡單的NSObject
做同樣的事情,但后來我的代理人不再被調用了。
這是我的代碼:
PSCoordinates.h
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
@interface PSCoordinates : NSObject <CLLocationManagerDelegate>
@property (nonatomic, retain) CLLocationManager* locationManager;
@end
PSCoordinates.m
#import "PSCoordinates.h"
@implementation PSCoordinates
- (id) init {
self = [super init];
if (self) {
self.locationManager = [[CLLocationManager alloc] init];
if ([CLLocationManager locationServicesEnabled])
{
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = 100.0f;
NSLog(@"PSCoordinates init");
[self.locationManager startUpdatingLocation];
}
}
return self;
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSLog(@"Géolocalisation : %@",[newLocation description]);
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
NSLog(@"Géolocalisation (erreur) : %@",[error description]);
}
@end
我打電話給他
PSCoordinates * coordinates = [[PSCoordinates alloc] init];
按下按鈕時 init正在工作,因為我可以看到NSLog PSCoordinates init
。
我發現人們遇到同樣問題的其他話題,但答案都沒有解決。
非常感謝您的幫助。
在您的班級中將“PSCoordinates * coordinates”設為全局。 它會工作:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.