在iOS中获取iPhone位置,而不将首选项“位置服务”设置为“打开”



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


更新01:

我照iMokhles说的做了,我想这应该奏效,但什么都没发生。我想这是因为权利的原因,我看到了系统日志,下面是记录的内容:

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>

编辑上方的检查代码

好运

最新更新