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

相关问题