2014-09-25 80 views
0

核心位置未調用didUpdateLocations。我有兩個類涉及LocationManager和一個視圖控制器。 Plist設置爲requestAlwaysAuthorization。位置在調試中模擬。任何人都可以幫助我發現錯誤?使用Xcode 6模擬器的核心位置

LocationManager.h

@interface LPLocationManager : NSObject <CLLocationManagerDelegate> 

+(LPLocationManager*)sharedManager; 

@property (strong, atomic) CLLocationManager *locationManager; 
@property (nonatomic, retain) CLLocation *location; 
@end 

LocationManager.m

+(LPLocationManager*)sharedManager{ 
    static LPLocationManager *sharedManager = nil; 
    static dispatch_once_t onceToken; 
    dispatch_once(&onceToken, ^{ 
     sharedManager = [[LPLocationManager alloc]init]; 
    }); 

    return sharedManager; 
} 

- (id)init 
{ 
    self = [super init]; 
    if (self) { 
     self.locationManager = [[CLLocationManager alloc]init]; 
     self.locationManager.delegate = self; 
     self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
     self.locationManager.distanceFilter = 10; 

    } 

    return self; 
} 

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


    self.location = [locations lastObject]; 

    [self setCurrentLocation:self.location]; 

    NSLog(@"self.location in didupdatelocation %@", self.location); 

    [self.locationManager stopUpdatingLocation]; 

} 

ViewController.m(其中startUpdating叫)

- (void)refresh:(UIRefreshControl *)refreshControl { 


    LPLocationManager *locationObject = [LPLocationManager sharedManager]; 
    NSLog(@"location object %@", locationObject); 
    [locationObject.locationManager requestAlwaysAuthorization]; 
    NSLog(@"locationManager %@", locationObject.locationManager); 
    [locationObject.locationManager startUpdatingLocation]; 
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; 
    [center addObserver:self selector:@selector(getLocation:) name:@"locationNotification" object:nil]; 

    [refreshControl endRefreshing]; 

} 
+0

http://stackoverflow.com/questions/25844430/xcode-6-gm-cllocationmanager/25844674#25844674 – 2014-09-25 05:50:10

回答

1

在您的info.plist添加這些鍵值文件

<key>NSLocationWhenInUseUsageDescription</key> 
<string></string> 
<key>NSLocationAlwaysUsageDescription</key> 
<string></string> 

在此之後定義該宏

#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) 

,並把這個代碼在viewWillAppear中

locationManager.delegate = self; 
locationManager.desiredAccuracy = kCLLocationAccuracyBest; 

    if(IS_OS_8_OR_LATER) { 
     [locationManager requestAlwaysAuthorization]; 
    } 

[locationManager startUpdatingLocation]; 
+0

我做了上述,但它仍然沒有工作。這是模擬器的問題。見下面的答案。 – noobsmcgoobs 2014-09-25 06:00:14

4

想通了。

在模擬器中,轉至設置 - >常規並滾動到重置。點擊重置位置&隱私。

關閉模擬器並重新運行應用程序。回到設置中,轉到隱私 - >位置,然後選擇總是爲應用程序。

+0

仍然無法使用。 – Neeraj 2014-10-15 05:16:46

+0

它顯示了其請求授權,但提示 - (空)的LocationManager:(CLLocationManager *)經理didFailWithError:(NSError *)錯誤 被immedietly打來電話,我得到的錯誤是 錯誤域= kCLErrorDomain代碼= 0 「操作無法完成(kCLErrorDomain錯誤0.)」 – Neeraj 2014-10-15 05:19:34

+0

你試過了嗎? http://stackoverflow.com/questions/24062509/ios-8-location-services-not-working – noobsmcgoobs 2014-10-15 05:19:42