-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move configuration and styles to swiftui environment
- Loading branch information
1 parent
fcaae9c
commit fa4c55a
Showing
6 changed files
with
480 additions
and
555 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() } | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Oops, something went wrong.