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

feat(ios): support custom image as privacy screen #66

Merged
merged 8 commits into from
Sep 11, 2023
Merged
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
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ npx cap sync

These configuration values are available:

| Prop | Type | Description | Default |
| ------------ | -------------------- | ------------------------------------------------------------------------------------------------ | ----------------- |
| **`enable`** | <code>boolean</code> | Configure whether the plugin should be enabled from startup. Only available for Android and iOS. | <code>true</code> |
| Prop | Type | Description | Default |
| --------------- | -------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------- |
| **`enable`** | <code>boolean</code> | Configure whether the plugin should be enabled from startup. Only available for Android and iOS. | <code>true</code> |
| **`imageName`** | <code>string</code> | Configure whether the plugin should display a custom image from assets instead of a default background gray for the privacy screen. Only available for iOS. | <code>""</code> |

### Examples

Expand All @@ -60,7 +61,8 @@ In `capacitor.config.json`:
{
"plugins": {
"PrivacyScreen": {
"enable": true
"enable": true,
"imageName": "Splashscreen"
}
}
}
Expand All @@ -77,6 +79,7 @@ const config: CapacitorConfig = {
plugins: {
PrivacyScreen: {
enable: true,
imageName: "Splashscreen",
},
},
};
Expand Down
40 changes: 34 additions & 6 deletions ios/Plugin/PrivacyScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,18 @@ import UIKit
self.config = config

self.privacyViewController = UIViewController()
self.privacyViewController!.view.backgroundColor = UIColor.gray
if self.config.imageName == "" || self.config.imageName == nil {
self.privacyViewController!.view.backgroundColor = UIColor.gray
} else {
self.privacyViewController!.view.backgroundColor = UIColor.systemBackground
var imageView = UIImageView()
imageView.frame = CGRect(x: 0, y: 0, width: self.privacyViewController!.view.bounds.width, height: self.privacyViewController!.view.bounds.height)
imageView.contentMode = .center
imageView.clipsToBounds = true
let imageBackground = UIImage(named: self.config.imageName)
imageView.image = imageBackground
self.privacyViewController!.view.addSubview(imageView)
}
self.privacyViewController!.modalPresentationStyle = UIModalPresentationStyle.overFullScreen

super.init()
Expand All @@ -26,7 +37,7 @@ import UIKit
@objc public func enable(completion: (() -> Void)?) {
self.isEnabled = true
DispatchQueue.main.async {
self.plugin.bridge?.webView?.disableScreenshots()
self.plugin.bridge?.webView?.disableScreenshots(self.config.imageName)
completion?()
}
}
Expand Down Expand Up @@ -61,14 +72,31 @@ import UIKit
}

public extension WKWebView {
func disableScreenshots() {
addSecureText()
addSecureText()
func disableScreenshots(_ imageName: String = "") {
addSecureText(imageName)
addSecureText(imageName)
}

func addSecureText() {
func addSecureText(_ imageName: String = "") {
let field = UITextField()
field.isSecureTextEntry = true
if imageName != "" {
let imageBackground = UIImage(named: imageName)
field.backgroundColor = UIColor.systemBackground

var backgroundView = UIView()
backgroundView.backgroundColor = UIColor.systemBackground
backgroundView.frame = self.bounds
field.addSubview(backgroundView)

var imageView = UIImageView()
imageView.frame = self.bounds
imageView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
imageView.contentMode = .center
imageView.clipsToBounds = true
imageView.image = imageBackground
field.addSubview(imageView)
}
field.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(field)
field.centerYAnchor.constraint(equalTo: self.topAnchor).isActive = true
Expand Down
1 change: 1 addition & 0 deletions ios/Plugin/PrivacyScreenConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ import Foundation

public struct PrivacyScreenConfig {
var enable = true
var imageName = ""
}
1 change: 1 addition & 0 deletions ios/Plugin/PrivacyScreenPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public class PrivacyScreenPlugin: CAPPlugin {
private func getPrivacyScreenConfig() -> PrivacyScreenConfig {
var config = PrivacyScreenConfig()
config.enable = getConfig().getBoolean("enable", config.enable)
config.imageName = getConfig().getString("imageName", config.imageName)!
return config
}
}
9 changes: 9 additions & 0 deletions src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ declare module '@capacitor/cli' {
* @example true
*/
enable?: boolean;
/**
* Configure whether the plugin should display a custom image from assets instead of a default background gray for the privacy screen.
*
* Only available for iOS.
*
* @default ""
* @example "Splashscreen"
*/
imageName?: string;
};
}
}
Expand Down
Loading