Bug 60420 - UIImage.SaveToPhotosAlbum(...) crashes silently.
Summary: UIImage.SaveToPhotosAlbum(...) crashes silently.
Status: RESOLVED ANSWERED
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: XI 11.2 (d15-4)
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2017-10-27 17:58 UTC by Joel Wetzel
Modified: 2017-11-10 10:53 UTC (History)
4 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED ANSWERED

Description Joel Wetzel 2017-10-27 17:58:20 UTC
App crashes silently when calling:

uiImage.SaveToPhotosAlbum((image, error) =>
{
    Console.WriteLine("error:" + error);
});

It never hits the Console.WriteLine statement or throws an Exception.  This code has been in production for over a year without issue, but started crashing when updating Visual Studio for Mac and Xamarin.iOS to the latest stable versions.
Comment 1 Sebastien Pouliot 2017-10-27 19:11:17 UTC
The related code has not changed in the last 18 months. From your description I suspect your application was re-compiled with iOS 10+ SDK and this will make iOS checks for extra permissions (in the Info.plist) to access some resources.

Without the required permissions (likely [1] in your case) iOS will crash the running process (not very developer friendly) when some API are called (like `UIImageWriteToSavedPhotosAlbum`).

If this is not the case then please re-open the bug and
a. attach the crash report and the device logs at crash time;
b. attach a self contained test case so we can reproduce;

[1] https://stackoverflow.com/a/39694916/220643
Comment 2 Randy 2017-11-07 19:29:46 UTC
I'm seeing a similar problem. I have code that would save an image to the photos library. I have the info.plist entry and when settings/privacy/photos shows my app is allowed read/write permission, it works. If I change the setting to 'Never' I would expect an error and get it on ios 9. On ios 11 my app crashes instead.

The code is:

 image.SaveToPhotosAlbum ((i, e) =>
 {
   if (e != null)
     MessageBox.Show (Res.DlgErrorSavingImage, e.LocalizedDescription);
 });

(MessageBox.Show just calls UIAlertView to display the error message. A breakpoint on the 'if' line never executes.)

I also see a problem after a factory reset on a device or simulator. If the first thing I do is call SaveToPhotosAlbum, the app crashes on ios 11. I don't get the permission dialog to allow access. If I first use UIImagePickerController to select an image, I get the permission dialog and can then use SaveToPhotosAlbum later.

It's like SaveToPhotosAlbum won't present the first time permission dialog if my app isn't listed in settings/private/photos and if it is there with permission='Never' it crashes instead of calling the exception function like it should.
Comment 3 Randy 2017-11-07 19:31:28 UTC
A simple way to test is to take any working app that calls SaveToPhotosAlbum and change the privacy permission to Never. The app will crash on ios11 and call the callback on older ios versions. The crash also can't be caught with a try/catch block.
Comment 4 kevinmicallef 2017-11-09 21:58:20 UTC
There's a new Privacy Key in iOS 11 for write-access to photos.  Edit your info.plist and add the following key 'NSPhotoLibraryAddUsageDescription'

https://www.cultofmac.com/496314/how-apple-made-photos-app-even-more-private-ios-11/

https://developer.apple.com/library/content/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html
Comment 5 Randy 2017-11-10 10:53:35 UTC
My app needs both read and write access to the photo library and already has NSPhotoLibraryUsageDescription in info.plist. The problem is if the very first thing it does is try to write to the library it crashes. If the first access is to read, it gets the allow dialog and then read and write work. Also if the setting is later changed from 'read and write' to 'never', the next attempt to write will crash the app. It's supposed to fail and call the callback function with the error.

The description for NSPhotoLibraryAddUsageDescription says it is for when you only want write access and not read access.

Using just NSPhotoLibraryUsageDescription should have been sufficient according to the documentation and using both could have even caused a problem. The cocoa docs imply you should only use one of these.

However, setting both keys in info.plist is the solution in this case. If the first call is to write a photo, it gets an allow dialog and sets the permission to 'write only'. Then a call to read a photo will get another allow dialog and update the permission to 'read and write'. Switching the permission to 'never' will now get error callbacks properly too instead of crashing.