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

标签 ios swift photolibrary phphotolibrary

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

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

我试过一些像 PhotosHelper 这样的 swift 库.as 也可以这样做,但我仍然面临同样的问题

以下是我正在关注的代码

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>

关于ios - 应用程序的 Info.plist 必须包含一个 NSPhotoLibraryUsageDescription 键和一个字符串值,向用户解释应用程序如何使用此数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57859562/

相关文章:

ios - 如何在Axe DevTools中添加全局标签(证明)

ios - 模拟器(iOS)结果不一致?

ios - 如何获取照片库图像数量

ios - 使用信标测距更新 WatchKit 应用程序

ios - 如何将数据传递给 View Controller 并在将其推送到导航堆栈之前加载它?

ios - 获取当前的 iOS 7 状态栏样式

ios - UIImagePicker 自动将照片库中选中的图片保存到 tmp 文件夹中

ios - 导航到下一个和上一个 subview (就像菜单)

swift - 在 Swift 中设置日期样式

iphone - 收到的文件存在于照片库中