- (void)viewDidLoad
{
[super viewDidLoad];
UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(100, 100, 100, 100);
btn.backgroundColor = [UIColor redColor];
[btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
-(void)click
{
[self openWiFi];
}
// 打开系统的Wi-Fi界面,当系统大于10的时候直接打开当前App的设置界面
- (void)openWiFi
{
NSURL *url = [NSURL URLWithString:@"Prefs:root=Privacy&path=WIFI"]; //[WIFI 必须大写]
if ([[UIApplication sharedApplication] canOpenURL:url])
{
// 系统小于10的时候,打开Wi-Fi界面
[[UIApplication sharedApplication] openURL:url];
}
else
{
// 系统大于10的时候直接打开当前App的设置界面
//注意首字母改成了大写,prefs->Prefs
Class LSApplicationWorkspace = NSClassFromString(@"LSApplicationWorkspace");
[[LSApplicationWorkspace performSelector:@selector(defaultWorkspace)] performSelector:@selector(openSensitiveURL:withOptions:) withObject:url withObject:nil];
}
就这么简单粗暴,直接上代码;
如果想打开其他的设置,例如定位;
步骤如下:
1.把手机设置语言为英语;
2.找到定位的界面;
3.把标题Location放到程序里WIFI的位置并改成全是大写LOCATION即可;【注:一定要是大写!!!】
Enjoy your code.
备注:
可能出现的报错:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
老版本的使用这个方法程序直接崩溃报错显示:The operation couldn’t be completed. (OSStatus error -10814.)
解决方案:
替换方法为
Class LSApplicationWorkspace = NSClassFromString(@"LSApplicationWorkspace");
[[LSApplicationWorkspace performSelector:@selector(defaultWorkspace)] performSelector:@selector(openSensitiveURL:withOptions:) withObject:url withObject:nil];
站在巨人的肩膀上才有这些总结
菜鸟走向大牛,大家共同前进,如果觉得不错,请给个赞/关注。
一起交流学习,有问题随时欢迎联系,邮箱:383708669@qq.com
网友评论