繁体   English   中英

Xcode 6模拟器的核心位置

[英]Core Location with Xcode 6 simulator

核心位置未调用didUpdateLocations 我有2个涉及LocationManager和View Controller的类。 已为requestAlwaysAuthorization设置了requestAlwaysAuthorization 在调试中模拟位置。 谁能帮我发现错误?

LocationManager.h

@interface LPLocationManager : NSObject <CLLocationManagerDelegate>

+(LPLocationManager*)sharedManager;

@property (strong, atomic) CLLocationManager *locationManager;
@property (nonatomic, retain) CLLocation *location;
@end

LocationManager.m

+(LPLocationManager*)sharedManager{
    static LPLocationManager *sharedManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedManager = [[LPLocationManager alloc]init];
    });

    return sharedManager;
}

- (id)init
{
    self = [super init];
    if (self) {
        self.locationManager = [[CLLocationManager alloc]init];
        self.locationManager.delegate = self;
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        self.locationManager.distanceFilter = 10;

    }

    return self;
}

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{


    self.location = [locations lastObject];

    [self setCurrentLocation:self.location];

    NSLog(@"self.location in didupdatelocation %@", self.location);

    [self.locationManager stopUpdatingLocation];

}

ViewController.m(在其中调用startUpdating)

- (void)refresh:(UIRefreshControl *)refreshControl {


    LPLocationManager *locationObject = [LPLocationManager sharedManager];
    NSLog(@"location object %@", locationObject);
    [locationObject.locationManager requestAlwaysAuthorization];
    NSLog(@"locationManager %@", locationObject.locationManager);
    [locationObject.locationManager startUpdatingLocation];
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(getLocation:) name:@"locationNotification" object:nil];

    [refreshControl endRefreshing];

}

弄清楚了。

在模拟器中,转到设置->常规,然后滚动到重置。 单击重置位置和隐私。

关闭模拟器并重新运行应用程序。 返回“设置”,转到“隐私”->“位置”,然后为应用选择“始终”。

将这些键值添加到您的info.plist文件中

<key>NSLocationWhenInUseUsageDescription</key>
<string></string>
<key>NSLocationAlwaysUsageDescription</key>
<string></string>

在此之后定义此宏

#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)

并将此代码放入viewWillAppear

locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    if(IS_OS_8_OR_LATER) {
        [locationManager requestAlwaysAuthorization];
    }

[locationManager startUpdatingLocation];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM