美文网首页swift
swift4, 跳转到系统设置

swift4, 跳转到系统设置

作者: 小曼blog | 来源:发表于2018-05-28 14:24 被阅读39次

有时候我们会因为一些权限需要用户到系统设置中进行开启权限,这就需要我们从自己的app中跳转到系统的设置中了,具体是怎么做的呢?其实网上有很多很多的资料,我就不再赘述了。这里只看代码部分。

简单效果图:

img.png

注意: 一定要用真机哟。

代码:

  1. 在storyboard中拖一个tableview,设置约束,设置代理。

2.在vc中实现代码:

//
//  ViewController.swift
//  JumpToSystemSetting
//
//  Created by iOS on 2018/5/28.
//  Copyright © 2018年 weiman. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var tableview: UITableView!
    
    private var dataArray: [String] = []
    private var urlDict: [String:String] = [:]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        makeData()
    }
    
    private func makeData() {
        
        urlDict = [
            "系统设置": UIApplicationOpenSettingsURLString,
            "个人热点":"prefs:root=INTERNET_TETHERING",
            "WIFI设置":"prefs:root=WIFI",
            "蓝牙设置":"prefs:root=Bluetooth",
            "系统通知":"prefs:root=NOTIFICATIONS_ID",
            "通用设置":"prefs:root=General",
            "显示设置":"prefs:root=DISPLAY&BRIGHTNESS",
            "壁纸设置":"prefs:root=Wallpaper",
            "声音设置":"prefs:root=Sounds",
            "隐私设置":"prefs:root=privacy",
            "蜂窝网路":"prefs:root=MOBILE_DATA_SETTINGS_ID",
            "音乐":"prefs:root=MUSIC",
            "APP Store":"prefs:root=STORE",
            "Notes":"prefs:root=NOTES",
            "Safari":"prefs:root=Safari",
            "Music":"prefs:root=MUSIC",
            "photo":"prefs:root=Photos"
        ]
         dataArray = Array(urlDict.keys)
    }


}

extension ViewController {
    
    /// 跳转到系统设置主页
    func jumpToSystemSeting() {
        let settingUrl = URL(string: UIApplicationOpenSettingsURLString)
        if let url = settingUrl, UIApplication.shared.canOpenURL(url) {
            UIApplication.shared.openURL(url)
        }
    }
    /// 定位服务
    func jumpToPosition() {
        let settingUrl = URL(string: "prefs:root=LOCATION_SERVICES")
        if let url = settingUrl, UIApplication.shared.canOpenURL(url) {
            UIApplication.shared.openURL(url)
        }
    }
    /// wifi服务
    func jumpToWifi() {
        let settingUrl = URL(string: "prefs:root=WIFI")
        if let url = settingUrl, UIApplication.shared.canOpenURL(url) {
            UIApplication.shared.openURL(url)
        }
    }
    
    /// 系统通知
    func jumpToNoti() {
        let settingUrl = URL(string: "prefs:root=NOTIFICATIONS_ID")
        if let url = settingUrl, UIApplication.shared.canOpenURL(url) {
            UIApplication.shared.openURL(url)
        }
    }
    
    /// 跳转的模版
    func jumpTemplate(strurl: String) {
        let urltemp = URL(string: strurl)
        if let url = urltemp, UIApplication.shared.canOpenURL(url) {
            UIApplication.shared.openURL(url)
        }
    }
}

extension ViewController: UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell.init(style: .default, reuseIdentifier: "cell")
        cell.textLabel?.text = dataArray[indexPath.row]
        return cell
    }
}

extension ViewController: UITableViewDelegate {
   
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let url = urlDict[dataArray[indexPath.row]]
        if let strUrl = url {
            jumpTemplate(strurl: strUrl)
        }
    }
}


相关文章

网友评论

  • 观星:Prefs:root 应该会被审核拒绝
    小曼blog:多谢提醒,我再考证下哈

本文标题:swift4, 跳转到系统设置

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