3

我正在編寫類似於Chris Alvares daemon的守護程序。我想在沒有用戶許可的情況下獲得後臺的位置信息。如果設置中的Location Services首選項設置爲ON,那麼我無法獲取位置信息。爲此,我將添加到我的可執行權利com.apple.locationd.preauthorized關鍵與布爾值設置爲true。問題是Location Services關閉時。在這種情況下,當我想獲取設備位置時,會彈出一個UIAlertView告訴用戶位置服務關閉。基本上有兩種方法,但我不知道它們是否可行。首先,以編程方式打開/關閉設置中的位置服務首選項。其次,獲得使用其他代碼的位置,而不需要位置服務被設置ON在沒有偏好的情況下在iOS中獲取iPhone位置設置爲ON的位置服務


更新01:

我一樣iMokhles說,我想它應該工作,但沒有任何反應。我想這是因爲權利的,我看到的syslog和這裏是被記錄:

iPhone locationd[44] <Error>: Entitlement com.apple.locationd.authorizeapplications required to use _CLDaemonSetLocationServicesEnabled 
iPhone myDaemon[3443] <Error>: CoreLocation: CLInternalSetLocationServicesEnabled failed 

所以我加入這個關鍵的權利,但它仍然給了我這個錯誤。當我檢查首選項應用權利後,我將這些行添加到權利plist,但再次沒有任何反應。

<key>com.apple.locationd.authorizeapplications</key> 
<true/> 
<key>com.apple.locationd.defaults_access</key> 
<true/> 
<key>com.apple.locationd.effective_bundle</key> 
<true/> 
<key>com.apple.locationd.status</key> 
<true/> 

回答

1

這裏是FlipSwitch Location Switch

爲例聲明它在你的頭

@interface CLLocationManager 
+ (id)sharedManager; 
+ (BOOL)locationServicesEnabled; 
+ (void)setLocationServicesEnabled:(BOOL)enabled; 
@end 

然後,你可以用它下面的代碼 檢查是否啓用訪問

if([[[objc_getClass("CLLocationManager") sharedManager] locationServicesEnabled] { 
    // Enabled 
} else { 
    // Disabled 
} 

並啓用/禁用位置

[objc_getClass("CLLocationManager") setLocationServicesEnabled:YES]; 
[objc_getClass("CLLocationManager") setLocationServicesEnabled:NO]; 

,不要忘記導入CoreLocation框架;) 和

#import <objc/runtime.h> 

編輯上述

好運

+0

校驗碼謝謝。我更新了這個問題:) – user3586942 2014-09-01 16:09:15

+0

檢查我的編輯代碼:D – iMokhles 2014-09-01 17:48:05

+0

謝謝。 'setLocationServicesEnabled'工作正常,但我無法從'locationServicesEnabled'獲取當前值。它總是返回真實值。我有個問題。 'locationServicesEnabled'是一個類方法,所以你爲什麼使用'sharedManager'來調用它? – user3586942 2014-09-01 18:33:26

相關問題