Skip to content

Commit

Permalink
Move configuration and styles to swiftui environment
Browse files Browse the repository at this point in the history
  • Loading branch information
vsanthanam committed Jul 3, 2023
1 parent fcaae9c commit fa4c55a
Show file tree
Hide file tree
Showing 6 changed files with 480 additions and 555 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import PackageDescription
let package = Package(
name: "SafariView",
platforms: [
.iOS(.v15)
.iOS(.v16)
],
products: [
.library(
Expand Down
58 changes: 58 additions & 0 deletions Sources/SafariView/Environment.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// SafariView
// Environment.swift
//
// MIT License
//
// Copyright (c) 2021 Varun Santhanam
// 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.

import SwiftUI

public extension EnvironmentValues {

var safariViewConfiguration: SafariView.Configuration {
get { self[SafariViewConfigurationEnvironmentKey.self] }
set { self[SafariViewConfigurationEnvironmentKey.self] = newValue }
}

var safariViewStyle: SafariView.Style {
get { self[SafariViewStyleEvironmentKey.self] }
set { self[SafariViewStyleEvironmentKey.self] = newValue }
}

}

struct SafariViewConfigurationEnvironmentKey: EnvironmentKey {

// MARK: - EnvironmentKey

typealias Value = SafariView.Configuration

static var defaultValue: Value { .init() }

}

private struct SafariViewStyleEvironmentKey: EnvironmentKey {

typealias Value = SafariView.Style

static var defaultValue: Value { .init() }

}
84 changes: 84 additions & 0 deletions Sources/SafariView/Modifiers.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// SafariView
// Modifiers.swift
//
// MIT License
//
// Copyright (c) 2021 Varun Santhanam
// 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.

import SwiftUI

public extension View {

func safariConfiguration(_ configuration: SafariView.Configuration) -> some View {
let modifier = SafariViewConfigurationModifier(configuration: configuration)
return ModifiedContent(content: self, modifier: modifier)
}

func safariStyle(_ style: SafariView.Style) -> some View {
let modifier = SafariViewStyleModifier(style: style)
return ModifiedContent(content: self, modifier: modifier)
}

}

private struct SafariViewConfigurationModifier: ViewModifier {

// MARK: - Initializers

init(configuration: SafariView.Configuration) {
self.configuration = configuration
}

// MARK: - ViewModifier

@ViewBuilder
func body(content: Content) -> some View {
content
.environment(\.safariViewConfiguration, configuration)
}

// MARK: - Private

private let configuration: SafariView.Configuration

}

private struct SafariViewStyleModifier: ViewModifier {

// MARK: - Initializers

init(style: SafariView.Style) {
self.style = style
}

// MARK: - ViewModifier

@ViewBuilder
func body(content: Content) -> some View {
content
.environment(\.safariViewStyle, style)
}

// MARK: - Private

private let style: SafariView.Style

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SafariView
// ViewExtensions.swift
// Presentation.swift
//
// MIT License
//
Expand Down Expand Up @@ -134,9 +134,11 @@ public extension View {
onDismiss: (() -> Void)? = nil,
@ViewBuilder safariView: @escaping (Item) -> SafariView
) -> some View where Item: Identifiable {
let modifier = SafariView.ItemModitifer(item: item,
build: safariView,
onDismiss: onDismiss ?? {})
let modifier = SafariView.IdentifiableItemModitifer(
item: item,
build: safariView,
onDismiss: onDismiss ?? {}
)
return ModifiedContent(content: self, modifier: modifier)
}

Expand Down Expand Up @@ -199,7 +201,12 @@ public extension View {
onDismiss: (() -> Void)? = nil,
@ViewBuilder safariView: @escaping (Item) -> SafariView
) -> some View {
let modifier = SafariView.GenericItemModifier(item: item, id: id, onDismiss: onDismiss, safariView: safariView)
let modifier = SafariView.ItemModifier(
item: item,
id: id,
onDismiss: onDismiss,
safariView: safariView
)
return ModifiedContent(content: self, modifier: modifier)
}

Expand Down
38 changes: 38 additions & 0 deletions Sources/SafariView/SafariView.docc/View.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# ``SafariView/SafariView``


A `SafariView` is a wrapper around [`SFSafariViewController`](https://developer.apple.com/documentation/safariservices/sfsafariviewcontroller) for use within SwiftUI applications.

The view includes Safari features such as Reader, AutoFill, Fraudulent Website Detection, and content blocking. The user's activity and interaction with `SafariView` are not visible to your app, which cannot access AutoFill data, browsing history, or website data. You do not need to secure data between your app and Safari. If you would like to share data between your app and Safari, so it is easier for a user to log in only one time, use [`ASWebAuthenticationSession`](https://developer.apple.com/documentation/authenticationservices/aswebauthenticationsession) instead.

- Important: In accordance with [App Store Review Guidelines](https://developer.apple.com/app-store/review/guidelines/), this view must be used to visibly present information to users; the view may not be hidden or obscured by other views or layers. Additionally, an app may not use `SafariView` to track users without their knowledge and consent.

You can present a `SafariView` using one of our provided view modifiers, like so:

```swift
import Foundation
import SafariView
import SwiftUI

struct ShowLicenseAgreement: View {

let licenseAgreementURL: URL

@State private var isShowingSafari = false

var body: some View {
Button(action: {
isShowingSafari.toggle()
}) {
Text("Show License Agreement")
}
.safari(isPresented: $isShowingSafari,
onDismiss: didDismiss) {
SafariView(url: licenseAgreementURL)
}
}

}
```

You can also use sheet presentation, or any other presentation mechanism of your choice.
Loading

0 comments on commit fa4c55a

Please sign in to comment.