当前位置代码在iOS 5中不起作用,但在iOS 6中工作正常

时间:2013-04-18 07:22:30

标签: objective-c ios5 xcode4.5

我已下载了一个当前位置示例项目,该项目在 iOS 5 iOS 6 中均正常运行。当我将相同的文件复制到我的另一个项目时,它在 iOS 5 中停止工作,但在 iOS 6 中工作正常。我收到错误

didFailWithError: Error Domain=kCLErrorDomain Code=1 "The operation couldn’t be completed. (kCLErrorDomain error 1.)

我的代码是:

·H

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController : UIViewController<CLLocationManagerDelegate>
{
    CLLocationManager *locationManager;
    CLGeocoder *geocoder;
    CLPlacemark *placemark;
}
@property (weak, nonatomic) IBOutlet UILabel *lblLat;
@property (weak, nonatomic) IBOutlet UILabel *lblLong;
@property (weak, nonatomic) IBOutlet UILabel *lblAddress;

@end

的.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    locationManager = [[CLLocationManager alloc] init];
    geocoder = [[CLGeocoder alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    [locationManager startUpdatingLocation];

}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"didFailWithError: %@", error);
    UIAlertView *errorAlert = [[UIAlertView alloc]
                               initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [errorAlert show];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation

{
    NSLog(@"didUpdateToLocation: %@", newLocation);
    CLLocation *currentLocation = newLocation;

    if (currentLocation != nil)
    {
        lblLong.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
        lblLat.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
    }

    // Reverse Geocoding
    NSLog(@"Resolving the Address");
    [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
        NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
        if (error == nil && [placemarks count] > 0)
        {
            placemark = [placemarks lastObject];
            lblAddress.text = [NSString stringWithFormat:@"%@ %@\n%@ %@\n%@\n%@",
                                 placemark.subThoroughfare, placemark.thoroughfare,
                                 placemark.postalCode, placemark.locality,
                                 placemark.administrativeArea,
                                 placemark.country];
        } else {
            NSLog(@"%@", error.debugDescription);
        }
    } ];

}

任何人都可以告诉问题在哪里?我使用的是Xcode 4.5,iOS部署目标是 iOS 5

感谢。

1 个答案:

答案 0 :(得分:-1)

我不确定为什么它在iOS6中工作,因为以下委托方法已被弃用。

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation

您可以尝试将其替换为:

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations

同样根据文档kCLErrorDomain Code=1表示: kCLErrorLocationUnknown

您是否通过XCode模拟您的位置?