在开发过程,我们希望访问系统的设置界面让用户去修改某些权限。比如:用户不允许发送通知时候,我们有精彩内容推荐检测到用户关闭了通知,我们可以友好的提示用户该项权限关闭,希望跳转至用户设置界面进行修改。
网上很多写法是:
1、设置scheme:prefs
2、通过openUrl方法调用:prefres:root=“某项服务”&path=“具体选项路径”这样的格式去调用相关设置选项。
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=NOTIFICATIONS_ID&path=com.yxc8710.SettingSkip"] options:@{} completionHandler:nil];
这样写的话,我们发现还是不能实现该应用的设置跳转。原因是iOS10之后系统不支持这种形式对系统设置里面的具体项进行跳转,仅仅支持跳转自己的应用设置。那么iOS10之后我们怎么处理啦?
1、使用UIApplicationOpenSettingsURLString
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
UIApplicationOpenSettingsURLString字段,是在iOS8之后提供的,支持iOS8,iOS9,iOS10系统,推荐使用。
而iOS 10之后prefs:root=bundleID和prefs:root=服务失效。但是在prefs:root=服务在iOS10之前的系统还是有效的,prefs:root=bundleID在iOS8、iOS9系统有有效。
2、使用App-Prefs
使用这个scheme,而且不用添加,打开”App-Prefs:”URL可以调用系统的设置主界面
帖上一段完整代码:
- (void)viewDidAppear:(BOOL)animated {
if (@available(iOS 10.0, *)) {
[[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
if (settings.authorizationStatus == UNAuthorizationStatusNotDetermined)
{
NSLog(@"未选择");
[self opentNotificationAlert];
}else if (settings.authorizationStatus == UNAuthorizationStatusDenied){
NSLog(@"未授权");
[self opentNotificationAlert];
}else if (settings.authorizationStatus == UNAuthorizationStatusAuthorized){
NSLog(@"已授权");
}
}];
} else {
if ([[UIApplication sharedApplication] currentUserNotificationSettings].types == 0) {
[self opentNotificationAlert];
}
}
}
- (void)opentNotificationAlert {
/**< 弹出框 */
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"推送通知关闭"
message:@"请前往打开通知,获取更多精彩瞬间!"
preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}]];
[alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
if (@available(iOS 10.0, *)) {
// [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"App-Prefs:"] options:@{} completionHandler:nil];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:nil];
} else {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=NOTIFICATIONS_ID&path=com.yxc8710.SettingSkip"]];//prefs:root=服务&path=路径
// [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=com.yxc8710.SettingSkip"]];//prefs:root=bundleID
// [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}
}]];
[self presentViewController:alertController animated:YES completion:nil];
}