xoail
xoail

Reputation: 3064

Mask view on top of video when enters background

Edit: It is not a duplicate of flagged link. If you look closely the question is different.

How to display mask view on top of a video when it enters background? I am working on a passcode protected content app that requires a mask screen to hide the contents when it enters background (so that double press Home button does not reveal the contents or video). My mask view logic work fine if there is no video playing but fails on any video (such as YouTube), a video snap shot is clearly visible.

Need to mask the video from background

This post talks about the problem in general but not when a video is being played. With a video the problem still exists. The code I have so far.

//AppDelegate.swift

var maskView: UIView!

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {        
    maskView.backgroundColor = UIColor.redColor()
    return true
}
func applicationDidEnterBackground(application: UIApplication) {
    window?.addSubview(maskView)
}

func applicationWillEnterForeground(application: UIApplication) {
    maskView.removeFromSuperview()
}

Upvotes: 3

Views: 863

Answers (1)

rkyr
rkyr

Reputation: 3241

Your problem is that you use wrong method. applicationDidEnterBackground not being called when you push home button twice. Instead of this method, I suggest you use applicationWillResignActive which will be called even if user open NotificationCenter. I test your case with code below.

// appDelegate:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
  maskView = UIView(frame: UIScreen.mainScreen().bounds)
  maskView.backgroundColor = UIColor.redColor()
  return true
}

func applicationWillResignActive(application: UIApplication) {
  window?.addSubview(maskView)
}

func applicationWillEnterForeground(application: UIApplication) {
  maskView.removeFromSuperview()
}

// PlayerViewController
@IBOutlet weak var videoView: PlayerView! // root view of current controller
let player = AVPlayer(URL: NSURL(string: "http://www.ebookfrenzy.com/ios_book/movie/movie.mov")!)

override func viewDidLoad() {
  videoView.setPlayer(player)
}

override func viewDidAppear(animated: Bool) {
  player.play()
}

// PlayerView
override class func layerClass() -> AnyClass {
  return AVPlayerLayer.classForCoder()
}

func setPlayer(player: AVPlayer) {
  (layer as! AVPlayerLayer).player = player
}

UPD: my guess wasn't successful because you don't give me more information. But now I figure it out: problem is that when you play embedded video it create new window that overlap yours. And inside this window you have player with bunch of different views (you can see this information with pviews command of chisel tool). To solve this situation I use code below. (AppDelegate)

var maskViews: [UIView] = []

func maskView() -> UIView {
    let view = UIView(frame: UIScreen.mainScreen().bounds)
    view.backgroundColor = UIColor.redColor()
    return view
}

func applicationWillResignActive(application: UIApplication) {
    for window in UIApplication.sharedApplication().windows {
        let mask = maskView()
        maskViews.append(mask)
        window.addSubview(mask)
    }
}

func applicationDidBecomeActive(application: UIApplication) {
    for view in maskViews {
        view.removeFromSuperview()
    }
    maskViews.removeAll()
}

Upvotes: 3

Related Questions