我正在写一个类似于的守护进程克里斯·阿尔瓦雷斯守护进程 http://chrisalvares.com/blog/7/creating-an-iphone-daemon-part-1/。我想在未经用户许可的情况下在后台获取设备位置。如果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
因此,我将此密钥添加到权利中,但它仍然给了我这个错误。在检查 Preferences 应用程序权利后,我将这些行添加到权利 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 的示例位置切换 https://github.com/a3tweaks/Flipswitch/blob/master/Switches/Location
在你的标题中声明它
@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>
EDIT检查上面的代码
祝你好运
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)