【发布时间】:2023-03-31 01:20:02
【问题描述】:
所以当我使用 WiFi 时它可以工作。但是在 4G 上,它只有在我使用 Wifi 并且已经有了位置的情况下才有效。很多时候没有 WiFi,手机会说它正在使用我的位置,但它没有更新标签,也没有将坐标上传到服务器。代码如下:
- (void)viewDidLoad {
[super viewDidLoad];
if (nil == locationManager)
locationManager = [[CLLocationManager alloc] init];
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
if ([self->locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[self->locationManager requestWhenInUseAuthorization];
}
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
CLLocation *newLocation = [locations lastObject];
NSString *locationLat = [NSString stringWithFormat:@"%.8f", newLocation.coordinate.latitude];
NSString *locationLong = [NSString stringWithFormat:@"%.8f", newLocation.coordinate.longitude];
latLongLabel.text = [NSString stringWithFormat:@"Lat: %@ - Long%@",locationLat,locationLong];
[self postLocation:locationLat secondArg:locationLong];
}
- (void)postLocation: (NSString *)latitudeString secondArg:(NSString *)longitudeString {
//POST COORDINATES TO MY SERVER
}
- (IBAction)startUpdating:(id)sender {
[locationManager startUpdatingLocation];
}
【问题讨论】:
-
在进行测试时,您是否会从一个位置移动到另一个位置?如果您离原始位置太近,
CLLocationManager不会向您发送任何位置更新。另外,您是否在模拟器中测试了您的应用程序?除了 4G/Wi-Fi 网络问题之外,您可以使用 Cupertino 周围的预定义电路来检查您的应用是否按预期工作...... -
我的应用在 SIM 卡上运行,使用像 Apple 这样的静态位置,或者在高速公路上移动。当我使用 Wifi 时,它也适用于我的手机。如果我在启动应用程序并按下 startUpdating() 按钮时使用 WiFi,它会在 100% 的时间在标签上显示我的纬度以及对 postLocation() 的调用。但是,如果我在没有 WiFi 的情况下启动我的应用程序,然后按 startUpdating 按钮,它就不起作用。唯一有效的情况是我最近在使用 WiFi。我没有想法......
-
我也有同样的问题。你发现问题出在哪里了吗?
标签: ios objective-c ios8 core-location