应用的 Info.plist 必须包含一个 NSPhotoLibraryUsageDescription 键和一个向用户解释应用如何使用此数据的字符串值

The app's Info.plist must contain an NSPhotoLibraryUsageDescription key with a string value explaining to the user how the app uses this data

我在项目的 info.plist 文件中添加了 NSPhotoLibraryAddUsageDescription 和 NSPhotoLibraryUsageDescription。 尝试创建相册或将图像添加到照片库时出现以下错误

此应用已崩溃,因为它试图在没有使用说明的情况下访问隐私敏感数据。应用程序的 Info.plist 必须包含一个 NSPhotoLibraryUsageDescription 键和一个向用户解释应用程序如何使用此数据的字符串值。

我已经尝试了一些 swift 库,例如 PhotosHelper。我也尝试过这样做,但我仍然面临同样的问题

以下是我正在关注的代码

import UIKit
import Photos

class PhotoLibraryManager
{


private class func fetchAlbumWithName(albumName:String)->PHAssetCollection?
{
    let fetchPredicate = PHFetchOptions()
    fetchPredicate.predicate = NSPredicate(format: "title == '" + albumName + "'")
    let fetchResult = PHAssetCollection.fetchAssetCollections(with: PHAssetCollectionType.album, subtype: PHAssetCollectionSubtype.albumRegular, options: fetchPredicate)
    return fetchResult.firstObject
}

/**
 This function requests for authorization to use the photo gallery and adds the image in the album both of which are specified.If the album does not exist it creates a new one and adds the image in that
 - Parameters:
 - image:The image to be inserted
 - albumName:The name of the album in which the image is to be inserted
 */


class func saveImageToPhone(image:UIImage,albumName:String)
{
    PHPhotoLibrary.requestAuthorization({(status:PHAuthorizationStatus)->Void in
        switch status
        {
        case PHAuthorizationStatus.authorized:
            insertImageAfterAuthorization(image: image,albumName: albumName)
        default:
            print("Unable To Access Photo Library")
        }
    })
}

/**
 This function fetches the specified album from the photo library if present or creates a new one
 - Parameters:
 - image:The image to be inserted
 - albumName:The name of the album in which the image is to be inserted
 */


private class func insertImageAfterAuthorization(image:UIImage,albumName:String)
{
    let album = fetchAlbumWithName(albumName: albumName)
    guard let albumToBeInserted = album else{
        print("Creating A New Album \(albumName)")
        PHPhotoLibrary.shared().performChanges({
            PHAssetCollectionChangeRequest.creationRequestForAssetCollection(withTitle: albumName)
        }, completionHandler: {(success:Bool,error:Error?)->Void in
            guard let errorObj = error else{
                let album = fetchAlbumWithName(albumName: albumName)
                guard let createdAlbum = album else{
                    print("Album Not Created")
                    return
                }
                addImageIntoAlbum(image: image,album: createdAlbum)
                return
            }
            print(errorObj.localizedDescription)
            return
        })
        return
    }
    addImageIntoAlbum(image: image,album: albumToBeInserted)
}

/**
 This function adds an image into the album specifed
 - Parameters:
 - image:The image to be added
 - album:The album in which the image is to inserted
 */

private class func addImageIntoAlbum(image:UIImage,album:PHAssetCollection)
{
    PHPhotoLibrary.shared().performChanges({
        let imgCreationRequest = PHAssetChangeRequest.creationRequestForAsset(from: image)
        print(imgCreationRequest)
        let albumRequest = PHAssetCollectionChangeRequest(for: album)
        guard let albumSpecificationRequest = albumRequest , let placeholderObjForImg = imgCreationRequest.placeholderForCreatedAsset else{
            print("Image Could Not Be Added")
            return
        }
        let arrAlbumSpecificationRequest:NSArray = [placeholderObjForImg]
        albumSpecificationRequest.addAssets(arrAlbumSpecificationRequest)
        //            albumSpecificationRequest.addAssets([placeholderObjForImg])
    }, completionHandler: {(success:Bool,error:Error?)->Void in
        guard let errorObj = error else{
            return
        }
        print(errorObj.localizedDescription)
    })
}

}

这是我的 info.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleDisplayName</key>
<string>NewApp</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
<string>Main</string>
<key>UIRequiredDeviceCapabilities</key>
<array>
    <string>armv7</string>
</array>
<key>UISupportedInterfaceOrientations</key>
<array>
    <string>UIInterfaceOrientationPortrait</string>
    <string>UIInterfaceOrientationLandscapeLeft</string>
    <string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>
<key>NSContactsUsageDescription</key>
<string>Connect People</string>
<key>NSCameraUsageDescription</key>
<string>Video Call</string>
<key>NSMicrophoneUsageDescription</key>
<string>For Audio Call</string>
<key>NSSiriUsageDescription</key>
<string>Siri Uses Test</string>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
    <string>UIInterfaceOrientationPortrait</string>
    <string>UIInterfaceOrientationPortraitUpsideDown</string>
    <string>UIInterfaceOrientationLandscapeLeft</string>
    <string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UIBackgroundModes</key>
<array>
    <string>audio</string>
    <string>fetch</string>
    <string>remote-notification</string>
    <string>voip</string>
</array>
<key>NSPhotoLibraryUsageDescription</key>
<string>Photo Use</string>
<key>NSPhotoLibraryAddUsageDescription</key>
<string>Photo Use</string>
<key>NSUserActivityTypes </key>
<array>
    <string>INStartAudioCallIntent</string>
    <string>INStartVideoCallIntent</string>
</array>
</dict>
</plist>

在此先感谢您的帮助。

打开您的 info.plist 作为源代码并将以下内容粘贴到其中。

<key>NSPhotoLibraryUsageDescription</key>
<string>This app requires access to the photo library.</string>

<key>NSPhotoLibraryAddUsageDescription</key>
<string>This app requires read and write access to the photo library.</string>

通过 Source CodeProperty List 打开您的 info.plist

对于 Property List,您只需添加一个键 NSPhotoLibraryUsageDescription 及其值 This app requires access to the photo library.

对于Source Code,您可以将以下代码添加到plist.And,代码必须在<dict></dict>之间。

<key>NSPhotoLibraryUsageDescription</key>
<string>This app requires access to the photo library.</string>

key的值实际上是一种描述,所以你可以通过youself.But来做一个描述,确保你的应用的用户可以很容易地理解你所做的描述。

对于相机,您可以使用以下代码:

<key>NSCameraUsageDescription</key>
<string>This app requires access to the camera.</string>

更多键:Cocoa keys.

终于解决了,不知道是什么原因。经过这么多的努力,我有了尝试不同项目的想法,所以我创建了一个新项目并将 info.plist 和其他所需的 swift 和故事无聊的文件从旧项目复制到新项目并且它有效。