2012-06-16 27 views
2

基本上,當我的應用首次啓動時,出現啓用位置服務提示。當用戶點擊時允許我想開始更新用戶位置並放大該區域。如何在用戶選擇允許後處理啓用位置服務?

在我viewDidLoad中,我開始外景經理,但不幸的是,用戶有機會挖掘上允許前視圖被加載。因爲用戶將允許定位服務,一切工作在第二次發射該應用程序的罰款已經

我的問題是我怎麼能捕捉攻上允許這樣我就可以運行代碼放大到一個地區的事件?

我一直在使用-(void)locationManager:didChangeAuthorizationStatus:嘗試,但它似乎並沒有當用戶點擊允許調用此委託方法。

希望這是有道理的,我對此很新。

回答

5

據我所知,你不能,但你沒有捕捉到這一事件,因爲你將無法得到你這個位置的座標之前,放大到一定的位置。您的應用在第二次啓動時工作良好,因爲它使用第一次啓動時緩存的位置數據。因此,您需要的是在收到新的有效座標後運行縮放代碼。 如果使用CLLocationManager,比其委託看看

– locationManager:didUpdateToLocation:fromLocation: 

。如果用戶拒絕使用位置服務,您的代理人將收到

locationManager:didFailWithError: 

與相應的錯誤。

如果使用MKMapKit,比的MKMapView委託實現方法

– mapViewWillStartLocatingUser: 

專注於當前用戶的位置。 處理拒絕執行

– mapView:didFailToLocateUserWithError: 

鏈接到相應的文件蘋果:

CLLocationManager

CLLocationManagerDelegate

MKMapViewDelegate

1

這工作得很好。我啓動位置管理器,然後將其設置爲委託並啓動它。當允許彈出窗口出現時,-(void)locationManager:didChangeAuthorizationStatus:被調用,CLAuthorizationStatus等於kCLAuthorizationStatusNotDetermined。如果點擊「不允許」,則再次調用CLAuthorizationStatus等於kCLAuthorizationStatusDenied。點擊「允許」時,會調用CLAuthorizationStatus等於kCLAuthorizationStatusAuthorized。檢查您的委託是否設置正確。

1

你能應付這樣的:

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

    switch([CLLocationManager authorizationStatus]) 
    { 
     case kCLAuthorizationStatusAuthorized: 
      NSLog(@"Location services authorised by user"); 
      break; 

     case kCLAuthorizationStatusDenied: 
      NSLog(@"Location services denied by user"); 
      break; 

     case kCLAuthorizationStatusRestricted: 
      NSLog(@"Parental controls restrict location services"); 
      break; 

     case kCLAuthorizationStatusNotDetermined: 
      NSLog(@"Unable to determine, possibly not available"); 
      break; 
    } 
} 
相關問題