2013-03-27 59 views
3

我正在使用iOS 6.1 iPhone app而啓用了ARC,它將使用CLLocationManager來跟蹤設備的位置。CLLocationManager不會將位置發送到didUpdateLocations方法

UPDATE

我有一個DestinationViewController它實現了CLLocationManagerDelegate。它會創建CLLocationManager並執行設置。我已將CoreLocation framework導入到我的項目中。

我試圖調試很多,可以看到CLLocationManager已創建且沒有錯誤。但問題是在[self.locationManager startUpdatingLocation];之前的[CLLocationManager authorizationStatus]kCLAuthorizationStatusNotDetermined 以及之後的。 因此,沒有提示要求爲此應用使用位置服務的權限。因爲這個方法從來沒有被解僱。

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

我也嘗試在網上搜索幾個小時尋找類似的例子,但沒有運氣。來自DestinationViewController的代碼發佈在下面。

接口

#import <UIKit/UIKit.h> 
#import <CoreLocation/CoreLocation.h> 

@interface DestinationViewController : UIViewController <CLLocationManagerDelegate> 

實施

#import "DestinationViewController.h" 

@interface DestinationViewController() 
@property (strong, nonatomic) CLLocationManager *locationManager; 
@end 


@implementation DestinationViewController 

// Initializer 
- (CLLocationManager *)locationManager 
{ 
    if (!_locationManager) { 
     _locationManager = [[CLLocationManager alloc] init]; 
    } 
    return _locationManager; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
    [self startLocation]; 
} 


- (void)startLocation 
{ 
    self.locationManager.delegate = self; 
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    self.locationManager.distanceFilter = 1; 

    NSString *error; 
    if (![CLLocationManager locationServicesEnabled]) { 
     error = @"Error message"; 
    } 

    CLAuthorizationStatus status = [CLLocationManager authorizationStatus]; 
    if (status == kCLAuthorizationStatusRestricted || 
     status == kCLAuthorizationStatusDenied ||) { 
     error = @"Error message"; 
    } 

    if (error) { 
     NSLog(error); 
    } 
    else 
    { 

     status = [CLLocationManager authorizationStatus]; 

    self.locationManager.pausesLocationUpdatesAutomatically = NO; 

     [self.locationManager startUpdatingLocation]; 

     status = [CLLocationManager authorizationStatus]; 

     NSLog(@"CLLocationManager is %@", self.locationManager); 
     NSLog(@"Location is %@", self.locationManager.location); 

     [self updateUI]; 
    } 
} 


- (void)locationManager:(CLLocationManager *)manager 
    didUpdateLocations:(NSArray *)locations 
{ 
    CLLocation *recentLocation = [locations lastObject]; 
    NSLog(@"Found location"); 
} 

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error 
{ 
    NSLog(@"didFailWithError"); 
} 

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status 
{ 
    NSLog(@"Change in authorization status"); 
} 
+0

這是模擬器還是設備?如果模擬器,您是否允許XCode模擬該位置? – 2013-03-27 10:50:06

+0

已經嘗試過。我創建了一個自定義位置,並試圖執行高速公路驅動也 – 2013-03-27 11:12:12

+0

你可以嘗試執行不推薦的'locationManager:didUpdateToLocation:fromLocation:'並看看是否被調用? – 2013-03-27 11:13:56

回答

-14

我終於解決了!我的代表沒有正確設置。 而不是做的:

self.locationManager.delegate = self;

我把它改爲:

[self.locationManager setDelegate:self];

現在我的委託方法被解僱。

感謝那些幫助!

+0

我從來沒有遇到過這個問題:/ – yuf 2013-12-12 23:05:25

+7

兩者都是一樣的。 – 2013-12-16 13:20:54

+3

我也(也)懷疑這與它有什麼關係。 – Drux 2014-01-31 11:18:53

7

您的代碼應該工作。

確保您的應用程序被授權使用位置服務。

您可以使用下面的方法來檢查代碼授權狀態

+ (CLAuthorizationStatus)authorizationStatus 

編輯

kCLAuthorizationStatusNotDetermined很可能是由於位置服務被關閉了。您應該首先檢查位置服務狀態。代碼應該是這樣的

if([CLLocationManager locationServicesEnabled]) { 
    // Location Services Are Enabled 
    switch([CLLocationManager authorizationStatus]) { 
     case kCLAuthorizationStatusNotDetermined: 
      // User has not yet made a choice with regards to this application 
      break; 
     case kCLAuthorizationStatusRestricted: 
      // This application is not authorized to use location services. Due 
      // to active restrictions on location services, the user cannot change 
      // this status, and may not have personally denied authorization 
      break; 
     case kCLAuthorizationStatusDenied: 
      // User has explicitly denied authorization for this application, or 
      // location services are disabled in Settings 
      break; 
     case kCLAuthorizationStatusAuthorized: 
      // User has authorized this application to use location services 
      break; 
    } 
} else { 
    // Location Services Disabled 
} 
+0

感謝您的回答。我做了以下事情: \t CLAuthorizationStatus status = [CLLocationManager authorizationStatus]; 而在調試器中,我可以看到它是kCLAuthorizationStatusAuthorized。所以它應該可以工作,但該方法仍然不會被調用。 – 2013-03-27 11:09:49

+0

調試了很多後,我可以看到,當我在iPhone 5上運行應用程序時,在調用'startUpdatingLocation'之前和之後,其'CLAuthorizationStatus'都設置爲'kCLAuthorizationStatusNotDetermined'。怎麼沒有警告顯示給用戶? – 2013-03-27 17:46:53

+0

感謝澄清;我已經更新了我的答案以涵蓋此內容。 – 2013-03-28 08:29:12