我在项目的info.plist文件中添加了NSPhotoLibraryAddUsageDescription以及NSPhotoLibraryUsageDescription。 尝试创建相册或将图片添加到照片库时,出现以下错误
此应用程序已崩溃,因为它尝试访问没有使用说明的隐私敏感数据。该应用程序的Info.plist必须包含一个NSPhotoLibraryUsageDescription键,该键具有一个字符串值,向用户解释该应用程序如何使用此数据。
我也尝试过一些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>
预先感谢您的帮助。
答案 0 :(得分:1)
最后我已经解决了这个问题,我不知道是什么原因。经过如此多的努力,我有了尝试在不同项目中尝试的想法,因此,我创建了一个新项目,并将info.plist和其他所需的快速而无聊的文件从旧项目复制到新项目,并且可以正常工作。
答案 1 :(得分:0)
打开您的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>
答案 2 :(得分:0)
通过info.plist
或Source Code
打开Property List
。
对于Property List
,您只需添加键NSPhotoLibraryUsageDescription
及其值This app requires access to the photo library.
对于Source Code
,您可以将以下代码添加到plist中。该代码必须在<dict>
和</dict>
之间。
<key>NSPhotoLibraryUsageDescription</key>
<string>This app requires access to the photo library.</string>
键的值实际上是一种解密,因此您可以自己进行描述。但是请确保应用程序的用户可以轻松理解您所做的描述。
对于相机,您可以使用以下代码:
<key>NSCameraUsageDescription</key>
<string>This app requires access to the camera.</string>
更多键:Cocoa keys。