Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add foreground/background events to privacy plugin #110

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions ios/Plugin/PrivacyScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import UIKit
private var isEnabled = false
private var window: UIWindow?
private var screenPrevent = UITextField()

private var privacyProtectionWindow: UIWindow?

init(plugin: PrivacyScreenPlugin, config: PrivacyScreenConfig, window: UIWindow?) {
self.plugin = plugin
self.config = config
Expand Down Expand Up @@ -59,10 +60,32 @@ import UIKit
}
}

@objc public func handleWillResignActiveNotification() {
private func showPrivacyProtectionWindow() {
guard let windowScene = self.window?.windowScene else {
return
}

self.privacyProtectionWindow = UIWindow(windowScene: windowScene)
self.privacyProtectionWindow?.rootViewController = self.privacyViewController
self.privacyProtectionWindow?.windowLevel = .alert + 1
self.privacyProtectionWindow?.makeKeyAndVisible()
}

private func hidePrivacyProtectionWindow() {
self.privacyProtectionWindow?.isHidden = true
self.privacyProtectionWindow = nil
}

@objc public func showPrivacyController() {
guard self.isEnabled else {
return
}

if(self.config.foregroundBackgroundEvent ?? false) {
self.showPrivacyProtectionWindow()
return;
}

DispatchQueue.main.async {
let window = UIApplication.shared.connectedScenes
.filter({$0.activationState == .foregroundActive || $0.activationState == .foregroundInactive})
Expand All @@ -81,8 +104,14 @@ import UIKit
}
}
}


@objc public func handleDidBecomeActiveNotification() {

@objc public func hidePrivacyController() {
if(self.config.foregroundBackgroundEvent ?? false) {
self.hidePrivacyProtectionWindow()
return;
}
DispatchQueue.main.async {
self.privacyViewController.dismiss(animated: false, completion: nil)
}
Expand Down
3 changes: 2 additions & 1 deletion ios/Plugin/PrivacyScreenConfig.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import Foundation

public struct PrivacyScreenConfig {
var enable = true
var enable: Bool = true
var imageName: String? = ""
var contentMode: String? = "center"
var preventScreenshots: Bool? = true
var foregroundBackgroundEvent: Bool? = false
}
37 changes: 28 additions & 9 deletions ios/Plugin/PrivacyScreenPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,22 @@ public class PrivacyScreenPlugin: CAPPlugin {
override public func load() {
let config = getPrivacyScreenConfig()
self.implementation = PrivacyScreen(plugin: self, config: config, window: UIApplication.shared.delegate?.window as? UIWindow)

if(config.foregroundBackgroundEvent ?? false) {
NotificationCenter.default.addObserver(self, selector: #selector(self.handleWillEnterForegroundNotification),
name: UIScene.willEnterForegroundNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.handleDidEnterBackgroundNotification),
name: UIScene.didEnterBackgroundNotification, object: nil)

NotificationCenter.default.addObserver(self, selector: #selector(self.handleDidBecomeActiveNotification),
name: UIApplication.didBecomeActiveNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.handleWillResignActiveNotification),
name: UIApplication.willResignActiveNotification, object: nil)
} else {
NotificationCenter.default.addObserver(self, selector: #selector(self.handleDidBecomeActiveNotification),
name: UIApplication.didBecomeActiveNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.handleWillResignActiveNotification),
name: UIApplication.willResignActiveNotification, object: nil)
}



NotificationCenter.default.addObserver(self, selector: #selector(self.handleCapturedDidChangeNotification),
name: UIScreen.capturedDidChangeNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.handleUserDidTakeScreenshotNotification),
Expand All @@ -40,13 +51,20 @@ public class PrivacyScreenPlugin: CAPPlugin {
call.resolve()
})
}


@objc private func handleDidBecomeActiveNotification() {
implementation?.hidePrivacyController()
}

@objc private func handleWillResignActiveNotification() {
implementation?.handleWillResignActiveNotification()
implementation?.showPrivacyController()
}

@objc private func handleDidBecomeActiveNotification() {
implementation?.handleDidBecomeActiveNotification()
@objc private func handleWillEnterForegroundNotification() {
implementation?.hidePrivacyController()
}
@objc private func handleDidEnterBackgroundNotification() {
implementation?.showPrivacyController()
}

@objc private func handleCapturedDidChangeNotification() {
Expand All @@ -71,6 +89,7 @@ public class PrivacyScreenPlugin: CAPPlugin {
config.imageName = getConfig().getString("imageName", config.imageName)
config.contentMode = getConfig().getString("contentMode", config.contentMode)
config.preventScreenshots = getConfig().getBoolean("preventScreenshots", true)
config.foregroundBackgroundEvent = getConfig().getBoolean("foregroundBackgroundEvent", false)
return config
}
}
}
12 changes: 12 additions & 0 deletions src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ declare module '@capacitor/cli' {
* @since 5.2.0
*/
preventScreenshots?: boolean;
/**
* Configure whether the plugin should use Foreground/Background events [UIScene for iOS 13+) for triggering.
*
* Only available for iOS.
*
* @default false
* @example false
* @since 5.2.0
* @see https://developer.apple.com/documentation/uikit/uiscene/3197922-didenterbackgroundnotification
* @see https://developer.apple.com/documentation/uikit/uiscene/3197925-willenterforegroundnotification/
*/
foregroundBackgroundEvent?: boolean;
};
}
}
Expand Down