And*_*iis 3 iphone objective-c core-location cllocationmanager ios
我正在iOS 6.1 iPhone app使用ARC启用它将使用a CLLocationManager来跟踪设备位置.
UPDATE
我有一个DestinationViewController实现了CLLocationManagerDelegate.它创建CLLocationManager并进行设置.我已经导入了CoreLocation framework我的项目.
我试图调试很多,可以看到它CLLocationManager已创建,并且没有错误.但问题是,[CLLocationManager authorizationStatus]是kCLAuthorizationStatusNotDetermined
双方之前[self.locationManager startUpdatingLocation];也经过.因此,没有提示要求为此应用程序使用位置服务的权限.因此,这种方法永远不会被解雇.
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:
(NSArray *)locations
Run Code Online (Sandbox Code Playgroud)
我也尝试在网上搜索几个小时来寻找类似的例子,但没有运气.DestinationViewController中的代码发布在下面.
接口
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface DestinationViewController : UIViewController <CLLocationManagerDelegate>
Run Code Online (Sandbox Code Playgroud)
履行
#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");
}
Run Code Online (Sandbox Code Playgroud)
你的代码应该有效.
确保您的应用程序有权使用位置服务.
您可以使用以下方法检查代码中的授权状态
+ (CLAuthorizationStatus)authorizationStatus
Run Code Online (Sandbox Code Playgroud)
编辑
kCLAuthorizationStatusNotDetermined很可能是由禁用位置服务引起的.您应该首先检查位置服务状态.代码应该是这样的
if([CLLocationManager locationServicesEnabled]) {
// Location Services Are Enabled
switch([CLLocationManager authorizationStatus]) {
case kCLAuthorizationStatusNotDetermined:
// User has not yet made a choice with regards to this application
break;
case kCLAuthorizationStatusRestricted:
// This application is not authorized to use location services. Due
// to active restrictions on location services, the user cannot change
// this status, and may not have personally denied authorization
break;
case kCLAuthorizationStatusDenied:
// User has explicitly denied authorization for this application, or
// location services are disabled in Settings
break;
case kCLAuthorizationStatusAuthorized:
// User has authorized this application to use location services
break;
}
} else {
// Location Services Disabled
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11098 次 |
| 最近记录: |