我需要禁止在iOS设备上拍摄屏幕截图。由于Apple不为此提供支持,因此我尝试尝试另一种解决方法-使用UIApplicationUserDidTakeScreenshotNotification
拍摄屏幕快照时收到通知,并从相册中获取最后一张照片,并按以下代码将其删除-
func deleteLastTakenScreenshotFromLibrary() {
let fetchOptions: PHFetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
let fetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions)
if (fetchResult.firstObject != nil) {
var firstAsset: PHAsset = fetchResult.firstObject as! PHAsset
let arrayToDelete = NSArray(object: firstAsset)
PHPhotoLibrary.shared().performChanges( {
PHAssetChangeRequest.deleteAssets(arrayToDelete)},
completionHandler: {
success, error in
print("Finished deleting asset. %@", (success ? "Success" : error))
})
}
但是在这里我发现了2个问题-
a).app正在请求用户许可,例如Allow <App Name> to delete this photo? This photo will be deleted from iCloud Photo Library on all your devices.
b)。点击allow
后,应用不会永久删除最后一张照片。
即使我在info.plist
1)。Privacy - Photo Library Usage Description
2)。Privacy - Media Library Usage Description
我不知道如何满足此要求。请帮助,非常感谢您的帮助。谢谢。 :)
答案 0 :(得分:0)
摘自PHPhotoLibrary
的文档:
对于每次对performChanges(:completionHandler :)或performChangesAndWait( :)方法的调用,Photos都会显示一条警报,询问用户是否有权编辑照片库的内容。