【发布时间】:2014-09-04 23:53:45
【问题描述】:
在我的 iOS 应用程序中,我有一个表格,它使用您的位置来查找您附近的数据。我今天刚刚将它安装在我的 iPhone 上,当需要定位服务时,我收到了预期的警报,上面写着“MyAppName 想使用您当前的位置”,一旦我按下确定,应用程序就基本冻结了。但是,当我关闭应用程序并重新打开它时,它运行得非常好。现在我想起来了,这也发生在模拟器上,因为我必须运行该应用程序几次,然后位置服务才能在那里正常工作。其他人是否发生过这种情况,您知道如何解决吗?我不希望我的用户必须关闭应用并重新打开它才能使定位服务正常工作。
以下是一些相关代码:
- (void)viewDidLoad
{
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"%@",error.userInfo);
if([CLLocationManager locationServicesEnabled]){
NSLog(@"Location Services Enabled");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Need Your Location"
message:@"Please go to Settings and turn on Location Services so you can see who's near you."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"didUpdateToLocation: %@", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
_thisGeoPoint = [PFGeoPoint geoPointWithLatitude:currentLocation.coordinate.latitude
longitude:currentLocation.coordinate.longitude];
}
[locationManager stopUpdatingLocation];
located = true;
}
【问题讨论】:
-
您需要在调试器中单步调试您的代码或添加日志记录以找出卡住的位置,但有几点 -
didUpdateToLocation:fromLocation方法已弃用,您应该使用didUpdateLocations并且一旦获得位置就停止更新位置可能不是一个好主意,因为位置准确性会随着时间的推移而提高 - 第一次修复可能会非常不准确,具体取决于 GPS/位置服务的状态
标签: ios objective-c core-location cllocationmanager