App设置UIAlertController

2015-12-18  本文已影响307人  雨雪霏霏why

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];

}];

可以打开设置

上一篇下一篇

猜你喜欢

热点阅读