ios 防止iOS在进入后台之前截取应用程序的屏幕截图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7520076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:36:12  来源:igfitidea点击:

Prevent iOS from taking screen capture of app before going into background

iphoneiosipadmultitasking

提问by Mobilewits

You all might know that iOS takes a screen shot of your application before throwing it into the background. This is usually for a better User experience like quick animation to bring the app back and so on. I don't want my app screen shot to be stored on the device, but I want the multitasking to still exist.

大家可能都知道 iOS 在将应用程序放入后台之前会对其进行屏幕截图。这通常是为了更好的用户体验,例如快速动画以将应用程序带回来等等。我不希望我的应用程序屏幕截图存储在设备上,但我希望多任务处理仍然存在。

I came out with a solution but I'm not sure if I'm heading in the right direction. So, when the applicationDidEnterBackgroundis called -- I put in an overlay image that will be captured by the OS, and once the app enters foreground, I will remove the overlay. I'm not sure if this is going to work but I'm on my way to implement this. Meanwhile, any other thoughts on this will help me figure out the optimal way of attacking this issue.

我想出了一个解决方案,但我不确定我是否朝着正确的方向前进。因此,当applicationDidEnterBackground调用时——我放入了一个将由操作系统捕获的覆盖图像,一旦应用程序进入前台,我将删除覆盖。我不确定这是否会奏效,但我正在努力实现这一点。同时,对此的任何其他想法都将帮助我找出解决此问题的最佳方法。

回答by Ole Begemann

You are on the right track. This is Apple's recommended way to do this as noted in the iOS Application Programming Guide:

你走在正确的轨道上。这是 Apple 推荐的方法,如 iOS 应用程序编程指南中所述

Remove sensitive information from views before moving to the background.When an application transitions to the background, the system takes a snapshot of the application's main window, which it then presents briefly when transitioning your application back to the foreground. Before returning from your applicationDidEnterBackground:method, you should hide or obscure passwords and other sensitive personal information that might be captured as part of the snapshot.

在移至后台之前从视图中删除敏感信息。当应用程序转换到后台时,系统会拍摄应用程序主窗口的快照,然后在将应用程序转换回前台时简要显示该窗口。在从您的applicationDidEnterBackground:方法返回之前,您应该隐藏或隐藏密码和其他可能作为快照一部分捕获的敏感个人信息。

回答by Deepak Kumar

Need to write the code in Application life cycle methods, here we are putting an imageViewwhile the app animate to background :

需要在应用程序生命周期方法中编写代码,这里我们将imageView应用程序动画放置到后台:

-(void)applicationWillResignActive:(UIApplication *)application
{
    imageView = [[UIImageView alloc]initWithFrame:[self.window frame]];
    [imageView setImage:[UIImage imageNamed:@"Splash_Screen.png"]];
    [self.window addSubview:imageView];
}

Here is the code to remove the imageView:

这是删除 的代码imageView

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    if(imageView != nil) {
        [imageView removeFromSuperview];
        imageView = nil;
    }
}

It is working and properly tested.

它正在工作并经过正确测试。

回答by Lily

I came across the same issue, and my research has lead me to the following answers:

我遇到了同样的问题,我的研究使我得出以下答案:

  • set a blurry screen overlay before the app goes in the background and once the app becomes active remove this overlay

  • if it is iOS 7 or later you can use the function ignoreSnapshotOnNextApplicationLaunch

  • 在应用程序进入后台之前设置一个模糊的屏幕叠加层,一旦应用程序变为活动状态,请删除此叠加层

  • 如果是 iOS 7 或更高版本,您可以使用函数 ignoreSnapshotOnNextApplicationLaunch

See in apple documentation: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplication_Class/Reference/Reference.html#//apple_ref/occ/instm/UIApplication/ignoreSnapshotOnNextApplicationLaunch

在苹果文档中查看:https: //developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplication_Class/Reference/Reference.html#//apple_ref/occ/instm/UIApplication/ignoreSnapshotOnNextApplicationLaunch

I hope this helps somebody.

我希望这对某人有帮助。

回答by Krumelur

Your approach is exactly the correct and only way to do it. Place an overlay view and remove it later. It is valid to do this if your app shows sensitive data that you don't want to be cached in image format anywhere.

您的方法是完全正确且唯一的方法。放置一个覆盖视图并稍后将其删除。如果您的应用程序显示您不希望以图像格式缓存在任何地方的敏感数据,则执行此操作是有效的。

回答by Ourang-Zeb Khan

Apple Doc https://developer.apple.com/library/archive/qa/qa1838/_index.html

苹果文档 https://developer.apple.com/library/archive/qa/qa1838/_index.html

Note:Your implementation of -applicationDidEnterBackground: should not start any animations (pass NO to any animated: parameter). The snapshot of your application's window is captured immediately upon returning from this method. Animations will not complete before the snapshot is taken.

注意:您的 -applicationDidEnterBackground: 实现不应启动任何动画(将 NO 传递给任何动画:参数)。从此方法返回后,将立即捕获应用程序窗口的快照。在拍摄快照之前动画不会完成。

Converted Apple code in swift 4.2 App delegate i declared

在我声明的 swift 4.2 App 委托中转换了 Apple 代码

func applicationDidEnterBackground(_ application: UIApplication) {
    // Your application can present a full screen modal view controller to
    // cover its contents when it moves into the background. If your
    // application requires a password unlock when it retuns to the
    // foreground, present your lock screen or authentication view controller here.

    let blankViewController = UIViewController()
    blankViewController.view.backgroundColor = UIColor.black

    // Pass NO for the animated parameter. Any animation will not complete
    // before the snapshot is taken.
    window.rootViewController?.present(blankViewController, animated: false)
}

func applicationWillEnterForeground(_ application: UIApplication) {
    // This should be omitted if your application presented a lock screen
    // in -applicationDidEnterBackground:
    window.rootViewController?.dismiss(animated: false) false
}

回答by Xeieshan

Improvement in Depak Kumar post : Make a property UIImage *snapShotOfSplash;

Depak Kumar 帖子的改进:创建一个属性 UIImage *snapShotOfSplash;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[UIApplication sharedApplication] ignoreSnapshotOnNextApplicationLaunch];
snapShotOfSplash =[UIImage imageNamed:@"splash_logo"];
}

- (void)applicationDidEnterBackground:(UIApplication *)application {


    self.overlayView = [[UIImageView alloc]initWithFrame:[self.window frame]];
    self.overlayView.backgroundColor = [UIColor whiteColor];
    [self.overlayView setImage:snapShotOfSplash];
    [self.overlayView setContentMode:UIViewContentModeCenter];
    [self.window addSubview:self.overlayView];
    [self.window bringSubviewToFront:self.overlayView]; }

- (void)applicationDidBecomeActive:(UIApplication *)application {
if(self.overlayView != nil) {
        [self.overlayView removeFromSuperview];
        self.overlayView = nil;
    }
}

回答by Agisight

Working methods in AppDelegate, swift 4.2:

AppDelegate 中的工作方法,swift 4.2:

func blurScreen(style: UIBlurEffect.Style = UIBlurEffect.Style.regular) {
    screen = UIScreen.main.snapshotView(afterScreenUpdates: false)
    let blurEffect = UIBlurEffect(style: style)
    let blurBackground = UIVisualEffectView(effect: blurEffect)
    screen?.addSubview(blurBackground)
    blurBackground.frame = (screen?.frame)!
    window?.addSubview(screen!)
}

func removeBlurScreen() {
    screen?.removeFromSuperview()
}

Where is:

哪里:

weak var screen : UIView? = nil // property of the AppDelegate

Call these methods in needed delegate methods:

在需要的委托方法中调用这些方法:

func applicationWillResignActive(_ application: UIApplication) {
    blurScreen()
}

func applicationDidBecomeActive(_ application: UIApplication) {
    removeBlurScreen()
}

回答by Ankit Aman

Implementation with some animation while going in background and reverse action

在后台运行和反向操作时使用一些动画实现

   - (void)applicationWillResignActive:(UIApplication *)application
{
     //     fill screen with our own colour
        UIView *colourView = [[UIView alloc]initWithFrame:self.window.frame];
        colourView.backgroundColor = [UIColor blackColor];
        colourView.tag = 1111;
        colourView.alpha = 0;
        [self.window addSubview:colourView];
        [self.window bringSubviewToFront:colourView];

        // fade in the view
        [UIView animateWithDuration:0.5 animations:^{
            colourView.alpha = 1;
        }];

}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // grab a reference to our coloured view
    UIView *colourView = [self.window viewWithTag:1111];
    // fade away colour view from main view
    [UIView animateWithDuration:0.5 animations:^{
        colourView.alpha = 0;
    } completion:^(BOOL finished) {
        // remove when finished fading
        [colourView removeFromSuperview];
    }];

 }

回答by blacker

swift 4.0 version.

快速 4.0 版本。

for use custom icon

使用自定义图标

first add this line at top of AppDelegate

首先在 AppDelegate 顶部添加这一行

var imageView: UIImageView?

and add this:

并添加这个:

func applicationDidEnterBackground(_ application: UIApplication) {
    imageView = UIImageView(frame: window!.frame)
    imageView?.image = UIImage(named: "AppIcon")
    window?.addSubview(imageView!)
}

func applicationWillEnterForeground(_ application: UIApplication) {
    if imageView != nil {
        imageView?.removeFromSuperview()
        imageView = nil
    }
}

background with black color

黑色背景

func applicationDidEnterBackground(_ application: UIApplication) {
    let blankViewController = UIViewController()
    blankViewController.view.backgroundColor = UIColor.black
    window?.rootViewController?.present(blankViewController, animated: false)
}

func applicationWillEnterForeground(_ application: UIApplication) {
    window?.rootViewController?.dismiss(animated: false)
}

回答by Michael Bazzoni

In iOS 7 you could use the allowScreenShotto stop the ability all together.

在 iOS 7 中,您可以使用allowScreenShot来停止所有功能。

See: Apple Developer: Configuration Profile Reference:

请参阅:Apple 开发人员:配置文件参考

allowScreenShot


Boolean
Optional. If set to false, users can't save a screenshot of the display and are prevented from capturing a screen recording; it also prevents the Classroom app from observing remote screens. Defaults to true.

Availability:Updated in iOS 9.0 to include screen recordings.