Skip to content

Commit

Permalink
Initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
0xWDG committed Jul 23, 2024
0 parents commit 4376794
Show file tree
Hide file tree
Showing 13 changed files with 390 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/build-documentation.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# .github/workflows/build-documentation.yml

name: build-documentation

on:
# Run on push to main branch
push:
branches:
- main

# Dispatch if triggered using Github (website)
workflow_dispatch:

jobs:
Build-documentation:
runs-on: macos-latest
steps:
- name: Build documentation
uses: 0xWDG/build-documentation@main
20 changes: 20 additions & 0 deletions .github/workflows/build-multiplatform.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# .github/workflows/build-multiplatform.yml

name: Build-Packages

on:
# Run on pull_request
pull_request:

# Dispatch if triggered using Github (website)
workflow_dispatch:

jobs:
Build-Packages:
runs-on: macos-latest
steps:
- name: Build Swift Packages
uses: 0xWDG/build-swift@main
with:
# watchOS is unsupported
watchOS: false
16 changes: 16 additions & 0 deletions .github/workflows/swiftlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Run Swiftlint
on:
push:
pull_request:
workflow_dispatch:

jobs:
swiftlint:
runs-on: macos-latest
steps:
- uses: actions/checkout@v3

- name: SwiftLint
run: |
brew install swiftlint
swiftlint --reporter github-actions-logging --strict
23 changes: 23 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Run tests on Linux and macOS
on:
push:
workflow_dispatch:

jobs:
test_linux:
if: true
runs-on: ubuntu-latest
continue-on-error: true
steps:
- uses: actions/checkout@v3

- name: Swift test
run: swift test

test_macos:
runs-on: macos-latest
steps:
- uses: actions/checkout@v3

- name: Swift test
run: swift test
63 changes: 63 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
## User settings
xcuserdata/

## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9)
*.xcscmblueprint
*.xccheckout

## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4)
build/
DerivedData/
*.moved-aside
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3

## Obj-C/Swift specific
*.hmap

## App packaging
*.ipa
*.dSYM.zip
*.dSYM

## Playgrounds
timeline.xctimeline
playground.xcworkspace

### Swift Package Manager
Packages/
Package.pins
Package.resolved
# *.xcodeproj
#
# Xcode automatically generates this directory with a .xcworkspacedata file and xcuserdata
# hence it is not needed unless you have added a package configuration file to your project
.swiftpm
.build/

### CocoaPods
Pods/
*.xcworkspace

### Carthage
Carthage/Checkouts
Carthage/Build/

### Accio dependency management
Dependencies/
.accio/

### fastlane
fastlane/report.xml
fastlane/Preview.html
fastlane/screenshots/**/*.png
fastlane/test_output

### Code Injection
iOSInjectionProject/
4 changes: 4 additions & 0 deletions .spi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
version: 1
builder:
configs:
- documentation_targets: [preventScreenshot]
16 changes: 16 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
excluded:
- "*resource_bundle_accessor*" # SwiftPM Generated
- ".build/*"

opt_in_rules:
- missing_docs
- empty_count
- empty_string
- toggle_bool
- unused_optional_binding
- valid_ibinspectable
- modifier_order
- first_where
- fatal_error_message
- force_unwrapping

21 changes: 21 additions & 0 deletions LICENCE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Wesley de Groot, [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
29 changes: 29 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// swift-tools-version: 5.8.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "preventScreenshot",
platforms: [
.iOS(.v15),
.macOS(.v12),
.tvOS(.v14),
.macCatalyst(.v15)
],
products: [
// Products define the executables and libraries a package produces, making them visible to other packages.
.library(
name: "preventScreenshot",
targets: ["preventScreenshot"])
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
// Targets can depend on other targets in this package and products from dependencies.
.target(
name: "preventScreenshot"),
.testTarget(
name: "preventScreenshotTests",
dependencies: ["preventScreenshot"])
]
)
55 changes: 55 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# preventScreenshot

preventScreenshot allows you to prevents screenshots of a SwiftUI Element.

[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2F0xWDG%2FpreventScreenshot%2Fbadge%3Ftype%3Dplatforms)](https://swiftpackageindex.com/0xWDG/preventScreenshot)
[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2F0xWDG%2FpreventScreenshot%2Fbadge%3Ftype%3Dswift-versions)](https://swiftpackageindex.com/0xWDG/preventScreenshot)
[![Swift Package Manager](https://img.shields.io/badge/SPM-compatible-brightgreen.svg)](https://swift.org/package-manager)
![License](https://img.shields.io/github/license/0xWDG/preventScreenshot)

## Requirements

- Swift 5.9+ (Xcode 15+)
- iOS 13+, macOS 10.15+

## Installation (Pakage.swift)

```swift
dependencies: [
.package(url: "https://github.com/0xWDG/preventScreenshot.git", .branch("main")),
],
targets: [
.target(name: "MyTarget", dependencies: [
.product(name: "preventScreenshot", package: "preventScreenshot"),
]),
]
```

## Installation (Xcode)

1. In Xcode, open your project and navigate to **File****Swift Packages****Add Package Dependency...**
2. Paste the repository URL (`https://github.com/0xWDG/preventScreenshot`) and click **Next**.
3. Click **Finish**.

## Usage

Example to read a ImageView (Multi platform):

```swift
import SwiftUI
import preventScreenshot

struct ContentView: View {
var body: some View {
VStack {
Text("Don't take a screenshot of this")
.preventScreenshot()
}
.padding()
}
}
```

## Contact

We can get in touch via [Twitter/X](https://twitter.com/0xWDG), [Discord](https://discordapp.com/users/918438083861573692), [Mastodon](https://iosdev.space/@0xWDG), [Email](mailto:[email protected]), [Website](https://wesleydegroot.nl).
88 changes: 88 additions & 0 deletions Sources/preventScreenshot/preventScreenshot.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
//
// Inspect.swift
// Inspect
//
// Created by Wesley de Groot on 25/06/2024.
// https://wesleydegroot.nl
//
// https://github.com/0xWDG/preventScreenshot
// MIT LICENCE
//

import Foundation
import SwiftUI

#if os(macOS)
public typealias PlatformView = NSView

Check failure on line 16 in Sources/preventScreenshot/preventScreenshot.swift

View workflow job for this annotation

GitHub Actions / swiftlint

public declarations should be documented (missing_docs)
public typealias PlatformTextField = NSSecureTextField

Check failure on line 17 in Sources/preventScreenshot/preventScreenshot.swift

View workflow job for this annotation

GitHub Actions / swiftlint

public declarations should be documented (missing_docs)
public typealias PlatformRepresentable = NSViewRepresentable

Check failure on line 18 in Sources/preventScreenshot/preventScreenshot.swift

View workflow job for this annotation

GitHub Actions / swiftlint

public declarations should be documented (missing_docs)
public typealias PlatformHostingController = NSHostingController

Check failure on line 19 in Sources/preventScreenshot/preventScreenshot.swift

View workflow job for this annotation

GitHub Actions / swiftlint

public declarations should be documented (missing_docs)
#else
public typealias PlatformView = UIView

Check failure on line 21 in Sources/preventScreenshot/preventScreenshot.swift

View workflow job for this annotation

GitHub Actions / swiftlint

public declarations should be documented (missing_docs)
public typealias PlatformTextField = UITextField

Check failure on line 22 in Sources/preventScreenshot/preventScreenshot.swift

View workflow job for this annotation

GitHub Actions / swiftlint

public declarations should be documented (missing_docs)
public typealias PlatformRepresentable = UIViewRepresentable

Check failure on line 23 in Sources/preventScreenshot/preventScreenshot.swift

View workflow job for this annotation

GitHub Actions / swiftlint

public declarations should be documented (missing_docs)
public typealias PlatformHostingController = UIHostingController

Check failure on line 24 in Sources/preventScreenshot/preventScreenshot.swift

View workflow job for this annotation

GitHub Actions / swiftlint

public declarations should be documented (missing_docs)
#endif

/// Screenshot Prevent Wrapper
public struct ScreenshotPreventWrapper<Content: View>: PlatformRepresentable {
let content: () -> Content

public init(@ViewBuilder content: @escaping () -> Content) {
self.content = content
}

public func makeNSView(context: Context) -> NSView {
return self.makeUIView(context: context)
}

public func makeUIView(context: Context) -> PlatformView {
let secureTextField = PlatformTextField()

#if !os(macOS)
secureTextField.isSecureTextEntry = true
secureTextField.isUserInteractionEnabled = false
#endif

guard let secureView = secureTextField.layer?.sublayers?.first?.delegate as? PlatformView else {
return PlatformView()
}

secureView.subviews.forEach { subview in
subview.removeFromSuperview()
}

let hostedContent = PlatformHostingController(rootView: content())
#if os(macOS)
// TODO: This seems not to work
hostedContent.view.layer?.backgroundColor = .clear
#else
hController.view.backgroundColor = .clear
#endif
hostedContent.view.translatesAutoresizingMaskIntoConstraints = false

secureView.addSubview(hostedContent.view)
NSLayoutConstraint.activate([
hostedContent.view.topAnchor.constraint(equalTo: secureView.topAnchor),
hostedContent.view.bottomAnchor.constraint(equalTo: secureView.bottomAnchor),
hostedContent.view.leadingAnchor.constraint(equalTo: secureView.leadingAnchor),
hostedContent.view.trailingAnchor.constraint(equalTo: secureView.trailingAnchor)
])

return secureView
}

public func updateNSView(_ nsView: PlatformView, context: Context) { }
public func updateUIView(_ uiView: PlatformView, context: Context) { }
}


extension View {
@ViewBuilder public func preventScreenshot() -> some View {

Check failure on line 81 in Sources/preventScreenshot/preventScreenshot.swift

View workflow job for this annotation

GitHub Actions / swiftlint

public declarations should be documented (missing_docs)
ScreenshotPreventWrapper {
self
.redacted(reason: .privacy)
.privacySensitive()
}
}
}
12 changes: 12 additions & 0 deletions Tests/preventScreenshotTests/preventScreenshotTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import XCTest
@testable import Inspect

final class preventScreenshotTests: XCTestCase {

Check failure on line 4 in Tests/preventScreenshotTests/preventScreenshotTests.swift

View workflow job for this annotation

GitHub Actions / swiftlint

Type name 'preventScreenshotTests' should start with an uppercase character (type_name)
func testExample() throws {
// XCTest Documentation
// https://developer.apple.com/documentation/xctest

// Defining Test Cases and Test Methods
// https://developer.apple.com/documentation/xctest/defining_test_cases_and_test_methods
}
}
Loading

0 comments on commit 4376794

Please sign in to comment.