我目前正在开发我的第一个基于 CoreLocation 的应用程序,但我遇到了 CLLocationManager 不调用委托方法的问题。这是我正在使用的课程:
//
// LocationGetter.h
// whatsunderme
//
// Created by Tobias Timpe on 16.06.12.
// Copyright (c) 2012 tobisoft. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@protocol LocationGetterDelegate
@required
- (void) newPhysicalLocation:(CLLocation *)location;
@end
@interface LocationGetter : NSObject <CLLocationManagerDelegate> {
CLLocationManager *locationManager;
id delegate;
}
- (void)startUpdates;
@property (nonatomic, retain) CLLocationManager *locationManager;
@property (nonatomic, retain) id delegate;
@end
//
// LocationGetter.m
// whatsunderme
//
// Created by Tobias Timpe on 16.06.12.
// Copyright (c) 2012 tobisoft. All rights reserved.
//
#import "LocationGetter.h"
@implementation LocationGetter
@synthesize locationManager, delegate;
BOOL didUpdate = NO;
- (void)startUpdates {
if (self.locationManager == nil) {
self.locationManager = [[CLLocationManager alloc] init];
}
self.locationManager.delegate = self;
NSLog(@"Starting Location Updates");
locationManager.distanceFilter = 100;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
NSLog(@"Error");
}
// Delegate method from the CLLocationManagerDelegate protocol.
- (void)locationManager:(CLLocationManager *)manage didUpdateToLocation:(CLLocation *)
newLocation fromLocation:(CLLocation *)oldLocation {
NSLog(@"new Location found");
didUpdate = YES;
// Disable future updates to save power.
[self.locationManager stopUpdatingLocation];
[self.delegate newPhysicalLocation:newLocation];
}
@end
didUpdateToLocation 和 didFailWithError 方法都没有被调用。我不知道为什么。希望您能帮我解决这个问题,因为我花了 2 个小时试图找出问题所在。