Mac上的iOS 7模拟器不适用于自定义位置(也不要求权限) [英] iOS 7 Simulator on Mac doesn't work with custom location (Also doesn't ask permission)

查看:150
本文介绍了Mac上的iOS 7模拟器不适用于自定义位置(也不要求权限)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图制作一个使用设备当前位置的iOS7应用程序。我在我的Mac上使用iPhone模拟器,但我遇到了一些问题。每当我看到位置管理器出现时,即使在设置了自定义位置(从模拟器>调试>位置)后,它也会打印出0.000000的经纬度。

另外,模拟器在打开应用程序时没有要求使用当前位置的权限似乎很奇怪。有人知道这里发生了什么?

   - (void)viewDidLoad 
{
[super viewDidLoad];
//加载视图后通常从笔尖执行任何其他设置。
[super viewDidLoad];
CLLocationManager * locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone; //每当我们移动
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[locationManager startUpdatingLocation];

_location = [locationManager位置];


_coord.longitude = _location.coordinate.longitude;
_coord.latitude = _location.coordinate.latitude;

$ b $ - (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
_coord.longitude = _location.coordinate.longitude;
_coord.latitude = _location.coordinate.latitude;
printf(%f\\\
,self.coord.longitude);
printf(%f\\\
,self.coord.latitude);


解决方案

您需要获取newLocation从委托方法didUpdateLocationToLocation:fromLocation :.还实现didFailWithError委托方法。您需要一段时间才能开始获取更新位置,因此需要进行委托调用。



通常会缓存最后一个位置,因此可能需要检查位置的时间戳并过滤旧的位置地点。

编辑:



这是我可以提供的最干净的例子。在Xcode中启动新项目,选择Single View应用程序模板iPhone。不要触摸故事板,只需用您的ViewController.m替换内容并在模拟器或设备中运行即可。如果在模拟器上,进入调试并设置一些位置,您将获得控制台中的坐标。

  #importViewController.h$ b我也在启动和停止位置更新。 $ b #import< CoreLocation / CoreLocation.h> 

@interface ViewController()< CLLocationManagerDelegate>

@property(强,非原子)CLLocationManager * locationManager;

@end

@implementation ViewController

#pragma mark - 位置管理器委托方法

- (void)locationManager :(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
$ b $ if([newLocation.timestamp timeIntervalSinceNow]> = -300.0){

NSLog(@更新位置,纬度%f经度%f,newLocation.coordinate.longitude,newLocation.coordinate.latitude);


$ b - (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];

[self.locationManager startUpdatingLocation];
}

- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];

[self.locationManager stopUpdatingLocation];

$ b - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
if(error.code == kCLErrorDenied){

//提醒用户
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@禁止访问位置服务
message:@您可以在设置中打开位置服务 - > ;隐私 - >位置服务
委托:无
cancelButtonTitle:@OK
otherButtonTitles:nil];
[alertView show];

} else if(error.code == kCLErrorLocationUnknown){
NSLog(@Error:location unknown);
} else {
NSLog(@Error retrieve location);


$ b $#bragma mark - Location Manager getter

- (CLLocationManager *)locationManager
{
if( !_locationManager){
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
_locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
_locationManager.distanceFilter = 60.0;
}
return _locationManager;
}

@end


I'm trying to make an iOS7 app that uses the current location of the device. I'm using the iPhone simulator on my Mac, but I'm having some problems. Every time my view that the location manager is in appears, it prints out 0.000000 for both latitude and longitude, even after I've set a custom location (from simulator>debug>location).

Also, it seemed strange that the simulator didn't ask for permission to use current location when it opened the app. Anybody know what's going on here?

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [super viewDidLoad];
    CLLocationManager *locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
    [locationManager startUpdatingLocation];

    _location = [locationManager location];


    _coord.longitude = _location.coordinate.longitude;
    _coord.latitude = _location.coordinate.latitude;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    _coord.longitude = _location.coordinate.longitude;
    _coord.latitude = _location.coordinate.latitude;
    printf("%f\n",self.coord.longitude);
    printf("%f\n",self.coord.latitude);
}

解决方案

You need to get the newLocation from the delegate method didUpdateLocationToLocation:fromLocation:. Also implement didFailWithError delegate method. It takes some time before you start getting updated locations, hence the delegate call.

The last location is usually cached, so it maybe wise to check location's timestamp and filter the old location out.

Edit:

This is the cleanest example I can provide. Start new project in Xcode, pick Single View application template, iPhone. Don't touch storyboard, just replace content of your ViewController.m with this and run in Simulator or device. If on Simulator, go to Debug and set some location and you will get coordinates in the console. I am also starting and stopping location updates when the view goes on or off screen.

#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>

@interface ViewController () <CLLocationManagerDelegate>

@property (strong, nonatomic) CLLocationManager *locationManager;

@end

@implementation ViewController

#pragma mark - Location Manager delegate methods

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

    if ([newLocation.timestamp timeIntervalSinceNow] >= -300.0) {

        NSLog(@"updated location with latitude %f longitude %f", newLocation.coordinate.longitude, newLocation.coordinate.latitude);
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [self.locationManager startUpdatingLocation];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

    [self.locationManager stopUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error     {
    if(error.code == kCLErrorDenied) {

        // alert user
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Access to location services is disabled"
                                                        message:@"You can turn Location Services on in Settings -> Privacy -> Location Services"
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alertView show];

    } else if(error.code == kCLErrorLocationUnknown) {
        NSLog(@"Error: location unknown");
    } else {
        NSLog(@"Error retrieving location");
    }
}

#pragma mark - Location Manager getter

- (CLLocationManager *)locationManager
{
    if (!_locationManager) {
        _locationManager = [[CLLocationManager alloc] init];
        _locationManager.delegate = self;
        _locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
        _locationManager.distanceFilter = 60.0;
    }
    return _locationManager;
}

@end

这篇关于Mac上的iOS 7模拟器不适用于自定义位置(也不要求权限)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆