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

后端 未结 10 2658
Happy的楠姐
Happy的楠姐 2020-11-29 22:35

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

相关标签:
10条回答
  • Working methods in 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()
    }
    
    0 讨论(0)
  • 2020-11-29 23:13

    Apple Doc 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.

    Converted Apple code in swift 4.2 App delegate i declared

    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
    }
    
    0 讨论(0)
  • 2020-11-29 23:14

    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.

    0 讨论(0)
  • 2020-11-29 23:19

    swift 4.0 version.

    for use custom icon

    first add this line at top of 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)
    }
    
    0 讨论(0)
提交回复
热议问题