【问题标题】:Get iPhone location in iOS without preference Location Services set to ON在 iOS 中获取 iPhone 位置,无需将“位置服务”设置为“开”
【发布时间】:2014-10-25 19:55:37
【问题描述】:

我正在编写一个类似于Chris Alvares daemon 的守护进程。我想在未经用户许可的情况下在后台获取设备位置。如果“设置”中的Location Services 首选项设置为ON,那么获取位置没有问题。为此,我将布尔值设置为 true 添加到我的可执行权利 com.apple.locationd.preauthorized 键中。问题是当Location Services 关闭时。在这种情况下,当我想获取设备位置时,会弹出 UIAlertView 告诉用户位置服务已关闭。基本上有两种方法,但我不知道它们是否可行。首先,以编程方式打开/关闭设置中的定位服务首选项。其次,使用其他代码获取位置,无需设置 Locations Services ON


更新 01:

我按照 iMokhles 所说的做了,我想它应该可以工作,但没有任何反应。我想这是因为权利,我看到了系统日志,这是记录的内容:

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

所以我将此密钥添加到权利中,但它仍然给了我这个错误。在我检查了 Preferences app entitlement 后,我​​将这些行添加到了 entitlements 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/>

【问题讨论】:

    标签: ios geolocation jailbreak iphone-privateapi location-services


    【解决方案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 Framework ;) 和

    #import <objc/runtime.h>
    

    编辑 检查上面的代码

    祝你好运

    【讨论】:

    • 谢谢。 setLocationServicesEnabled 工作正常,但我无法从 locationServicesEnabled 获取当前值。它总是返回真值。我有个问题。 locationServicesEnabled 是一个类方法,那你为什么用sharedManager 来调用它呢?
    • 顺便说一句,为什么使用 [objc_getClass("CLLocationManager") setLocationServicesEnabled:] 而不是 [CLLocationManager setLocationServicesEnabled:] 工作? (我的意思是日志说我的问题是使用objc_getClass 时的权利,我根本不需要权利!)。不过我有一个猜测。我认为这是因为当您从它的名称中获取类时,它是从从 dyld_shared_chache 加载的 CLLocation 框架的二进制文件中执行的,并且因为该二进制文件具有正确的权限,所以不需要权利。我说的对吗?
    • [CLLocationManager locationServicesEnabled] 有效!我不知道为什么它在几分钟前不起作用。也许我的测试错了:)我已经稍微修改了你的答案。
    猜你喜欢
    • 2015-02-10
    • 1970-01-01
    • 2012-03-01
    • 2013-12-30
    • 2015-12-30
    • 1970-01-01
    • 1970-01-01
    • 2014-01-28
    • 1970-01-01
    相关资源
    最近更新 更多