简体   繁体   English

如何在iOS 8上使用Objective-C打开/关闭定位服务

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

#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 1. 在iOS中获取iPhone位置,而无需将“位置服务”设置为“开”

2. IOS8 CLLocationManager.h 2. IOS8 CLLocationManager.h

I want turn on(off) my iphone6 location services in my app.but this code does't work on IOS8. 我想在我的应用中打开(关闭)我的iphone6定位服务。但是此代码在IOS8上不起作用。 I don't know why. 我不知道为什么

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. CLLocationManager没有您显示的界面。 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. 您可以检查相同的参考,以了解如何使用各种类型的位置服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM