Files
buzz/mobile/ios/Runner/PhotoLibrarySaver.swift
Tom Brow a5b3dbc00d fix(mobile): stop reporting saved images as failed on iOS (#9599)
On iOS, **Save image** in the image viewer reported a failure for a save
that worked. It asks only for permission to add photos. With that
permission the image landed in Photos, but the app showed "Could not
save image" and iOS then asked for full library access. After choosing
**Keep Add Only**, every later save did the same: saved, and reported as
failed.

Now the save reports what actually happened, and add-only permission is
all it needs, so iOS never asks for full access.

| Before | After |
| --- | --- |
| <img
src="https://github.com/user-attachments/assets/5926b595-6a2d-4382-83e5-89329c009159"
alt="Before: full access prompt over a &quot;Could not save image&quot;
message" width="300"> | <img
src="https://github.com/user-attachments/assets/3b797c3e-43c4-4766-950b-8bef415ae2e0"
alt="After: &quot;Saved to Photos&quot; message" width="300"> |

Before:


https://github.com/user-attachments/assets/e4f88b29-cabc-4c43-9967-d8481e434551

After:


https://github.com/user-attachments/assets/ca3631a5-c988-4082-898a-426867788051

### Cause

`photo_manager`'s `saveImage` returns the saved asset, so its iOS code
fetches the new asset back after writing it. PhotoKit does not let an
add-only app fetch assets: the fetch makes iOS show the full-access
prompt and comes back empty, and the plugin reports an error for a write
that succeeded. The latest release (3.12.0) has the same code.

On iOS the image is now written by a small native saver
(`PhotoLibrarySaver`) that creates the asset and reports the result of
that write, without reading the library. Android keeps using
`photo_manager`, and the permission request is unchanged on both
platforms.

### Regressions

None expected.

- The write is the same PhotoKit creation request the plugin made, with
the same original filename, so saved images are unchanged.
- Real failures still show "Could not save image": a denied permission,
a failed download and a failed write all still surface.
- Android and the rest of the app's photo library use (the composer's
photo picker) are untouched.

### Validation

- Added test: the image actions journey saves on iOS with a photo
library that grants add-only access and rejects every read, and expects
"Saved to Photos", one write with the downloaded bytes and filename, and
an add-only permission request. A failed write then shows "Could not
save image". It fails without the fix.
- Full mobile suite and the iOS Runner unit tests pass.
- Full-app run on an iOS 27 simulator, on a fresh install with no photo
permission, comparing this branch with unmodified `main`:
- `main`: first save adds the image, shows "Could not save image" and
the full-access prompt; a second save after **Keep Add Only** adds
another copy and shows the error again.
- This branch: first and second saves each add one image and show "Saved
to Photos", with no full-access prompt, and only add permission is
granted afterwards.
- This branch, other paths: **Don't Allow** shows "Could not save image"
and saves nothing; saving with full access already granted works;
**Share image** still opens the share sheet.
- Not covered: a physical device, and Android (its code path is
unchanged).

---------

Signed-off-by: Tom Brow <tomb@squareup.com>
Co-authored-by: judge <7615ec4b7ddf99f0bb48e913297f99f8f772d289a1c9ef54e1d906dd55a93ba8@buzz.block.builderlab.xyz>
2026-11-18 07:17:27 -08:00

44 lines
1.5 KiB
Swift

import Flutter
import Photos
/// Adds an image to Photos with add-only access.
///
/// PhotoKit lets an add-only app create an asset but never fetch one, so this
/// reports the result of the write itself and never reads the new asset back.
final class PhotoLibrarySaver {
private let channel: FlutterMethodChannel
init(messenger: FlutterBinaryMessenger) {
channel = FlutterMethodChannel(name: "buzz/photo_library_saver", binaryMessenger: messenger)
channel.setMethodCallHandler { call, result in
guard call.method == "saveImage" else {
result(FlutterMethodNotImplemented)
return
}
guard let arguments = call.arguments as? [String: Any],
let bytes = arguments["bytes"] as? FlutterStandardTypedData,
let filename = arguments["filename"] as? String
else {
result(FlutterError(code: "invalid_arguments", message: nil, details: nil))
return
}
PHPhotoLibrary.shared().performChanges {
let options = PHAssetResourceCreationOptions()
options.originalFilename = filename
PHAssetCreationRequest.forAsset().addResource(
with: .photo, data: bytes.data, options: options)
} completionHandler: { saved, error in
DispatchQueue.main.async {
if saved {
result(nil)
} else {
result(
FlutterError(
code: "save_failed", message: error?.localizedDescription, details: nil))
}
}
}
}
}
}