투케이2K

23. (ios/swift) 화면 캡쳐 (capture) 발생 시 경고 창 호출 실시 - capturedDidChangeNotification 본문

IOS

23. (ios/swift) 화면 캡쳐 (capture) 발생 시 경고 창 호출 실시 - capturedDidChangeNotification

투케이2K 2021. 10. 23. 22:43
반응형

[개발 환경 설정]

개발 툴 : XCODE

개발 언어 : SWIFT


[소스 코드]

//
//  SceneDelegate.swift
//  testCode
//
//  Created by lotecs on 2021/10/16.
//

import UIKit

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
        // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
        // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
        guard let _ = (scene as? UIWindowScene) else { return }
    }
    func sceneDidDisconnect(_ scene: UIScene) {
        // Called as the scene is being released by the system.
        // This occurs shortly after the scene enters the background, or when its session is discarded.
        // Release any resources associated with this scene that can be re-created the next time the scene connects.
        // The scene may re-connect later, as its session was not necessarily discarded (see `application:didDiscardSceneSessions` instead).
    }

    func sceneDidBecomeActive(_ scene: UIScene) {
        // Called when the scene has moved from an inactive state to an active state.
        // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
    }
    func sceneWillResignActive(_ scene: UIScene) {
        // Called when the scene will move from an active state to an inactive state.
        // This may occur due to temporary interruptions (ex. an incoming phone call).
    }

    
    
    // MARK: [화면 캡쳐 이벤트 발생 체크를 위한 노티피케이션 센터 등록]
    func sceneWillEnterForeground(_ scene: UIScene) {
        print("")
        print("===============================")
        print("[SceneDelegate >> sceneWillEnterForeground]")
        print("[설명 : Scene 포그라운드 실행]")
        print("===============================")
        print("")
        
        /*
        [화면 캡쳐 및 녹화 기능 설명]
        1. ios 는 캡쳐 기능을 원칙적으로 막을 수 없어 대체 방안으로 캡쳐 이벤트 발생 시 경고 창을 표시해줍니다
        2. 노티피케이션 센터를 통해 옵저버를 추가하고 캡쳐나 녹화가 일어날때 팝업창 호출 이벤트를 발생시킵니다
        */

        // [노티피케이션 센터를 통해 옵저버를 추가하고 캡쳐나 녹화가 일어날때 셀렉터 메소드 호출]
        NotificationCenter.default.addObserver(self, selector: #selector(alertScreenCapture(notification:)), name: UIApplication.userDidTakeScreenshotNotification, object: nil)
        
        NotificationCenter.default.addObserver(self, selector: #selector(alertScreenCapture(notification:)), name: UIScreen.capturedDidChangeNotification, object: nil)
    }
    
    
    
    // MARK: [화면 캡쳐 이벤트 발생 시 팝업창 호출 부분]
    @objc func alertScreenCapture(notification:Notification) -> Void {
        print("===============================")
        print("[SceneDelegate >> alertScreenCapture]")
        print("설명 : 캡쳐 및 녹화 이벤트 발생 실시")
        print("===============================")
        let alert = UIAlertController(title: "주의", message: "캡쳐 혹은 녹화를 하면 안됩니다.", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "확인", style: .default, handler: nil))
        self.window?.rootViewController!.present(alert, animated: true, completion: nil)
    }
    

    func sceneDidEnterBackground(_ scene: UIScene) {
        // Called as the scene transitions from the foreground to the background.
        // Use this method to save data, release shared resources, and store enough scene-specific state information
        // to restore the scene back to its current state.
    }

}

 


[결과 출력]


 

반응형
Comments