diff --git a/ln_jq_app/ios/Podfile b/ln_jq_app/ios/Podfile index 467c7f8..3a6e2e4 100644 --- a/ln_jq_app/ios/Podfile +++ b/ln_jq_app/ios/Podfile @@ -61,8 +61,12 @@ post_install do |installer| # 开启定位权限支持 config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] << 'PERMISSION_LOCATION=1' - # 如果你需要其他权限,也在这里加,比如相机: - # config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] << 'PERMISSION_CAMERA=1' + # 2. 相机 (Camera) + config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] << 'PERMISSION_CAMERA=1' + + # 3. 相册 (Photos) + config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] << 'PERMISSION_PHOTOS=1' + end end diff --git a/ln_jq_app/ios/Podfile.lock b/ln_jq_app/ios/Podfile.lock index 6fec17e..a681750 100644 --- a/ln_jq_app/ios/Podfile.lock +++ b/ln_jq_app/ios/Podfile.lock @@ -107,6 +107,6 @@ SPEC CHECKSUMS: shared_preferences_foundation: 7036424c3d8ec98dfe75ff1667cb0cd531ec82bb url_launcher_ios: 7a95fa5b60cc718a708b8f2966718e93db0cef1b -PODFILE CHECKSUM: c2b648820a58f7013d9d17db812a119f8affaf6f +PODFILE CHECKSUM: 6416011b1bc721211379eaad259ff1cba3dbfad2 COCOAPODS: 1.16.2 diff --git a/ln_jq_app/ios/Runner/Info.plist b/ln_jq_app/ios/Runner/Info.plist index 51cc9d6..8a7cdf2 100644 --- a/ln_jq_app/ios/Runner/Info.plist +++ b/ln_jq_app/ios/Runner/Info.plist @@ -55,6 +55,8 @@ NSCameraUsageDescription 需要访问您的相机以扫描二维码 NSPhotoLibraryUsageDescription + 需要访问您的相册以选择二维码图片进行识别 + NSPhotoLibraryAddUsageDescription 需要访问您的相册以选择二维码图片进行识别 CADisableMinimumFrameDurationOnPhone diff --git a/ln_jq_app/lib/pages/qr_code/controller.dart b/ln_jq_app/lib/pages/qr_code/controller.dart index 626b5a1..40f0e6d 100644 --- a/ln_jq_app/lib/pages/qr_code/controller.dart +++ b/ln_jq_app/lib/pages/qr_code/controller.dart @@ -125,19 +125,46 @@ class QrCodeController extends GetxController } void requestPermission() async { - final List results = await Future.wait([ - requestCameraPermission(), - requestPhotosPermission(), - ]); + if(Platform.isIOS){ + var status = await Permission.camera.request(); + if (status.isGranted) { + } + else if (status.isPermanentlyDenied) { + openAppSettings(); + } + else { + showErrorToast('需要相机权限才能扫描二维码'); + } + return; + } - final isCameraGranted = results[0]; - final isPhotosGranted = results[1]; - - if (!isCameraGranted) { + final bool results = await requestCameraPermission(); + if (!results) { showErrorToast('相机权限未被授予,请到权限管理中打开'); } - if (!isPhotosGranted) { - showErrorToast('相册权限未被授予,请到权限管理中打开'); + } + + void requestPhotoPermission() async { + if (Platform.isAndroid) { + final bool results = await requestPhotosPermission(); + if (!results) { + showErrorToast('相册权限未被授予,请到权限管理中打开'); + } else { + scanFromGallery(); + } + } + if(Platform.isIOS){ + var status = await Permission.photos.request(); + print("权限状态: $status"); // 在控制台看这个输出 + if (status.isGranted) { + scanFromGallery(); + } + else if (status.isPermanentlyDenied) { + openAppSettings(); + } + else { + showErrorToast('需要相册权限才能从相册中选择图片'); + } } } @@ -161,7 +188,6 @@ class QrCodeController extends GetxController await StorageService.to.saveVehicleInfo(vehicle); Get.back(result: true); - } on DioException catch (e) { showErrorToast("网络请求失败,请稍后重试"); resumeScanner(); @@ -173,7 +199,6 @@ class QrCodeController extends GetxController } } - @override void onClose() { qrViewController?.dispose(); diff --git a/ln_jq_app/lib/pages/qr_code/view.dart b/ln_jq_app/lib/pages/qr_code/view.dart index a2f3b54..df58a84 100644 --- a/ln_jq_app/lib/pages/qr_code/view.dart +++ b/ln_jq_app/lib/pages/qr_code/view.dart @@ -27,7 +27,7 @@ class QrCodePage extends GetView { ), actions: [ TextButton( - onPressed: controller.scanFromGallery, + onPressed: controller.requestPhotoPermission, child: const Text( '相册', style: TextStyle(color: Colors.white, fontSize: 16),