From a1bd7e842082fa53104009a86f74a5dd8001aaf2 Mon Sep 17 00:00:00 2001 From: v504 Date: Mon, 1 Sep 2025 18:16:14 +0800 Subject: [PATCH] Refactor QRCodeStyleView to streamline photo library permission handling. Remove deprecated permission check and implement a new method for custom logo selection that requests permissions dynamically. Enhance user experience by guiding users to settings if access is denied. --- .../xcdebugger/Breakpoints_v2.xcbkptlist | 90 ------------------ .../Views/Generator/QRCodeStyleView.swift | 93 +++++++++++-------- 2 files changed, 53 insertions(+), 130 deletions(-) diff --git a/MyQrCode.xcodeproj/xcuserdata/yc.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/MyQrCode.xcodeproj/xcuserdata/yc.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist index 76092c1..b80ee3d 100644 --- a/MyQrCode.xcodeproj/xcuserdata/yc.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist +++ b/MyQrCode.xcodeproj/xcuserdata/yc.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -114,36 +114,6 @@ endingLineNumber = "233" landmarkName = "createInputComponent(for:emailConfig:wifiConfig:contactConfig:locationConfig:calendarConfig:socialConfig:phoneConfig:urlConfig:textConfig:)" landmarkType = "7"> - - - - - - - - - - - - - - - - - - diff --git a/MyQrCode/Views/Generator/QRCodeStyleView.swift b/MyQrCode/Views/Generator/QRCodeStyleView.swift index 6756795..33a44e9 100644 --- a/MyQrCode/Views/Generator/QRCodeStyleView.swift +++ b/MyQrCode/Views/Generator/QRCodeStyleView.swift @@ -133,7 +133,6 @@ struct QRCodeStyleView: View { } } .onAppear { - checkPhotoLibraryPermission() initializeExistingStyle() } .sheet(isPresented: $showingImagePicker) { @@ -409,10 +408,9 @@ struct QRCodeStyleView: View { } // 自定义Logo选项 - if photoLibraryAccessGranted { - Button(action: { - showingImagePicker = true - }) { + Button(action: { + handleCustomLogoSelection() + }) { VStack(spacing: 8) { if let customLogoImage = customLogoImage { Image(uiImage: customLogoImage) @@ -448,41 +446,7 @@ struct QRCodeStyleView: View { ) ) } - } else { - // 权限被拒绝时的处理 - Button(action: { - // 引导用户到设置页面 - if let settingsUrl = URL(string: UIApplication.openSettingsURLString) { - UIApplication.shared.open(settingsUrl) - } - }) { - VStack(spacing: 8) { - RoundedRectangle(cornerRadius: 12) - .fill(Color.red.opacity(0.2)) - .frame(width: 60, height: 60) - .overlay( - Image(systemName: "exclamationmark.triangle") - .font(.title2) - .foregroundColor(.red) - ) - - Text("permission_required".localized) - .font(.caption) - .foregroundColor(.red) - .multilineTextAlignment(.center) - .id(languageManager.refreshTrigger) - } - .padding(12) - .background( - RoundedRectangle(cornerRadius: 16) - .fill(Color.clear) - .overlay( - RoundedRectangle(cornerRadius: 16) - .stroke(Color.red.opacity(0.3), lineWidth: 1) - ) - ) - } - } + // Logo选项 ForEach(QRCodeLogo.allCases, id: \.self) { logo in @@ -754,6 +718,55 @@ struct QRCodeStyleView: View { } } + // MARK: - 处理自定义Logo选择 + private func handleCustomLogoSelection() { + // 检查当前权限状态 + let status = PHPhotoLibrary.authorizationStatus() + + switch status { + case .authorized, .limited: + // 已有权限,直接显示图片选择器 + photoLibraryAccessGranted = true + showingImagePicker = true + print("相册权限已授权,显示图片选择器") + + case .notDetermined: + // 权限未确定,请求权限 + print("相册权限未确定,正在请求...") + PHPhotoLibrary.requestAuthorization { newStatus in + DispatchQueue.main.async { + self.photoLibraryAccessGranted = (newStatus == .authorized || newStatus == .limited) + print("权限请求结果: \(newStatus.rawValue), 授权状态: \(self.photoLibraryAccessGranted)") + + if self.photoLibraryAccessGranted { + // 权限获取成功,显示图片选择器 + self.showingImagePicker = true + } + } + } + + case .denied, .restricted: + // 权限被拒绝,显示设置引导 + photoLibraryAccessGranted = false + print("相册权限被拒绝,引导用户到设置页面") + // 可以在这里显示一个alert来引导用户 + showPermissionAlert() + + @unknown default: + photoLibraryAccessGranted = false + print("相册权限未知状态") + } + } + + // MARK: - 显示权限提示 + private func showPermissionAlert() { + // 这里可以显示一个alert来引导用户到设置页面 + // 或者直接打开设置页面 + if let settingsUrl = URL(string: UIApplication.openSettingsURLString) { + UIApplication.shared.open(settingsUrl) + } + } + // MARK: - 权限检查 private func checkPhotoLibraryPermission() { let status = PHPhotoLibrary.authorizationStatus()