iOS11で追加されるScreen Recordingを使うと映像コンテンツを録画することができてしまいます。リリースしたアプリの中に録画されるとマズいアプリがあったので、対応方法を探していたのですが、iOS11 beta4から対応できることになったので紹介します。
環境
Xcode9 beta 4
iOS11 beta 4
コード
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let screen = UIScreen.main
screen.addObserver(self, forKeyPath: "captured", options: [.new, .old], context: nil)
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
guard let kp = keyPath else { return }
if kp == "captured" {
guard let ch = change else { return }
let new = ch[NSKeyValueChangeKey.newKey] as! Bool
let old = ch[NSKeyValueChangeKey.oldKey] as! Bool
if new && !old {
//録画開始
} else if !new && old {
//録画終了
}
}
}