2010-10-04 50 views
2

locationServicesEnabled從屬性更改爲方法。locationServicesEnabled適用於iOS 3和iOS 4

這是棄用:

CLLocationManager *manager = [[CLLocationManager alloc] init]; 
if (manager.locationServicesEnabled == NO) { 
    // ... 
} 

現在我應該使用:

if (![CLLocationManager locationServicesEnabled]) { 
    // ... 
} 

我想支持的iOS 3和iOS 4設備。我如何在iOS 3設備上檢查這一點並擺脫已棄用的警告?

回答

1

嘗試:

BOOL locationServicesEnabled; 
CLLocationManager locationManager = [CLLocationManager new]; 
if([locationManager respondsToSelector:@selector(locationServicesEnabled) ]) 
{ 
    locationServicesEnabled = [locationManager locationServicesEnabled]; 
} 
else 
{ 
    locationServicesEnabled = locationManager.locationServicesEnabled; 
} 

作爲固定/變通。

使用編譯器定義會導致您在使用最低部署目標時允許較早的操作系統版本訪問您的應用程序時出現問題。

+0

我試過你的方法,但無論一般位置服務是否啓用,'locationServicesEnabled'總是YES。我檢查了另一種方法,他從來沒有進入這裏'#if __IPHONE_OS_VERSION_MIN_REQUIRED> __IPHONE_3_1'(在iOS 4.1的iPod Touch 2G上測試過)看起來您的最低部署目標是正確的。 – testing 2010-10-27 13:24:19

+0

現在我使用iPad進行了測試,但iPad也觸及了方法而不是屬性。 – testing 2010-10-27 13:52:10

+0

基本SDK爲4.1,部署目標爲3.0 – testing 2010-10-27 14:04:20

2

Editted:

#if __IPHONE_OS_VERSION_MIN_REQUIRED > __IPHONE_3_1 
    #if __IPHONE_OS_VERSION_MIN_REQUIRED > __IPHONE_3_2 
    if (![CLLocationManager locationServicesEnabled]) { 
    // ... 
    } 
    #else 
    CLLocationManager *manager = [[CLLocationManager alloc] init]; 
    if (manager.locationServicesEnabled == NO) { 
     // ... 
    } 
    #endif 
#else 
CLLocationManager *manager = [[CLLocationManager alloc] init]; 
if (manager.locationServicesEnabled == NO) { 
    // ... 
} 
#endif 
+0

謝謝。爲什麼選擇iOS 3.1? – testing 2010-10-04 13:21:20

+0

啊,因爲當我建立我的應用程序時,我們通常使用3.1.3,這意味着我們可以識別常量__IPHONE_3_1,但是因爲3.1.3將等於3.1,因此它將轉到else分支。原因也是3.1.3是4.0之前最高的iphone sdk(3.2僅適用於ipad) – vodkhang 2010-10-04 13:31:42

+0

如果應用程序可以在iPad上運行,該怎麼辦? – testing 2010-10-04 13:37:18

5

由於屬性'locationServicesEnabled'僅僅被棄用,它仍然可用(對於未確定的時間量)。爲了動態處理這種情況,您需要提供一個防禦性解決方案。類似上述的解決,我用:

BOOL locationAccessAllowed = NO ; 
if([CLLocationManager instancesRespondToSelector:@selector(locationServicesEnabled)]) 
{ 
    // iOS 3.x and earlier 
    locationAccessAllowed = locationManager.locationServicesEnabled ; 
} 
else if([CLLocationManager respondsToSelector:@selector(locationServicesEnabled)]) 
{ 
    // iOS 4.x 
    locationAccessAllowed = [CLLocationManager locationServicesEnabled] ; 
} 

爲「instancesRespondToSelector」檢查了電話,看看屬性仍然可用,然後我仔細檢查類本身支持方法調用(作爲一個靜態方法它會報告YES)。

只是另一種選擇。

相關問題