【问题标题】:Testing a fake current location on iOS Simulater is not working在 iOS 模拟器上测试虚假的当前位置不起作用
【发布时间】:2014-10-18 14:52:12
【问题描述】:

我一直试图在 iOS 模拟器中伪造我的位置,但我一直在阅读的方法不起作用。我已经使用调试器设置了我的假位置,并在 Debug->Location->Custom Location 中创建了一个自定义位置,但我仍然记录 0.000 0.000 的经纬度。 我正在使用此代码查找当前位置。

- (IBAction)currentLocationButtonPressed:(UIButton *)sender
{
    //Search for pubs and bars in current location.
    //Push to tableViewController
    NSLog(@"current location Pressed.");

    locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; // Best accuracy
    [locationManager startUpdatingLocation];
    NSLog(@"%@",[self deviceLocation]);
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"didUpdateToLocation: %@", newLocation);
    CLLocation *currentLocation = newLocation;

    if (currentLocation != nil) {
        NSLog(@"Latitude: %f Longitude: %f", currentLocation.coordinate.latitude, currentLocation.coordinate.longitude);
    }
}

- (NSString *)deviceLocation {
    return [NSString stringWithFormat:@"latitude: %f longitude: %f", locationManager.location.coordinate.latitude, locationManager.location.coordinate.longitude];
}

委托已设置,我正在请求授权。

- (void)viewDidLoad {
    [super viewDidLoad];

    searchLocationTextField.delegate = self;
    locationManager.delegate = self;
    locationManager = [[CLLocationManager alloc]init];
    [locationManager requestWhenInUseAuthorization];  // For foreground access
    [locationManager requestAlwaysAuthorization]; // For background access

}

【问题讨论】:

  • 在运行模拟器时,您还可以从 xcode 模拟当前位置: 'Debug' -> 'Simulate Location' 。你试过吗?
  • 是的,我试过了。没用。 @AsifAsif
  • 我认为从这个意义上说,对当前位置进行存根会更有意义。

标签: ios objective-c geolocation ios-simulator


【解决方案1】:

在分配后将 locationManager 委托设置为。

locationManager = [[CLLocationManager alloc]init];
locationManager.delegate = self;

locationManager:didUpdateToLocation:fromLocation: 委托方法已弃用,请改用locationManager:didUpdateLocations:

-(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *currentLocation = [locations lastObject];

    NSLog(@"Latitude: %f Longitude: %f", currentLocation.coordinate.latitude, currentLocation.coordinate.longitude);
} 

【讨论】:

  • 是的,这似乎更有意义,但是 NSLog(@"didUpdateToLocation: %@", [locations lastObject]);甚至不会登录到控制台。
  • 即使在更新之后,currentLocation 也不会记录到控制台。似乎该方法甚至没有被调用。
  • 另外,你需要在分配locationManager后设置委托,而不是其他方式。答案已更新。
  • 我以为就是这样。仍然没有登录到控制台。
  • 你有测试设备吗?位置委托并不总是在模拟器中被解雇。我从 Xcode 5 开始就习惯了这种行为。
猜你喜欢
  • 1970-01-01
  • 2014-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-21
  • 1970-01-01
相关资源
最近更新 更多