didUpdateLocation方法从不调用

Aks*_*her 7 iphone cllocationmanager cllocation ios4 ios

我正在iphone sdk4.0上创建一个应用程序.在那里,确实没有调用更新位置方法.我在下面给出了我的代码.请帮助.谢谢.

-(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];
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*ass 18

此外,在iOS8中,你必须有两件额外的东西:

  • 在Info.plist中添加一个密钥,并向位置管理员请求授权以启动它.

    • 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];
Run Code Online (Sandbox Code Playgroud)

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


Muj*_*key 5

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

在你的init:尝试检查是否有locationServicesEnabled.

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

其他原因,您可能不允许获取您的应用程序的位置.解决方案:只需从iPhone中删除您的应用程序并重新构建,现在应该弹出对话框以允许位置.

用它来检查错误

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

否则一切似乎都没问题,你已经运行ios 4.0,可以安装在iPhone 3G及更高版本,如果是iPhone 2g,那么可能会出现这个问题.