2013-04-18 65 views
0

罰款我已經下載了這兩個的iOS 5的iOS 6做工精細的當前位置的樣本項目。當我將相同的文件複製到另一個項目時,它在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

謝謝。

+0

您是否檢查過該應用是否有權限獲取位置更新?我的意思是從手機設置中的隱私部分 – haynar 2013-05-09 13:39:19

回答

-1

我不確定爲什麼它在iOS6中工作,因爲下面的委託方法已被棄用。

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

你可以試着用來替換:

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

而且根據文檔kCLErrorDomain Code=1含義: kCLErrorLocationUnknown

你通過了XCode模擬您的位置?

+1

棄用並不意味着該方法不會在iOS 6中運行,這意味着它應該在即將到來的iOS版本中刪除,可能是7甚至8,無論如何,如果你是計劃用iOS 5支持設備時,您必須使用已棄用的方法,因爲第二種方法在iOS 6及更高版本中可用 – haynar 2013-05-09 13:37:26

相關問題