我正在使用iOS 6.1 iPhone app
而啓用了ARC
,它將使用CLLocationManager
來跟蹤設備的位置。CLLocationManager不會將位置發送到didUpdateLocations方法
UPDATE
我有一個DestinationViewController
它實現了CLLocationManagerDelegate
。它會創建CLLocationManager
並執行設置。我已將CoreLocation framework
導入到我的項目中。
我試圖調試很多,可以看到CLLocationManager
已創建且沒有錯誤。但問題是在[self.locationManager startUpdatingLocation];
之前的[CLLocationManager authorizationStatus]
是kCLAuthorizationStatusNotDetermined
以及之後的。 因此,沒有提示要求爲此應用使用位置服務的權限。因爲這個方法從來沒有被解僱。
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:
(NSArray *)locations
我也嘗試在網上搜索幾個小時尋找類似的例子,但沒有運氣。來自DestinationViewController的代碼發佈在下面。
接口
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface DestinationViewController : UIViewController <CLLocationManagerDelegate>
實施
#import "DestinationViewController.h"
@interface DestinationViewController()
@property (strong, nonatomic) CLLocationManager *locationManager;
@end
@implementation DestinationViewController
// Initializer
- (CLLocationManager *)locationManager
{
if (!_locationManager) {
_locationManager = [[CLLocationManager alloc] init];
}
return _locationManager;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self startLocation];
}
- (void)startLocation
{
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = 1;
NSString *error;
if (![CLLocationManager locationServicesEnabled]) {
error = @"Error message";
}
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
if (status == kCLAuthorizationStatusRestricted ||
status == kCLAuthorizationStatusDenied ||) {
error = @"Error message";
}
if (error) {
NSLog(error);
}
else
{
status = [CLLocationManager authorizationStatus];
self.locationManager.pausesLocationUpdatesAutomatically = NO;
[self.locationManager startUpdatingLocation];
status = [CLLocationManager authorizationStatus];
NSLog(@"CLLocationManager is %@", self.locationManager);
NSLog(@"Location is %@", self.locationManager.location);
[self updateUI];
}
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
{
CLLocation *recentLocation = [locations lastObject];
NSLog(@"Found location");
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError");
}
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
NSLog(@"Change in authorization status");
}
這是模擬器還是設備?如果模擬器,您是否允許XCode模擬該位置? – 2013-03-27 10:50:06
已經嘗試過。我創建了一個自定義位置,並試圖執行高速公路驅動也 – 2013-03-27 11:12:12
你可以嘗試執行不推薦的'locationManager:didUpdateToLocation:fromLocation:'並看看是否被調用? – 2013-03-27 11:13:56