我正在研究一个iOS 6.1 iPhone app
with ARC
enabled ,它将使用 aCLLocationManager
来跟踪设备的位置。
更新
我有一个DestinationViewController
实现CLLocationManagerDelegate
. 它创建CLLocationManager
并进行设置。我已将其导入CoreLocation framework
到我的项目中。
我尝试了很多调试,可以看到CLLocationManager
已创建并且不存在错误。但问题是[CLLocationManager authorizationStatus]
既是kCLAuthorizationStatusNotDetermined
before[self.locationManager startUpdatingLocation];
也是after。所以没有提示询问是否允许使用此应用的位置服务。因为这个方法永远不会被解雇。
- (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");
}