Get iPhone location in iOS without preference Loca

2019-07-06 08:48发布

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条回答
叼着烟拽天下
2楼-- · 2019-07-06 09:30

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

查看更多
登录 后发表回答