175 lines
6.1 KiB
Swift
175 lines
6.1 KiB
Swift
import SwiftUI
|
||
import UserNotifications
|
||
import Photos
|
||
|
||
/// 权限管理视图,用于管理应用的各种权限设置
|
||
struct PermissionManagementView: View {
|
||
// MARK: - 状态变量
|
||
@State private var photoLibraryStatus: PHAuthorizationStatus = .notDetermined // 相册权限状态
|
||
@State private var notificationStatus: UNAuthorizationStatus = .notDetermined // 通知权限状态
|
||
|
||
var body: some View {
|
||
ZStack {
|
||
// 设置整体背景色
|
||
Color.themeTextWhiteSecondary
|
||
.edgesIgnoringSafeArea(.all)
|
||
|
||
VStack(spacing: 0) {
|
||
// 自定义导航栏
|
||
SimpleNaviHeader(title: "Permission Management") {
|
||
Router.shared.pop() // 返回上一页
|
||
}
|
||
.padding(.top, UIApplication.shared.windows.first?.safeAreaInsets.top ?? 0)
|
||
|
||
// 权限设置卡片
|
||
VStack(spacing: 0) {
|
||
// 1. 相册权限
|
||
PermissionRow(
|
||
title: "Gallery Permissions",
|
||
isEnabled: photoLibraryStatus == .authorized
|
||
) {
|
||
requestPhotoLibraryPermission() // 请求相册权限
|
||
}
|
||
|
||
// 2. 通知权限
|
||
PermissionRow(
|
||
title: "Notification Permissions",
|
||
isEnabled: notificationStatus == .authorized
|
||
) {
|
||
requestNotificationPermission() // 请求通知权限
|
||
}
|
||
}
|
||
.background(Color.white)
|
||
.cornerRadius(16)
|
||
.padding()
|
||
|
||
Spacer() // 将内容推到顶部
|
||
}
|
||
}
|
||
.navigationBarHidden(true) // 隐藏系统导航栏
|
||
.edgesIgnoringSafeArea(.all)
|
||
.onAppear {
|
||
// 视图出现时检查权限状态
|
||
checkPhotoLibraryPermission()
|
||
checkNotificationPermission()
|
||
}
|
||
}
|
||
|
||
// MARK: - 相册权限相关方法
|
||
|
||
/// 检查相册权限状态
|
||
private func checkPhotoLibraryPermission() {
|
||
photoLibraryStatus = PHPhotoLibrary.authorizationStatus()
|
||
}
|
||
|
||
/// 请求相册权限
|
||
private func requestPhotoLibraryPermission() {
|
||
// 如果未授权,则请求权限
|
||
if photoLibraryStatus == .notDetermined {
|
||
PHPhotoLibrary.requestAuthorization { status in
|
||
DispatchQueue.main.async {
|
||
self.photoLibraryStatus = status
|
||
// 如果用户拒绝,则打开应用设置
|
||
if status != .authorized {
|
||
self.openAppSettings()
|
||
}
|
||
}
|
||
}
|
||
} else {
|
||
// 如果已决定过权限,则直接打开设置
|
||
openAppSettings()
|
||
}
|
||
}
|
||
|
||
// MARK: - 通知权限相关方法
|
||
|
||
/// 检查通知权限状态
|
||
private func checkNotificationPermission() {
|
||
UNUserNotificationCenter.current().getNotificationSettings { settings in
|
||
DispatchQueue.main.async {
|
||
self.notificationStatus = settings.authorizationStatus
|
||
}
|
||
}
|
||
}
|
||
|
||
/// 请求通知权限
|
||
private func requestNotificationPermission() {
|
||
UNUserNotificationCenter.current().getNotificationSettings { settings in
|
||
// 如果从未请求过权限
|
||
if settings.authorizationStatus == .notDetermined {
|
||
// 请求通知权限
|
||
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { granted, _ in
|
||
DispatchQueue.main.async {
|
||
self.notificationStatus = granted ? .authorized : .denied
|
||
// 如果用户拒绝,则打开应用设置
|
||
if !granted {
|
||
self.openAppSettings()
|
||
}
|
||
}
|
||
}
|
||
} else {
|
||
// 如果已经请求过权限,则直接打开设置
|
||
DispatchQueue.main.async {
|
||
self.openAppSettings()
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
// MARK: - 辅助方法
|
||
|
||
/// 打开应用设置页面
|
||
private func openAppSettings() {
|
||
if let url = URL(string: UIApplication.openSettingsURLString) {
|
||
UIApplication.shared.open(url)
|
||
}
|
||
}
|
||
}
|
||
|
||
/// 权限设置行视图
|
||
struct PermissionRow: View {
|
||
let title: String // 权限名称
|
||
let isEnabled: Bool // 是否已授权
|
||
let action: () -> Void // 点击事件
|
||
|
||
var body: some View {
|
||
Button(action: action) {
|
||
HStack {
|
||
// 权限名称
|
||
Text(title)
|
||
.foregroundColor(.primary)
|
||
.font(.system(size: 16))
|
||
|
||
Spacer()
|
||
|
||
// 权限开关
|
||
Toggle("", isOn: .constant(isEnabled))
|
||
.labelsHidden()
|
||
.tint(Color.themePrimary)
|
||
.disabled(true)
|
||
.onAppear {
|
||
// 使用主题色并确保完全不透明
|
||
let themeColor = UIColor(Color.themePrimary)
|
||
UISwitch.appearance(whenContainedInInstancesOf: [UIView.self]).onTintColor = themeColor
|
||
UISwitch.appearance(whenContainedInInstancesOf: [UIView.self]).thumbTintColor = .white
|
||
// 确保使用不透明的背景
|
||
UISwitch.appearance(whenContainedInInstancesOf: [UIView.self]).backgroundColor = UIColor.clear
|
||
}
|
||
}
|
||
.padding()
|
||
}
|
||
.buttonStyle(PlainButtonStyle())
|
||
// 添加底部边框
|
||
.overlay(
|
||
Rectangle()
|
||
.frame(height: 1)
|
||
.foregroundColor(Color(.systemGray6)),
|
||
alignment: .bottom
|
||
)
|
||
}
|
||
}
|
||
|
||
// 预览
|
||
#Preview {
|
||
PermissionManagementView()
|
||
} |