美文网首页直播 IM程序员iOS
iOS一行代码带你跳转系统各设置页面

iOS一行代码带你跳转系统各设置页面

作者: Qinz | 来源:发表于2017-03-23 11:16 被阅读2465次
    Qinz
    iOS开发中,有时会涉及到跳转到系统设置的界面,比如打开蓝牙,通知等,废话不多说,直接上图:Demo给出了31种界面的跳转,当然你也可以根据我下面第三步反映的规律进行其它页面的跳转~
    1.gif 2.gif 3.gif 4.gif 5.gif
    第一步:配置info信息,添加URL Sechmes的字段为prefs,不配置是没办法跳转的!
    配置info
    第二步:上代码 下面是本Demo所涉及所有跳转页面
      let listArr = ["无线局域网","蓝牙","个人热点","运营商","通知","睡眠","通用","调节亮度","墙纸",
    "声音","siri语音助手","隐私","电话","icloud","iTunes Strore 与 APP Store","safari","关于本机",
    "软件更新","辅助功能","日期与时间","键盘","存储空间","语言与地区","VPN","描述文件与设备管理","音乐",
    "备忘录","照片与相机","还原","Twiter","Facebook"]
    
    • 1.跳转的函数
     //跳转系统设置页面
        func jump(url:String?){
            
            guard let jumpUrl = URL.init(string: url!) else {
                return
            }
            if UIApplication.shared.canOpenURL(jumpUrl)
            {
                if #available(iOS 10.0, *){
                    UIApplication.shared.open(jumpUrl, options: [:], completionHandler: { (success) in
                        if success {
                            print("跳转成功")
                        }
                    })
                }else{
                    UIApplication.shared.openURL(jumpUrl)
                }
            }
        }
    
    • 2.跳转设置:注意以下为私有API,请谨慎使用:可能会导致上架被拒!
     
        //MARK: -- 选择每个cell执行的操作
        public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            
            switch indexPath.row {
                
            case 0://无线局域网
                jump(url: "App-prefs:root=WIFI")
            case 1: //蓝牙
                jump(url: "App-Prefs:root=Bluetooth")
            case 2: //个人热点
                jump(url: "App-prefs:root=INTERNET_TETHERING")
            case 3: //运营商
                jump(url: "App-prefs:root=Carrier")
            case 4: //通知:需要大写后面加上ID
                jump(url: "App-prefs:root=NOTIFICATIONS_ID")
            case 5: //睡眠
                jump(url: "App-prefs:root=DO_NOT_DISTURB")
            case 6: //通用
                jump(url: "App-prefs:root=General")
            case 7: //调节亮度
                jump(url: "App-prefs:root=DISPLAY&BRIGHTNESS")
            case 8://墙纸
                jump(url: "App-prefs:root=Wallpaper")
            case 9://声音
                jump(url: "App-prefs:root=Sounds")
            case 10://siri语音助手
                jump(url: "App-prefs:root=SIRI")
            case 11://隐私
                jump(url: "App-prefs:root=Privacy")
            case 12: //电话
                jump(url: "App-prefs:root=Phone")
            case 13:  //icloud
                jump(url: "App-prefs:root=CASTLE")
            case 14://iTunes Strore 与 APP Store
                jump(url: "App-prefs:root=STORE")
            case 15://safari
                jump(url: "App-prefs:root=SAFARI")
            case 16: //关于本机
                jump(url: "App-prefs:root=General&path=About")
            case 17://软件更新
                jump(url: "App-prefs:root=General&path=SOFTWARE_UPDATE_LINK")
            case 18: //辅助功能
                jump(url: "App-prefs:root=General&path=ACCESSIBILITY")
            case 19: //日期与时间
                jump(url: "App-prefs:root=General&path=DATE_AND_TIME")
            case 20: //键盘
                jump(url: "App-prefs:root=General&path=Keyboard")
            case 21://存储空间
                jump(url: "App-prefs:root=CASTLE&path=STORAGE_AND_BACKUP")
            case 22: //语言与地区
                jump(url: "App-prefs:root=General&path=Language_AND_Region")
            case 23://VPN
                jump(url: "App-prefs:root=General&path=VPN")
            case 24://描述文件与设备管理
                jump(url: "App-prefs:root=General&path=ManagedConfigurationList")
            case 25://音乐
                jump(url: "App-prefs:root=MUSIC")
            case 26://备忘录
                jump(url: "App-prefs:root=NOTES")
            case 27: //照片与相机
                jump(url: "App-prefs:root=Photos")
            case 28://还原
                jump(url: "App-prefs:root=General&path=Reset")
            case 29: //Twiter
                jump(url: "App-prefs:root=TWITTER")
            case 30: //Facebook
                jump(url: "App-prefs:root=FACEBOOK")
                
            default:
                break
            }
        }
    
    第三步:扩展
    • 1.我们来看下面这几句跳转代码
           
            case 0://无线局域网
                jump(url: "App-prefs:root=WIFI")
            case 1: //蓝牙
                jump(url: "App-Prefs:root=Bluetooth")
            case 2: //个人热点
                jump(url: "App-prefs:root=INTERNET_TETHERING")
            case 3: //运营商
                jump(url: "App-prefs:root=Carrier")
           case 4: //通知:需要大写后面加上ID
                jump(url: "App-prefs:root=NOTIFICATIONS_ID")
            case 5: //睡眠
                jump(url: "App-prefs:root=DO_NOT_DISTURB")
            case 6: //通用
                jump(url: "App-prefs:root=General")
    
    
    • 2.再来看看我们将手机语言切换至英文设置界面的情况——是不是很容易发现规律呢?
    1.png

    总结: 基本都是 jump(url: "App-Prefs:root=Bluetooth") ,root等于号后面的就是该英文单词,当然有的是用长的英文单词是用“_”来连接,如jump(url: "App-prefs:root=NOTIFICATIONS_ID"),有的是将小写改为大写,如SAFARI。可以自己去试试就明白其中的关联了~是不是很简单呢?根据该规律还有些界面自己设置也可以跳转过去,当然有些也是跳不过去的!

    OC也是一行代码调用:
     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"App-Prefs:root=Bluetooth"]];
    

    最后附上demo下载地址:https://git.oschina.net/Qinz_323/JumpDemo

    参考来源:http://stackoverflow.com/questions/9092142/ios-uialertview-button-to-go-to-setting-app

    我是Qinz,希望我的文章对你有帮助。

    相关文章

      网友评论

      • Jack小麻雀_:果然不应该调用私有API,前天提交的版本今天(2018.5.16)就被苹果拒绝了,说是引用了私有API。希望楼主多多提示使用私有API可能造成的后果~
        Qinz:文章中已经注明了私有API,我想着开发iOS应该都知道会有被拒的可能性,你的建议很好,我会加上这句话:+1:
      • 妹子爱编程:打开推送的详细代码请写下,小哥哥,没效果按照你这样写,妹子很着急。

      本文标题:iOS一行代码带你跳转系统各设置页面

      本文链接:https://www.haomeiwen.com/subject/ydsznttx.html