在PHPhotoLibrary中禁用对删除请求的确认 [英] Disable confirmation on delete request in PHPhotoLibrary

查看:204
本文介绍了在PHPhotoLibrary中禁用对删除请求的确认的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要做的是将视频保存到PHPhotoLibrary,然后在上传到应用程序中的客户端远程服务器时将其删除(基本上,照片库充当临时存储,以便在任何情况下添加额外的安全层失败(我已将我的视频保存在应用程序目录中)。

What I am trying to do is to save videos to PHPhotoLibrary, and then remove them when upload to clients remote server in the application completes (basically, photo library serves as temporary storage to add additional layer of security in case anything at all fails (I already save my vides it in the applications directory).

问题:

问题是要工作,一切都必须在没有用户输入的情况下工作。你可以像这样写照片库:

The problem is for that to work, everything has to work without input from the user. You can write video to photos library like this:

func storeVideoToLibraryForUpload(upload : SMUpload) {

    if PHPhotoLibrary.authorizationStatus() != PHAuthorizationStatus.Authorized {

        // Don't write to library since this is disallowed by user
        return
    }

    PHPhotoLibrary.sharedPhotoLibrary().performChanges({ () -> Void in

        // Write asset
        let assetRequest = PHAssetChangeRequest.creationRequestForAssetFromVideoAtFileURL(NSURL(fileURLWithPath: upload.nonsecureFilePath!)!)
        let assetPlaceholder = assetRequest.placeholderForCreatedAsset
        let localIdentifier = assetPlaceholder.localIdentifier

        // Store local identifier for later use
        upload.localAssetIdentifier = localIdentifier

    }, completionHandler: { (success, error) -> Void in
        ....
    })
}

这完美无瑕,我得到本地标识符,我存储它供以后使用..独角兽和彩虹。

And that works flawlessly, I get local identifier, I store it for later use.. Unicorns and rainbows.

现在当我想在上传完成后立即删除该视频时,我打电话给:

Now when I want to remove that video immediately after upload finishes, I call following:

func removeVideoFromLibraryForUpload(upload : SMUpload) {

    // Only proceed if there is asset identifier (video previously stored)
    if let assetIdentifier = upload.localAssetIdentifier {

        // Find asset that we previously stored
        let assets = PHAsset.fetchAssetsWithLocalIdentifiers([assetIdentifier], options: PHFetchOptions())

        // Fetch asset, if found, delete it
        if let fetchedAssets = assets.firstObject as? PHAsset {

            PHPhotoLibrary.sharedPhotoLibrary().performChanges({ () -> Void in

                // Delete asset
                PHAssetChangeRequest.deleteAssets([fetchedAssets])

            }, completionHandler: { (success, error) -> Void in
                ...
            })
        } 
    }
}

成功删除视频,但用户必须先确认删除。这是一个问题,因为备份将不起作用。

Which successfully deletes the video, BUT user have to confirm deletion first. That is a problem as that backing up won't work.

我显然知道为什么有确认(所以你不清除整个用户库,但是事情是,我的应用程序制作了视频 - 所以我认为会有解决方法,因为作为所有者我不应该这样做,或者至少可以选择禁用确认。

I obviously know why there is confirmation (so you don't clear entire user library for example, but the thing is, My app made the video - and so I thought there will be way around it, since as an "owner" I should not be doing that, or at least have option to disable confirmation.

提前致谢!

TLDR :如果我的应用程序创建了该内容,如何禁用删除请求的确认?(我不想删除任何其他内容。)

TLDR: How can I disable confirmation on delete request, if my application created that content? (I don't want to delete anything else).

注意:有人可能会说这是相当奇怪的事情,但是应用程序是内部分发的,并且有充分的理由这样做(视频内容太宝贵而不会丢失,即使用户出于某种原因删除了应用程序,或者根本没有任何问题,我们需要能够保留视频),所以请不要质疑,只关注你的注意力关于这个问题:)

Note: Somebody can probably say this is rather strange thing to do but the application is distributed internally and there is good reason to do it like this (the video content is too valuable to be lost, even if user deletes the application for some reason, or there is anything at all that goes wrong, we need to be able to preserve the videos), so please don't question that and just focus your attention on the question :)

推荐答案

我看不到避免删除确认的方法。它是Photos框架的一个实现细节,类似于当你的应用程序试图使用它时,你无法阻止设备询问用户是否允许使用麦克风的方式,这是一个安全问题。相信。将资产保存到设备照片库后,您的应用不再是该资产的所有者,因此,正如您在问题中所述,设备必须确保该应用在获取此类数据之前已获得用户的许可。

I cannot see a way to avoid the delete confirmation. It is an implementation detail of the Photos framework, similar to the way you cannot prevent the device from asking the user's permission to use the microphone when your app tries to use it, and is a matter of security & trust. Once you have saved an asset to the device photo library your app is no longer the owner of that asset, so as you noted in your question the device must of course ensure the app has the user's permission before it goes about deleting such data.

您永远无法完全保护用户数据免受其不可预测的行为影响 - 如果他们决定删除您的应用,或从照片中删除特定资产,则由他们。我认为您最好的选择是提供内置删除确认,或者为用户提供指南,明确表示他们应该小心保护这些重要数据,并备份设备,删除应用程序!

You can never entirely safeguard your users' data against their own unpredictable behaviour - if they decide to remove your app, or delete a particular asset from within Photos, it is up to them. I think your best option is to either put up with the built-in delete confirmation, or to provide a guide to your users that makes it clear that they should be careful to protect this important data by backing up their device, and not deleting the app!

如果您决定坚持这种方法,也许您可​​以做的最好的事情是为用户做好准备他们的设备可能会要求他们确认删除正在上传到您自己的服务器的文件。例如,在尝试删除资产之前,先建立自己的模态警报。我通常不会建议公共运输应用程序使用这种方法,但由于您只是在内部分发,因此您的团队可能会接受。

If you did decide to stick to this approach, perhaps the best thing you could do is to prepare the user for the fact that their device may ask them for confirmation to delete a file that is being uploaded to your own servers. For example, put up your own modal alert just before trying to delete the asset. I wouldn't normally suggest that kind of approach for a public shipping app, but since you're only distributing internally it may be acceptable for your team.

这篇关于在PHPhotoLibrary中禁用对删除请求的确认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆