c# - 观察并删除(截图),如果在Iphone中使用我的App时截图

标签 c# xamarin.ios visual-studio-2019

我正在尝试观察在 Iphone 上使用我的应用程序时是否截取了屏幕截图。如果在使用我的应用程序时截取了屏幕截图,我希望删除该屏幕截图。

我也了解到,在删除过程中,需要用户给予删除权限。

我成功地使用了 Observer 方法来检查在使用我的应用程序时是否截取了屏幕截图。

我被困在需要访问该屏幕截图并将其删除的地步,当然要获得用户许可。

```public override void OnActivated(UIApplication application)
    {
        try
        {
            // Start observing screenshot notification
            if (_screenshotNotification == null)
            {
                _screenshotNotification = NSNotificationCenter.DefaultCenter.AddObserver(UIApplication.UserDidTakeScreenshotNotification,
                            (NSNotification n) => {

Console.WriteLine("UserTookScreenshot");                                    

var photosOptions = new PHFetchOptions();                                   

photosOptions.SortDescriptors = new NSSortDescriptor[] { new 

NSSortDescriptor("creationDate", false) };                                  

photosOptions.FetchLimit = 1;                                                                       

var photo = PHAsset.FetchAssets(photosOptions);                                 

Console.WriteLine(photo);                                   

var filePath = photo.Path;                                  

System.IO.File.Delete(filePath);                                    

n.Dispose();
                                       }
                     );
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            Console.WriteLine(ex.StackTrace);
        }
    }```

我知道上面的代码不适用于删除使用我的应用程序时截取的当前屏幕截图。它给出了我想要实现的目标的总体思路。

如何在Iphone 中立即删除使用我的APP 时截取的屏幕截图(在用户许可的情况下)?我也想知道这是否可能。

最佳答案

是的,这是可能的,您显然必须正确管理图像的权限。为了做到这一点,首先你必须添加一个观察者来检测屏幕截图,如图所示 here

First: declare an NSObject variable on your AppDelegate. In the example below I added _screenshotNotification.

Second: On the AppDelegate’s OnActivated (app moving to active state), add code to start observing the UIApplication.UserDidTakeScreenshotNotification and then do something once the notification is posted.

Third: On the AppDelegate’s OnResignActivation (app moving to inactive state), add code to remove the observer.

然后您必须实际找到文件并删除,因此您在尝试执行此操作的页面中,只需添加 Foundation & Photos using 语句,然后尝试此操作。 (我从 here 转换了 Swift,但还没有测试过)

var fetchOptions = new PHFetchOptions();
fetchOptions.SortDescriptors[0] = new Foundation.NSSortDescriptor("creationDate", true);
var fetchResult = PHAsset.FetchAssets(PHAssetMediaType.Image, fetchOptions);
if (fetchResult != null)
{
             var lastAsset = (fetchResult.LastObject as PHAsset);
             var arrayToDelete = new PHAsset[1] { lastAsset };
             PHPhotoLibrary.SharedPhotoLibrary.PerformChanges(() => { PHAssetChangeRequest.DeleteAssets(arrayToDelete); },
                                                              async (bool success, NSError errorMessage) => { }); //Handle Completion Here Appropriately 
}

关于c# - 观察并删除(截图),如果在Iphone中使用我的App时截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56731764/

相关文章:

c# - Resharper ToString 格式化完成

c# - 在 C# 中为 Windows Vista/7 显示身份验证对话框

c# - 如何在 xamarin ui 测试中给出 ipa 文件的路径

带有另一张图像的 Xamarin iOS 事件指示器

visual-studio-2019 - VS 2019更新到16.8.4打破了我的每一个解决方案

c# - Sharepoint 更新列表方法 : Newly created Columns are not visible

c# - 最大的 "float"小于等于 `int.MaxValue`

visual-studio-2019 - 如何在 VS 2019 中保存启动项目配置?

.net - MonoTouch 和 MonoDroid 中的 PortableDLL

visual-studio-2019 - 如何在 Visual Studio 中增加调试器悬停字体大小?