2014-09-04 79 views
0

在我的iOS應用程序中,我有一張表格使用您的位置來查找您附近的數據。我今天剛把它安裝在我的iPhone上,當需要定位服務時,我得到了預期的警報,聲稱「MyAppName想使用你當前的位置」,一旦我按下確定,應用程序基本上就會凍結。然而,當我關閉應用並重新打開它時,它工作得很好。現在我想到了,這也發生在模擬器上,因爲我必須在定位服務正常工作之前運行應用程序幾次。這是否發生在其他人身上?你知道如何解決這個問題嗎?我不希望我的用戶必須關閉應用程序並重新打開它,以使位置服務正常工作。爲什麼位置服務在我允許之後沒有被使用?

下面是一些相關的代碼:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    locationManager = [[CLLocationManager alloc] init]; 
    locationManager.delegate = self; 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    [locationManager startUpdatingLocation]; 
} 
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error 
{ 
    NSLog(@"%@",error.userInfo); 
    if([CLLocationManager locationServicesEnabled]){ 
     NSLog(@"Location Services Enabled"); 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Need Your Location" 
                  message:@"Please go to Settings and turn on Location Services so you can see who's near you." 
                  delegate:nil 
               cancelButtonTitle:@"OK" 
               otherButtonTitles:nil]; 
     [alert show]; 
    } 
} 

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
    NSLog(@"didUpdateToLocation: %@", newLocation); 
    CLLocation *currentLocation = newLocation; 

    if (currentLocation != nil) { 
     _thisGeoPoint = [PFGeoPoint geoPointWithLatitude:currentLocation.coordinate.latitude 
              longitude:currentLocation.coordinate.longitude]; 
    } 
    [locationManager stopUpdatingLocation]; 
    located = true; 
} 
+1

你需要通過你的代碼在調試器中的步驟或添加日誌記錄,以找出在那裏卡住,但幾個百分點 - 的'didUpdateToLocation:fromLocation'方法已棄用,您應該使用'didUpdateLocations',並且可能並不是一個好辦法,只要您獲取位置就停止更新位置,因爲位置準確性可以隨着時間的推移而得到改善 - 根據GPS的狀態,第一次修復可能非常不準確/位置服務 – Paulw11 2014-09-05 00:13:26

回答

1

這是發生,因爲的LocationManager停止運行時,它最初沒有權限。因此,一旦您檢測到權限已更改,您將需要重新運行它。您可以使用didChangeAuthorizationStatus delegate method來檢查權限是否已更改。

因此,像這樣:

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status 
{ 
    if (status == kCLAuthorizationStatusAuthorized) { 
     [locationManager startUpdatingLocation]; 
    } 
} 
+0

這不是我的經驗。當權限被授予時調用'didChangeAuthorizationStatus',但不需要再次調用'startUpdatingLocation' – Paulw11 2014-09-05 01:04:30

+0

didChangeAuthorizationStatus在顯示警告時被調用,但當我按下「Allow」時沒有被調用,所以我得到相同的結果我以前得到。任何想法爲什麼發生@ Paulw11? – user3200744 2014-09-10 01:24:59

+0

不是。您正在運行的是哪個版本的iOS?您是否嘗試過從設備中刪除應用並重新安裝?我發現隱私設置可能會搞砸。 – Paulw11 2014-09-10 01:26:32

相關問題