feat: 权限
This commit is contained in:
parent
b89b3b0cc4
commit
4cbfaefb49
@ -14,6 +14,7 @@ enum AppRoute: Hashable {
|
||||
case userInfo
|
||||
case account
|
||||
case about
|
||||
case permissionManagement
|
||||
|
||||
@ViewBuilder
|
||||
var view: some View {
|
||||
@ -42,6 +43,8 @@ enum AppRoute: Hashable {
|
||||
AccountView()
|
||||
case .about:
|
||||
AboutUsView()
|
||||
case .permissionManagement:
|
||||
PermissionManagementView()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
167
wake/View/Owner/PermissionManagementView.swift
Normal file
167
wake/View/Owner/PermissionManagementView.swift
Normal file
@ -0,0 +1,167 @@
|
||||
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) // 禁用交互,仅用于显示状态
|
||||
}
|
||||
.padding()
|
||||
}
|
||||
.buttonStyle(PlainButtonStyle())
|
||||
// 添加底部边框
|
||||
.overlay(
|
||||
Rectangle()
|
||||
.frame(height: 1)
|
||||
.foregroundColor(Color(.systemGray6)),
|
||||
alignment: .bottom
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// 预览
|
||||
#Preview {
|
||||
PermissionManagementView()
|
||||
}
|
||||
@ -51,7 +51,9 @@ struct SettingsView: View {
|
||||
settingRow(
|
||||
icon: "Permission",
|
||||
title: "Permission Management",
|
||||
action: {}
|
||||
action: {
|
||||
Router.shared.navigate(to: .permissionManagement)
|
||||
}
|
||||
)
|
||||
|
||||
// 支持与服务
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user