App设置UIAlertController

App中遇到需要用户打开设置界面的需求时,可以友好的引导用户去设置界面设置,而不是只是添加个提示,让用户自己去设置(可能他们也不知道怎么设置)

这样提示用户是不是很友好呢,那如何实现呢

UIAlertController * controller = [UIAlertController alertControllerWithTitle:@"提示" message:@"123456" preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction * action = [UIAlertAction actionWithTitle:@"Settiogs" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

NSURL * appSettings = [NSURL URLWithString:UIApplicationOpenSettingsURLString];

[[UIApplication sharedApplication] openURL:appSettings];

}];

[controller addAction:action];

[self presentViewController:controller animated:YES completion:nil];

我开始在Appdelegate是这样写的(TextViewController * vc = [[TextViewController alloc]init];self.window.rootViewController = vc;(这个controller是根controller上发现界面不显示  出现这个错误Attempt to presentonwhose view is not in the window hierarchy!)当我变成这样写时

TextViewController * vc = [[TextViewController alloc]init];

UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:vc];

self.window.rootViewController = nav; 就ok了

非常重要的代码是NSURL * appSettings = [NSURL URLWithString:UIApplicationOpenSettingsURLString];

[[UIApplication sharedApplication] openURL:appSettings];

}];

可以打开设置

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容