在没有偏好位置服务的iOS中获取iPhone位置设置为开启 [英] Get iPhone location in iOS without preference Location Services set to ON

查看:191
本文介绍了在没有偏好位置服务的iOS中获取iPhone位置设置为开启的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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






更新01:



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

  iPhone定位[44]< ; Error>:Entitlement com.apple.locationd.authorizeapplications required required to use _CLDaemonSetLocationServicesEnabled 
iPhone myDaemon [3443]<错误>:CoreLocation:CLInternalSetLocationServicesEnabled失败

因此,我将这个密钥添加到权利,但它仍然给我这个错误。在检查Preferences应用程序授权后,我将这些行添加到授权plist中,但再次没有任何反应。

 < key> com.apple .locationd.authorizeapplications< /密钥GT; 
< 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 {
//已禁用
}

并启用/禁用位置
$ b $ pre $ [objc_getClass(CLLocationManager)setLocationServicesEnabled:YES];
[objc_getClass(CLLocationManager) setLocationServicesEnabled:NO];

并且不要忘记导入CoreLocation框架;)

  #import< objc / runtime.h> 

编辑
检查上面的代码

好运

I'm writing a daemon similar to Chris Alvares daemon. I want to get the divice location in background without users permission. If the Location Services preference in Settings is set to ON then I have no problem getting location. For this I'm adding to my executable entitlements com.apple.locationd.preauthorized key with boolean value set to true. The problem is when the Location Services is off. In this case when I want to get the device location a UIAlertView pops up telling the user that location services is off. There are basically 2 ways but i don't know if they are doable. First, to programmatically turn on/off the Location Services preferences in settings. Second, get location using another code without needing Locations Services to be set ON


Update 01:

I did as iMokhles said and I guess it should work but nothing happens. I guess it is because of the entitlements and I saw the syslog and here is what is logged:

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

So I added this key to entitlements but it still gave me this error. After I checked Preferences app entitlement I added these lines to the entitlements plist but again nothing happens.

<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/>

解决方案

Here is an Example from FlipSwitch Location Switch

declare it in your header

@interface CLLocationManager
+ (id)sharedManager;
+ (BOOL)locationServicesEnabled;
+ (void)setLocationServicesEnabled:(BOOL)enabled;
@end

Then you can access it using the following code to check if enabled

if([[[objc_getClass("CLLocationManager") sharedManager] locationServicesEnabled] {
    // Enabled
} else {
   // Disabled
}

and to enable/disable location

[objc_getClass("CLLocationManager") setLocationServicesEnabled:YES];
[objc_getClass("CLLocationManager") setLocationServicesEnabled:NO];

and don't forget to import CoreLocation Framework ;) and

#import <objc/runtime.h>

EDIT Check code above

Good Luck

这篇关于在没有偏好位置服务的iOS中获取iPhone位置设置为开启的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆