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)
}
}
通知扩展,方便使用
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)
}
}