didUpdateLocation方法从未调用

我正在iphone sdk4.0上做一个应用程序。在那个更新的位置方法从来没有调用。 我已经给我的代码below.Please help.Thanks提前。

-(id)init { [super init]; obj=[[UIApplication sharedApplication]delegate]; locationManager = [[CLLocationManager alloc] init]; locationManager.delegate = self; //locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m [locationManager startUpdatingLocation]; return self; } -(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { NSLog(@"%f",newLocation.coordinate.latitude); NSLog(@"%f",newLocation.coordinate.longitude); obj=[[UIApplication sharedApplication]delegate]; location=[[CLLocation alloc]initWithLatitude:newLocation.coordinate.latitude longitude:newLocation.coordinate.longitude]; obj.lattitude1=[NSString stringWithFormat:@"%f",newLocation.coordinate.latitude]; obj.longitude1=[NSString stringWithFormat:@"%f",newLocation.coordinate.longitude]; //location=[[CLLocation alloc]initWithLatitude:39.9883 longitude:-75.262227]; } 

嗨,你的代码似乎没问题。 现在这些可能是可能的原因:

在你的init:尝试检查是否有locationServicesEnabled或不。

 locationManager = [[CLLocationManager alloc] init]; if(locationManager.locationServicesEnabled == NO){ //Your location service is not enabled, Settings>Location Services } 

其他原因,你可能不允许获取你的应用程序的位置。 解决scheme:只需从iPhone中删除您的应用程序并重新构build,现在应该popup对话框允许位置。

用这个来检查错误

 - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { NSLog(@"Error while getting core location : %@",[error localizedFailureReason]); if ([error code] == kCLErrorDenied) { //you had denied } [manager stopUpdatingLocation]; } 

否则一切似乎确定,你已经运行的ios 4.0,它可以安装在iPhone 3G和以后,如果是iPhone 2g,那么这个问题可能会发生。

另外在iOS8中,你必须有两件事:

  • 将一个密钥添加到Info.plist,并请求位置pipe理器的授权,要求它开始。

    • NSLocationWhenInUseUsageDescription

    • NSLocationAlwaysUsageDescription

  • 您需要为相应的位置方法请求授权。

    • [self.locationManager requestWhenInUseAuthorization]

    • [self.locationManager requestAlwaysAuthorization]

代码示例:

 self.locationManager = [[CLLocationManager alloc] init]; self.locationManager.delegate = self; // Check for iOS 8. Without this guard the code will crash with "unknown selector" on iOS 7. if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) { [self.locationManager requestWhenInUseAuthorization]; } [self.locationManager startUpdatingLocation]; 

来源: http : //nevan.net/2014/09/core-location-manager-changes-in-ios-8/

确保你添加了CLLocationManager作为一个属性。

@属性(非primefaces,强大)CLLocationManager * locationManager;

如果你正在模拟器上testing它,它将无法正常工作。

发生这种情况是因为该方法仅在设备更改位置时被调用(并且模拟器从不更改位置)。

-locationManager:didFailWithError:在你的委托有没有被调用? 也许您在某个时候拒绝了对位置数据的访问,现在不会提示,但访问被拒绝。

在您allocinit您的位置pipe理器之后,请检查位置服务是否被允许。 此代码来自Apple的“LocateMe”代码示例

 if (locationManager.locationServicesEnabled == NO) { UIAlertView *servicesDisabledAlert = [[UIAlertView alloc] initWithTitle:@"Location Services Disabled" message:@"You currently have all location services for this device disabled. If you proceed, you will be asked to confirm whether location services should be reenabled." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [servicesDisabledAlert show]; [servicesDisabledAlert release]; } 

如果您使用模拟器进行testing,请尝试使用设备进行testing。 模拟器需要打开计算机的WiFi,并且可以find位于已知位置的数据库中的WiFi基站。