2014-11-24 23 views
0

我想在獲取用戶位置之前獲得用戶授權。我使用此代碼爲:獲取用戶位置警報不停留在屏幕上的iOS 8

 self.locationManager = [[CLLocationManager alloc] init]; 
     [self.locationManager setDelegate:self]; 
     if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) { 
      // iOS8+ 
      // Sending a message to avoid compile time error 
      [self.locationManager requestAlwaysAuthorization]; 
     } 
     [self showIndicatorView]; 
     self.getCityService = [[GetCitiesService alloc] initServiceWithDelegate:self isLocalCall:NO]; 
     [self.getCityService fetchCities]; 

我看到屏幕上的警報,但之前,我允許它與否,它消失形式的屏幕和應用程序沒有被授權。

我想我的代碼停止,直到用戶給予權限。

回答

1

您的代碼看起來很奇怪,因爲您似乎要撥打requestAlwaysAuthorization兩次。一旦登錄self.locationManager,一次登錄sendAction

你的代碼應該是這樣的:

self.locationManager = [[CLLocationManager alloc] init]; 
    [self.locationManager setDelegate:self]; 
    if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) { 
     // iOS8+ 
     // Sending a message to avoid compile time error 
     [self.locationManager requestAlwaysAuthorization]; 
    } 
+0

我做任何輸入 – iBug 2014-11-24 11:18:34

+0

那麼你所呼叫的方法兩次可能會導致意外的行爲之前,在此之前,但警報GET消失。 – rckoenes 2014-11-24 11:19:45

+0

我使用你的代碼,但仍然是同樣的問題。 – iBug 2014-11-24 11:27:31

2

顯然,在iOS的8 SDK,requestAlwaysAuthorization(背景位置)或requestWhenInUseAuthorization(位置僅在前臺)上CLLocationManager調用起始位置更新之前是必要的。

還需要在Info.plist中使用NSLocationAlwaysUsageDescription或NSLocationWhenInUseUsageDescription項,並在提示中顯示一條消息。添加這些解決了我的問題。

希望它可以幫助別人。

編輯:爲了更廣泛的信息,看看:http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/

+0

我已經將它們添加到info.plist中。 – iBug 2014-11-24 11:51:25

+1

您正在使用哪種xCode版本? – 2014-11-25 09:21:16

+0

我正在使用xcode 6.1 – iBug 2014-11-25 12:40:53