How to turn on/off location services with objective-c on IOS8

2.2k Views Asked by At
#import "ViewController.h"
#import <NotificationCenter/NotificationCenter.h>
#import <objc/runtime.h>

@interface CLLocationManager

+ (id)sharedManager;
+ (BOOL)locationServicesEnabled;
+ (void)setLocationServicesEnabled:(BOOL)enabled;
+ (BOOL)locationServicesEnabled:(BOOL)arg1;

@end


- (void)viewDidLoad
{
    [super viewDidLoad];
    id CLLocationManager1 = objc_getClass("CLLocationManager");
    self.mySwitch.on = [CLLocationManager1 locationServicesEnabled];//this works fine
}

- (IBAction)doSomeThing:(UISwitch *)sender
{
    id CLLocationManager1 = objc_getClass("CLLocationManager");
    [CLLocationManager1 setLocationServicesEnabled:sender.selected];//this does't work.
    [CLLocationManager1 locationServicesEnabled:sender.selected];
}

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

2.IOS8 CLLocationManager.h

I want turn on(off) my iphone6 location services in my app.but this code does't work on IOS8. I don't know why.

1

There are 1 best solutions below

2
On

You cannot turn location services on from your app if the user has switched them off. You can only check with

[CLLocationManager locationServicesEnabled]

whether the user has switched them off or not.

CLLocationManager does not have the interface that you show. For the real interface see the Class Reference. You can check that same reference to see how to use the various types of location services.