2013-12-19 24 views
0

我想獲取用戶位置在我的iOS應用程序中點擊一個按鈕。我正在使用下面的代碼來啓動位置管理器。iOS應用程序要求用戶兩次打開位置服務,當試圖訪問與位置服務關閉的位置

locationManager = [[CLLocationManager alloc] init]; 
locationManager.delegate = self; 
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer; 
locationManager.distanceFilter = 1000; 

if ([CLLocationManager locationServicesEnabled]) { 
    [locationManager startUpdatingLocation]; 
} 
else{ 
    UIAlertView *alert= [[UIAlertView alloc]initWithTitle:@"Error" message:@"The location  services seems to be disabled from the settings." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil]; 
    [alert show]; 
    alert= nil; 
} 

當位置服務打開並且用戶點擊按鈕時,它工作正常,位置正確提取。

但是,當位置服務關閉,用戶嘗試點擊按鈕,按照我的代碼,他的警報應顯示如上。但是在到達其他循環之前,iOS會顯示如下的系統級彈出窗口。

enter image description here

在這個系統級警報視圖的頂部,是越來越顯示我自己的警報視圖。這很令人沮喪。問題是,如果用戶取消系統級別彈出窗口而不是設置並嘗試點擊按鈕,則第二次同樣的事情(兩個警報場景)正在發生。但是,如果用戶繼續執行相同的步驟,則系統級警報現在不顯示。這意味着上面顯示的系統級別彈出窗口僅出現2次,之後沒有任何事情發生。如何處理這種情況。請幫忙。

+0

您是否找到解決方案? – RashmiG

回答

-1

我對此不確定。但是,無論用戶接受還是拒絕訪問其當前位置的請求,都會調用協調回調locationManager:didChangeAuthorizationStatus:。有關更多詳細信息,請參閱this

+0

但在這種情況下,這有什麼幫助? –

1

通常一個會寫:

if ([CLLocationManager locationServicesEnabled]) 
{ 
    [locationManager startUpdatingLocation]; 
} 

,讓iOS和用戶排序出來。僅僅撥打[CLLocationManager locationServicesEnabled]會觸發系統對話框(第一次)。下面的代碼是未經測試,但應該工作,你希望它的方式:

if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized) 
{ 
    if ([CLLocationManager locationServicesEnabled]) 
    { 
     [locationManager startUpdatingLocation]; 
    } 
} 
else 
{ 
    UIAlertView *alert= [[UIAlertView alloc]initWithTitle:@"Error" message:@"The location  services seems to be disabled from the settings." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil]; 
    [alert show]; 
    alert= nil; 
} 

編輯:

我最後的代碼將是用戶的結果甚至不會得到提示啓用位置的更新你的應用。您不妨做到以下幾點:

if ([[NSUserDefaults standardUserDefaults] objectForKey:@"userWasAskedForLocationOnce"]) 
{ 
    if ([CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorized) 
    { 
     UIAlertView *alert= [[UIAlertView alloc]initWithTitle:@"Error" 
                 message:@"The location services seems to be disabled from the settings." 
                delegate:nil 
              cancelButtonTitle:@"Ok" 
              otherButtonTitles:nil]; 
     [alert show]; 
     alert= nil; 
    } 
} 
else 
{ 
    [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:YES] 
               forKey:@"userWasAskedForLocationOnce"]; 

    if ([CLLocationManager locationServicesEnabled]) //this will trigger the system prompt 
    { 
     [locationManager startUpdatingLocation]; 
    } 
} 

在這種情況下,用戶會得到系統提示,以使首次位置更新。如果他們啓用它們,那麼他們以後就不會得到任何提示。如果沒有,他們會在後續啓動時收到您的自定義警告 - 直到他們在設置應用中啓用位置更新。

注1:測試此代碼,您可能想從時間刪除設備的應用時間(用戶默認是持久的,直到您刪除的應用程序)

注2:儘管此代碼應該達到預期行爲可能會讓用戶惱火。您的用戶界面應該安排好,以便用戶知道位置服務被禁用 - 如果這部分代碼在啓動時自動執行,警報有點矯枉過正。如果它是在某個用戶的操作之後執行的(點擊一個按鈕),那麼可以顯示警報。

+0

如果我顛倒下面的if循環,結果會不同嗎?如果([CLLocationManager locationServicesEnabled] == kCLAuthorizationStatusAuthorized) if([CLLocationManager authorizationStatus]) {locationManager startUpdatingLocation]; } } else UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@「錯誤」消息:@「位置服務似乎從設置被禁用。委託人:無cancelButtonTitle:@「Ok」otherButtonTitles:nil]; [alert show]; alert = nil; } –

+1

我做了一些測試並更新了答案。 –