如何检查iOS11屏幕录制是开还是关?

时间:2017-09-15 06:02:20

标签: ios swift ios11

为了检测iOS11屏幕录制功能On或Off我使用了isCaptured和UIScreenCapturedDidChange Notification。

当我第一次启动App和On iOS11内置屏幕录制功能时,它会通知值为True的选择器方法,但当我终止(终止)我正在运行的应用程序和启动应用程序再次执行相同的过程然后我的选择器方法没有被调用。

这是我的代码:

我在ViewWillAppear()方法中添加了一个Observer:

NotificationCenter.default.addObserver(self, selector: #selector(handleNotification), name: NSNotification.Name.UIScreenCapturedDidChange, object: nil)

选择器方法如下:

@objc
func handleNotification(notification:Notification){

    let isCaptured = UIScreen.main.isCaptured

    print("isCaptured value = \(isCaptured)")
}

在这种情况下,我需要终止应用程序,清除缓存并再次启动应用程序以获取屏幕录制事件。

请建议我可以在此处检测录制事件,以保护我的内容不被录制。

5 个答案:

答案 0 :(得分:3)

Swift 4

添加观察者

UIScreen.main.addObserver(self, forKeyPath: "captured", options: .new, context: nil)

接收更改

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey: Any]?, context: UnsafeMutableRawPointer?) {
    if (keyPath == "captured") {
        let isCaptured = UIScreen.main.isCaptured

        print(isCaptured)
    }
}

答案 1 :(得分:1)

我猜你无论通知如何都可以检查这个变量

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    let isCaptured = UIScreen.main.isCaptured
    return true
}

答案 2 :(得分:0)

这是检测是否已截取屏幕截图的方法

int

答案 3 :(得分:0)

此功能在iOS11及更高版本上可用。最好将其保留在 didFinishLaunchingWithOptions

Objective-C语法

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: 
 (NSDictionary *)launchOptions
{
if (@available(iOS 11.0, *)) {
    BOOL isCaptured = [[UIScreen mainScreen] isCaptured];
    if(isCaptured){
       // Do the action for hiding the screen recording
                  } 
   } else{
        // Fallback on earlier versions
         }
 return YES;
}

答案 4 :(得分:0)

@UmeshKumath这很容易,当杀死应用程序清除缓存并再次启动该应用程序以获取屏幕记录事件时,您需要像这样在viewdidload中运行代码:

override func viewDidLoad() {
    super.viewDidLoad()
UIScreen.main.addObserver(self, forKeyPath: "some key", options: .new, context: nil)
    let isCaptured = UIScreen.main.isCaptured
    if isCaptured == true {
        // do something
    }