前言
升级iOS11系统的手机使用我们app的拍照功能时闪退,经过查看代码发现是 writeImageToSavedPhotosAlbum
这个方法引起的
官方文档这是这样解释的:
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.
这是一个iOS11版本新增的权限,涉及保存图片到相册时需要添加该权限。
iOS11以前:
NSPhotoLibraryUsageDescription:访问相册和存储照片到相册(读写),会出现用户授权。
iOS11之后:
NSPhotoLibraryUsageDescription:无需添加。默认开启访问相册权限(读),无需用户授权。
NSPhotoLibraryAddUsageDescription: 添加内容到相册(写),会出现用户授权。
解决办法
原来是跟 iOS 10 保存图片、调用相机一样, Info.plist 里面要涉及隐私数据时要添加一句“提示语”。于是打开 Info.plist,点击 + 号,在 Key 中输入:Privacy - Photo Library Additions Usage Description,Type 选择 String,Value 中输入你的提示语。再次 Build,运行,OK!