我使用单例类来跟踪用户位置。如果我从我正在运行的应用程序中实例化该类并开始跟踪所有内容都是很好的。
然而,如果应用程序已关闭(在应用程序切换器中,因此它不再背景)并重新启动我检查是否存在活动事件。在这种情况下,我实例化类并开始位置跟踪。 <{1}}被实例化,位置箭头出现在状态栏中,但是
locationManager
任何想法?
code singelton class:
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
or
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
never get called
来自AppDelegate的代码:
@interface AlarmMeTrackUserLocation () <CLLocationManagerDelegate>
@property (strong, nonatomic) CLLocationManager *locationManager;
@end
@implementation AlarmMeTrackUserLocation
-(void)toggleLocationUpdates:(bool)toggle {
if (!toggle) {
[self.locationManager stopUpdatingLocation];
self.locationManager = nil;
sharedInstance = nil;
NSLog(@"--- UserTrack Stopped ---");
return;
}
if (!self.locationManager && toggle) {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
self.locationManager.pausesLocationUpdatesAutomatically = YES;
self.locationManager.distanceFilter = 200;
}
NSLog(@"--- UserTrack Started");
[self.locationManager startUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
...
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error {
...
}
+ (AlarmMeTrackUserLocation *)sharedTrackUserLocation {
if (sharedInstance == nil) {
sharedInstance = [[super allocWithZone:NULL] init];
//NSLog(@"--- New Instance created ---");
return sharedInstance;
}
NSLog(@"--- Existing Instance Used ---");
return sharedInstance;
}
答案 0 :(得分:0)
仔细检查我的代码后,我只能看到一个区别:在app委托中,我启动了一个后台线程来从网络服务器获取数据。如果请求完成,则实例化单例类并设置locationManager(仍在后台线程中)。
切换回主线程并实例化后终于工作了!
所以你似乎无法从后台线程委托CLLocationManager