簡體   English   中英

獲取iPhone的iPhone位置,無需首選項位置服務設置為ON

[英]Get iPhone location in iOS without preference Location Services set to ON

我正在寫一個類似於Chris Alvares守護進程的守護進程 我想在沒有用戶許可的情況下在后台獲取設備位置。 如果“設置”中的“ Location Services首選項設置為“ ON那么我獲取位置沒有問題。 為此,我將添加到我的可執行文件權限com.apple.locationd.preauthorized密鑰,其布爾值設置為true。 問題是Location Services關閉時。 在這種情況下,當我想獲取設備位置時,彈出UIAlertView告訴用戶位置服務已關閉。 基本上有兩種方法,但我不知道它們是否可行。 首先,以編程方式打開/關閉設置中的位置服務首選項。 其次,使用其他代碼獲取位置,而無需將Locations Services設置為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/>

以下是FlipSwitch 定位開關的示例

在標題中聲明它

@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 Framework;)和

#import <objc/runtime.h>

編輯檢查上面的代碼

祝好運

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM