2012-02-02 25 views
2

CLLocationManager提示警報如何知道CLLocationManager使用哪個警報按鈕?

「應用名稱」 你想使用你的位置

它提供了兩個按鈕,OK不允許。如何知道用戶選擇了哪些按鈕?

+0

爲什麼你不檢查你是否有權訪問用戶的位置?如果你這樣做,他選擇確定。 – Kheldar 2012-02-02 08:45:34

回答

5

當您單擊不允許按鈕

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
kCLAuthorizationStatusDenied異常調用。你可以寫在裏面。

另請參考:

kCLAuthorizationStatusNotDetermined = 0, // User has not yet made a choice with regards to this application 
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 
kCLAuthorizationStatusDenied,   // User has explicitly denied authorization for this application, or 
              // location services are disabled in Settings 
kCLAuthorizationStatusAuthorized   // User has authorized this application to use location services 

實施例:

如果上用戶點擊允許然後

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
     [self refreshYourView]; 
} 

如果點擊不允許

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error 
{ 
    if ([error code]== kCLAuthorizationStatusDenied) 
    { 
     UIAlertView *alert; 
    alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"User has clicked don't allow button." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
     [alert show]; 
     [alert release]; 

    } 
} 

編輯

備用:您可以顯示一個警報,要求用戶允許通過啓用位置服務從Settings進行位置訪問。在您的應用程序

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs://"]]; 

打開設置應用:

您可以在iOS 5.0和更高版本使用。

+0

我在應用程序中這樣做了兩次。一旦進入appdelegate,然後進入我有mapview的控制器之一。如果用戶在appdelegate CLLocationManager警報中選擇「確定」,則應用程序不應在ma控制器內提示此警報。但是,如果用戶選擇不允許在開始,應用程序應該在地圖控制器內提示此警報。這個怎麼做? – Nitish 2012-02-02 08:55:45

+0

@Nitish:當用戶選擇不允許並在地圖控制器中檢查該標誌時設置一個標誌。如果標誌爲假,則請求用戶允許。 – Maulik 2012-02-02 09:00:51

+0

這不是我所要求的。如果用戶選擇不允許在應用程序啓動時,CLLocationManager應該在地圖控制器中提示相同的警報。如果用戶在開始時選擇了「確定」,則地圖控制器中不應出現警報。 – Nitish 2012-02-02 09:03:55

4

實施CLLocationManagerDelegate Protocol

的LocationManager:didChangeAuthorizationStatus:

告訴該應用程序的授權狀態改變的委託。

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status 

參數

經理

The location manager object reporting the event. 

狀態

The new authorization status for the application. 

討論

這種方法被稱爲每當應用程序的能力,以使用位置SERVIC es變化。 可能會發生更改,因爲用戶允許或拒絕爲您的應用程序或整個系統使用位置服務

相關問題