Get iPhone location in iOS without preference Location Services set to ON

一笑奈何 提交于 2019-12-13 14:33:28

问题


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

回答1:


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



来源:https://stackoverflow.com/questions/25608339/get-iphone-location-in-ios-without-preference-location-services-set-to-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!