IOS11 flashing back when taking photos, and flashing back when saving pictures

Problem Description

Recently, testers reported that the mobile phone upgraded to iOS11 system crashed when using the camera function of our app.
After some searching, it was found that it was caused by the following code:
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

problem solved

  1. At first I thought that the calling method of the function has changed, but when calling it in different places, or passing in different parameters, it will still crash;
  2. Later, I used a clean demo project for debugging, and found that such an error message that did not appear during the previous crash occurred during the crash:

    [access] This app has crashed because it attempted to access privacy-sensitive data without a usage description. The app’s Info.plist must contain an NSPhotoLibraryAddUsageDescription key with a string value explaining to the user how the app uses this data.

  3. According to the error message, after adding the NSPhotoLibraryAddUsageDescription property to the plist file, the crash problem no longer occurs.

problem causes


  1. After solving the problem, I looked up the Apple development documents, the official documents described it like this:

NSPhotoLibraryAddUsageDescription
NSPhotoLibraryAddUsageDescription (String - iOS) This key lets you describe the reason your app seeks write-only access to the user’s photo library. When the system prompts the user to allow access, this string is displayed as part of the alert.
Important: To protect user privacy, an iOS app linked on or after iOS 10.0, and that accesses the user’s photo library, must statically declare the intent to do so. Include the NSPhotoLibraryAddUsageDescription key (in apps that link on or after iOS 11) or NSPhotoLibraryUsageDescription key in your app’s Info.plist file and provide a purpose string for the key. If your app attempts to access the user’s photo library without a corresponding purpose string, your app exits.
This key is supported in iOS 11.0 and later.
Source: official document

This is a new permission in the iOS11 version, which needs to be added when saving pictures to the album.
2. Since there are few official explanations, I found other information on the Internet:

Before iOS11:
NSPhotoLibraryUsageDescription: When accessing the album and storing photos in the album (read and write), user authorization will appear.
After iOS11:
NSPhotoLibraryUsageDescription: No need to add. The permission to access the album (read) is turned on by default, and no user authorization is required.
NSPhotoLibraryAddUsageDescription: Add content to the album (write), user authorization will appear.
Source: csdn blog

Guess you like

Origin blog.csdn.net/jhq1990/article/details/78323730