• iOS权限弹窗申请


    demo:

    
    import UIKit
    import CoreLocation
    import CoreBluetooth
    import CoreTelephony
    import UserNotifications
    
    class ViewController: UIViewController {
        var manager: CLLocationManager?
        var bleManager: CBCentralManager?
        var cellularData: CTCellularData?
    
        override func viewDidLoad() {
            super.viewDidLoad()
            view.backgroundColor = .blue
            
            bleRequest()
            netRequest()
            locationRequest()
            getNetPermissonState()
            
        }
        
        // 蓝牙权限
        func bleRequest() {
            self.bleManager = CBCentralManager.init()
        }
        
        // 定位权限
        func locationRequest() {
            self.manager = CLLocationManager.init()
            manager?.requestWhenInUseAuthorization()
        }
        
        // 发送网络请求
        func netRequest() {
            var request = URLRequest(url: URL(string: "https://www.baidu.com")!, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 30)
            request.httpMethod = "GET"
            
            URLSession.shared.dataTask(with: request) { data, response, error in
                if (error != nil) {
                    print("error--:\(String(describing: error))")
                }
                print("data:\(String(describing: data))")
                
            }.resume()
        }
    
    }
    
    // 通知权限
    extension ViewController: UNUserNotificationCenterDelegate {
        func getNotifyPermissionRequest() {
            // 先检查权限
            checkNotificationStatus()
        }
        
        // 检查通知权限
        func checkNotificationStatus() {
            UNUserNotificationCenter.current().getNotificationSettings { settings in
                switch settings.authorizationStatus {
                case .notDetermined:
                    print("未决定")
                case .denied:
                    print("拒绝")
                case .authorized:
                    print("同意")
                case .provisional:
                    print("provisional")
                case .ephemeral:
                    print("ephemeral")
                @unknown default:
                    print("default")
                }
            }
        }
        
        func showUserNotiAlert() {
            setPermisson()
        }
        
        func registerNotification() {
            let center = UNUserNotificationCenter.current()
            center.requestAuthorization(options: [.alert, .sound, .badge]) { isAllow, error in
                if isAllow {
                    print("注册成功")
                    NotificationCenter.post(customNotification: .success)
                }
            }
            
            UIApplication.shared.registerForRemoteNotifications()
        }
        
        
        
        
        
    }
    
    // 网络权限
    extension ViewController {
        // 不同国家地区,不同型号手机,默认权限不一样
        func getNetPermissonState() {
            cellularData = CTCellularData()
            let state = cellularData?.restrictedState
            switch state {
            case .restricted:
                print("restricted--被限制网络权限")
            case .notRestricted:
                print("notRestricted--未被限制网络权限")
            case .restrictedStateUnknown:
                print("restrictedStateUnknown")
                setPermisson()
            case .none:
                print("none")
            case .some(_):
                print("some")
            }
        }
        
        func setPermisson() {
            print("setPermisson")
            let alertController = UIAlertController.init(title: "提示", message: "您尚未授权网络权限", preferredStyle: .alert)
            
            let actionCancel = UIAlertAction(title: "取消", style: .cancel)
            
            let actionAgree = UIAlertAction(title: "确定", style: .default) { alert in
                var url: URL
                if #available(iOS 15.4, *) {
                    url = URL(string: UIApplication.openSettingsURLString)!
                } else {
                    url = URL(string: UIApplication.openSettingsURLString)!
                }
                
                if UIApplication.shared.canOpenURL(url) {
                    UIApplication.shared.open(url)
                }
            }
            
            alertController.addAction(actionCancel)
            alertController.addAction(actionAgree)
            self.navigationController?.present(alertController, animated: true)
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145

    通知扩展,方便使用

    import Foundation
    
    enum TestNotification: String {
        case success
        case failure
        
        var stringValue: String {
            return "MyTest" + self.rawValue
        }
        
        var notificationName: NSNotification.Name {
            return NSNotification.Name(stringValue)
        }
    }
    
    extension NotificationCenter {
        static func post(customNotification name: TestNotification, object: Any? = nil) {
            NotificationCenter.default.post(name: name.notificationName, object: object)
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
  • 相关阅读:
    deepstream 检测结果截图
    载银纳米TiO2/壳聚糖水凝胶/pH/GSH响应羧甲基壳聚糖水凝胶和纳米凝胶的制备
    基于JavaFX图形界面演示的迷宫创建与路径寻找
    重复控制器的性能优化
    搭建一个属于自己的博客
    大语言模型(LLM)Token 概念
    ActiveMQ的基础
    Shell命令切换root用户、管理配置文件、检查硬件
    spring framework 5.2 文档 - 核心 ioc 目录
    曲线加速【C#】
  • 原文地址:https://blog.csdn.net/JH_Cao/article/details/127916461