Skip to content

Commit

Permalink
Merge pull request #443 from JuyeonYu/feat-screen-protection
Browse files Browse the repository at this point in the history
image protection from taking screenshot using `UITextField` `isSecureTextEntry`.
  • Loading branch information
tommyming authored Mar 8, 2024
2 parents fd0e07d + 686da71 commit 5d94d49
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,13 @@ SKPhotoBrowserOptions.swapCloseAndDeleteButtons = true // no
SKPhotoBrowserOptions.closeAndDeleteButtonPadding = 20 // set offset from top and from nearest screen edge of close button and delete button
```

#### Screenshot Protection
You can protect your image from taking screenshot via SKPhotoBrowserOptions
Only working on the device, not on simulator
```swift
SKPhotoBrowserOptions.protectScreenshot = true // image will be hidden after taking screenshot
```

#### Custom Cache From Web URL
You can use SKCacheable protocol if others are adaptable. (SKImageCacheable or SKRequestResponseCacheable)

Expand Down
4 changes: 4 additions & 0 deletions SKPhotoBrowser.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
9CF727E1285208180043487A /* UIImage+BundledImage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9CF727E0285208180043487A /* UIImage+BundledImage.swift */; };
A64B89361CB04222000071B9 /* SKPhotoBrowserTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A64B89351CB04222000071B9 /* SKPhotoBrowserTests.swift */; };
A64B89381CB04222000071B9 /* SKPhotoBrowser.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8909B5301BC791280060A053 /* SKPhotoBrowser.framework */; };
D6690B1E2A8F580B00AA18EA /* UIView+ScreenshotProtection.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6690B1D2A8F580B00AA18EA /* UIView+ScreenshotProtection.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -83,6 +84,7 @@
A64B89331CB04222000071B9 /* SKPhotoBrowserTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SKPhotoBrowserTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
A64B89351CB04222000071B9 /* SKPhotoBrowserTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SKPhotoBrowserTests.swift; sourceTree = "<group>"; };
A64B89371CB04222000071B9 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
D6690B1D2A8F580B00AA18EA /* UIView+ScreenshotProtection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIView+ScreenshotProtection.swift"; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -160,6 +162,7 @@
9CF727E0285208180043487A /* UIImage+BundledImage.swift */,
9521CC61232F677900446D11 /* UIImage+Rotation.swift */,
9521CC62232F677900446D11 /* UIView+Radius.swift */,
D6690B1D2A8F580B00AA18EA /* UIView+ScreenshotProtection.swift */,
);
path = extensions;
sourceTree = "<group>";
Expand Down Expand Up @@ -333,6 +336,7 @@
8917B1B41D5A14B0000CE1C4 /* SKButtons.swift in Sources */,
89C24A821D657AD1005F09A9 /* SKPhotoBrowserOptions.swift in Sources */,
9CF727E1285208180043487A /* UIImage+BundledImage.swift in Sources */,
D6690B1E2A8F580B00AA18EA /* UIView+ScreenshotProtection.swift in Sources */,
26C97AD51D0EB6870039F6CB /* SKCache.swift in Sources */,
8909B5431BC791510060A053 /* SKCaptionView.swift in Sources */,
8909B5491BC791510060A053 /* SKPhotoBrowser.swift in Sources */,
Expand Down
4 changes: 4 additions & 0 deletions SKPhotoBrowser/SKPhotoBrowser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,10 @@ private extension SKPhotoBrowser {
func configurePagingScrollView() {
pagingScrollView.delegate = self
view.addSubview(pagingScrollView)

if SKPhotoBrowserOptions.protectScreenshot {
paginationView.protectScreenshot()
}
}

func configureGestureControl() {
Expand Down
4 changes: 4 additions & 0 deletions SKPhotoBrowser/SKPhotoBrowserOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ public struct SKPhotoBrowserOptions {

/// Provide custom session configuration (eg. for headers, etc.)
public static var sessionConfiguration: URLSessionConfiguration = .default

/// if this value is true, when you take a screenshot, the image will be hidden
/// only working on a device, not on simulator
public static var protectScreenshot: Bool = false
}

public struct SKButtonOptions {
Expand Down
24 changes: 24 additions & 0 deletions SKPhotoBrowser/extensions/UIView+ScreenshotProtection.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// UIView+ScreenshotProtection.swift
// SKPhotoBrowser
//
// Created by  JuyeonYu on 2023/08/18.
// Copyright © 2023 suzuki_keishi. All rights reserved.
//

import Foundation

extension UIView {
func protectScreenshot() {
DispatchQueue.main.async {
let textField = UITextField()
textField.isSecureTextEntry = true
self.addSubview(textField)
textField.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
textField.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
textField.layer.removeFromSuperlayer()
self.layer.superlayer?.insertSublayer(textField.layer, at: 0)
textField.layer.sublayers?.last?.addSublayer(self.layer)
}
}
}

0 comments on commit 5d94d49

Please sign in to comment.