繁体   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