iOS: chooseFromGallery asks for photo library access before the user picks anything
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 45/100
Hướng nghiên cứu
Start in CameraPlugin.swift at chooseFromGallery, then follow IONCAMRFlowBehaviour.chooseFromGallery to IONCAMRPermissionsBehaviour.checkForPhotoLibrary, which triggers PHPhotoLibrary.requestAuthorization before IONCAMRGalleryBehaviour.displayPhotoLibraryView opens. Reproduce on the iOS simulator with xcrun simctl privacy <udid> reset all <bundle id> and confirm the prompt appears. Done means a photo-only pick opens PHPickerViewController with no Photos prompt, while video and edit keep their current flow; the proposed patch in the issue is a starting point, and the scope choice needs maintainer input.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Plugin version: 8.2.6 (ion-ios-camera 2.0.0), iOS 15+. Related to #74, which covers the picker UI. This issue is about the permission prompt.
What happens. chooseFromGallery on iOS asks the user for photo library access before anything opens. When the user taps "Limit Access" or "Don't Allow", the app gets a partial library or an error, although the user only wanted to pick one photo.
Call order (ion-ios-camera 2.0.0):
CameraPlugin.chooseFromGallerycallsgalleryManager.chooseFromGallery(with:).IONCAMRFlowBehaviour.chooseFromGallerycallspermissionsBehaviour.checkForPhotoLibrary.IONCAMRPermissionsBehaviour.checkForPhotoLibrarycallsPHPhotoLibrary.requestAuthorization(for: .readWrite), which shows the system prompt the first time.- Only then
IONCAMRGalleryBehaviour.displayPhotoLibraryViewpresents the customIONCAMRPhotoLibraryView, built onPHAsset.fetchAssets. That grid really does need the authorization.
No option and no Info.plist key avoids step 3. The app has to declare NSPhotoLibraryUsageDescription and the user has to grant library access to pick a photo.
Why this matters. Since iOS 14, PHPickerViewController runs out of process and needs no photo library authorization. The app receives only the items the user picks. Apple's guidance is to use the system picker when an app only needs the photos the user selects, and to ask for library access only when the app needs the library itself. Before 8.1, getPhoto with CameraSource.Photos and pickImages presented PHPickerViewController, and the plugin still has that code (presentPhotoPicker). The authorization request in those older paths (showPhotos) was not needed either.
Minimal repro
- Fresh install, or reset privacy:
xcrun simctl privacy <udid> reset all <bundle id>. - Call
Camera.chooseFromGallery({ mediaType: MediaTypeSelection.Photo, allowMultipleSelection: false }). - Expected: the system photo picker opens with no prompt. Actual: "Allow access to your photos?" (Limit Access / Allow Full Access / Don't Allow) appears first.
Suggested fix. For photo-only picks without an edit step, present PHPickerViewController. Build it with PHPickerConfiguration() and no photoLibrary, so no asset identifier comes back and nothing touches PhotoKit. Load each pick with itemProvider.loadFileRepresentation(forTypeIdentifier: UTType.image.identifier) and copy it to a temporary file. That keeps the photo's own EXIF, including capture time and location unless the user turns location off in the picker's options. Return it as a MediaResult. We carry this as a local patch to CameraPlugin.swift (under 100 lines), tested on the iOS simulator: no prompt, and the app gets no Photos authorization. The patch is below. Keeping the custom grid for video and for edit, as the patch does, is one option; using the system picker for those too would be another.
Local patch (patch-package, CameraPlugin.swift)
diff --git a/node_modules/@capacitor/camera/ios/Sources/CameraPlugin/CameraPlugin.swift b/node_modules/@capacitor/camera/ios/Sources/CameraPlugin/CameraPlugin.swift
index 968733e..aea7dba 100644
--- a/node_modules/@capacitor/camera/ios/Sources/CameraPlugin/CameraPlugin.swift
+++ b/node_modules/@capacitor/camera/ios/Sources/CameraPlugin/CameraPlugin.swift
@@ -35,6 +35,8 @@ public class CameraPlugin: CAPPlugin, CAPBridgedPlugin {
private lazy var videoManager = IONCAMRFactory.createVideoManagerWrapper(withDelegate: self, and: self.bridge?.viewController ?? UIViewController())
private var imageCounter = 0
+ // patch-package: PHPickerViewController holds its delegate weakly.
+ private var galleryPhotoPickerDelegate: GalleryPhotoPickerDelegate?
public override func load() {
NotificationCenter.default.addObserver(
@@ -85,10 +87,35 @@ public class CameraPlugin: CAPPlugin, CAPBridgedPlugin {
@objc func chooseFromGallery(_ call: CAPPluginCall) {
handleCall(call, error: .chooseMultimediaIssue) { (options: IONCAMRGalleryOptions) in
- self.galleryManager.chooseFromGallery(with: options)
+ // patch-package: photos without an edit step go through the system
+ // PHPickerViewController. It runs out of process and needs no photo
+ // library authorization, while IONCameraLib's own grid asks for full
+ // read-write access before it opens. Video and edit keep that flow.
+ if options.mediaType == .picture && !options.allowEdit {
+ self.presentGalleryPhotoPicker(options)
+ } else {
+ self.galleryManager.chooseFromGallery(with: options)
+ }
}
}
+ private func presentGalleryPhotoPicker(_ options: IONCAMRGalleryOptions) {
+ // No photoLibrary in the configuration: no asset identifiers, so
+ // nothing here touches PhotoKit and no authorization prompt shows.
+ var configuration = PHPickerConfiguration()
+ configuration.filter = .images
+ configuration.selectionLimit = options.allowMultipleSelection ? options.limit : 1
+ let picker = PHPickerViewController(configuration: configuration)
+ let delegate = GalleryPhotoPickerDelegate(
+ webPath: { [weak self] url in self?.resolveWebPath(from: url.absoluteString) },
+ fail: { [weak self] error in self?.sendError(error) },
+ finish: { [weak self] results in self?.call?.resolve(["results": results]) }
+ )
+ galleryPhotoPickerDelegate = delegate
+ picker.delegate = delegate
+ bridge?.viewController?.present(picker, animated: true, completion: nil)
+ }
+
@objc func editURIPhoto(_ call: CAPPluginCall) {
handleCall(call, error: .editPictureIssue) { (options: IONCAMRPhotoEditOptions) in
self.editManager.editPhoto(with: options)
@@ -735,3 +762,63 @@ extension CameraPlugin: IONCAMRCallbackDelegate {
return exif.isEmpty ? nil : exif
}
}
+
+// patch-package: the chooseFromGallery photo path. Each picked item is copied
+// as the file the picker hands over (its own bytes and metadata, the capture
+// time and the position included unless the user turns them off in the
+// picker), in pick order. A cancel rejects with chooseMultimediaCancel; an
+// item that cannot be read fails the whole call with chooseMultimediaIssue.
+final class GalleryPhotoPickerDelegate: NSObject, PHPickerViewControllerDelegate {
+ private let webPath: (URL) -> String?
+ private let fail: (IONCAMRError) -> Void
+ private let finish: ([[String: Any]]) -> Void
+
+ init(webPath: @escaping (URL) -> String?, fail: @escaping (IONCAMRError) -> Void, finish: @escaping ([[String: Any]]) -> Void) {
+ self.webPath = webPath
+ self.fail = fail
+ self.finish = finish
+ }
+
+ func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
+ picker.dismiss(animated: true, completion: nil)
+ guard !results.isEmpty else {
+ fail(.chooseMultimediaCancel)
+ return
+ }
+ var copies = [URL?](repeating: nil, count: results.count)
+ let lock = NSLock()
+ let group = DispatchGroup()
+ for (index, result) in results.enumerated() {
+ group.enter()
+ result.itemProvider.loadFileRepresentation(forTypeIdentifier: UTType.image.identifier) { url, error in
+ defer { group.leave() }
+ guard let url else {
+ CAPLog.print("chooseFromGallery: item \(index) unreadable: \(String(describing: error))")
+ return
+ }
+ let copy = FileManager.default.temporaryDirectory
+ .appendingPathComponent(UUID().uuidString)
+ .appendingPathExtension(url.pathExtension.isEmpty ? "jpg" : url.pathExtension)
+ do {
+ try FileManager.default.copyItem(at: url, to: copy)
+ lock.lock()
+ copies[index] = copy
+ lock.unlock()
+ } catch {
+ CAPLog.print("chooseFromGallery: item \(index) copy failed: \(error)")
+ }
+ }
+ }
+ group.notify(queue: .main) { [self] in
+ var items: [[String: Any]] = []
+ for copy in copies {
+ guard let copy, let path = webPath(copy) else {
+ fail(.chooseMultimediaIssue)
+ return
+ }
+ items.append(["type": 0, "uri": copy.absoluteString, "webPath": path, "saved": false])
+ }
+ finish(items)
+ }
+ }
+}
- Ngôn ngữ chính
- TypeScript
- Star
- 6
- Fork
- 4
- Merge trung bình
- 2 ngày 11 giờ
- Pull request đã merge (30 ngày)
- 6
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của ionic-team/capacitor-camera
-
bug(android): NullPointerException on ActivityResultLauncher.unregister() in LegacyCameraFlowĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
ionic-team/capacitor-camera#75 · 1 reaction ·
-
[Bug]: Crash SecurityException: No persistable permission grants found in IONCAMROpenPhotoPickerActivityCó thể làm lại được Pull request cho issue này đã bị đóng mà không được merge. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
ionic-team/capacitor-camera#58 · 1 bình luận ·
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 67/100
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 68/100
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 68/100
ionic-team/capacitor-camera#76 · 1 bình luận · 1 reaction ·
Tất cả issue của ionic-team/capacitor-camera
Issue tương tự
-
community first-timers-only good first issue hacktoberfest help wanted low hanging fruit up-for-grabs
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 85/100
lingdojo/kana-dojo#34223 · 1 bình luận · 5 reaction ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug status: needs triage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
mastra-ai/mastra#30587 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
area:web p2 product:business triage:active type:bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
cuadrao-ai/argus#2228 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
embedpdf/embed-pdf-viewer#1098 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
.Needs Triage .Team/Metabot Priority:P3 Type:Bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 73/100
metabase/metabase#87252 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày