【发布时间】:2020-01-11 13:09:34
【问题描述】:
我在项目的 info.plist 文件中添加了 NSPhotoLibraryAddUsageDescription 和 NSPhotoLibraryUsageDescription。 尝试创建相册或将图像添加到照片库时出现以下错误
此应用程序已崩溃,因为它试图在没有使用说明的情况下访问隐私敏感数据。应用的 Info.plist 必须包含一个 NSPhotoLibraryUsageDescription 键和一个字符串值,向用户解释应用如何使用这些数据。
我已经尝试了一些像 PhotosHelper.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 中添加了 NSPhotoLibraryUsageDescription 键?
-
更新您的问题,显示您的 Info.plist 的相关部分。
-
这可能是 plist 问题而不是代码问题。正如 rmaddy 所说,请出示您的 plist。
-
您为 NSCameraUsageDescription 添加的字符串值是多少?
-
我用 info.plist 文件更新了问题
标签: ios swift photolibrary phphotolibrary