SafariView is a package that you can use if you want to display content of the web (e.g. your website) inside your application.
In UIKit there is a component for that called SFSafariController
, but this component is not available for SwiftUI.
If you use this package you can use it the same way as you would interact with a native SwiftUI View.
⚠️ Important
Because this view is using SFSafariViewController be aware on the rules from Apple for that specific component. You can check the documentation here:
"In accordance with App Store Review Guidelines, this view (controller) must be used to visibly present information to users; the controller may not be hidden or obscured by other views or layers. Additionally, an app may not use this view to track users without their knowledge and consent."
- Import the SwiftPackage into XCode
- (File/Add Packages ...)
- Search for https://github.com/bestler/SafariView
- Press Add Package
- The package should show up in the Navigator on the left panel
- Add SafariView to a sheet() or fullscreen() modifier
- Rebuild your code
import SwiftUI
import SafariView
struct ContentView: View {
@State private var isPresented = false
var body: some View {
Button("Open in SafariView") {
isPresented.toggle()
}
.sheet(isPresented: $isPresented) {
SafariView(url: URL(string: "https://apple.com")!)
}
}
}
If you want to see a whole Demo Project on how to use it, take a look here.
The only mandatory parameter in the initalization of this view is url
.
Besides that there are optional parameters that you can specify in the initializer for customization. Available parameters:
dismissButtonStyle
: Specifices which type of Button to use to dismiss the screenpreferredBarTintColor
: The color to tint the background of the navigation bar and the toolbar (default: .systemBackground)preferredControlTintColor
: The color to tint the control buttons on the navigation bar and the toolbar (default: .tintColor/.accentColor)isBarCollapsing
: If you scroll the title bar (default: false)entersReaderIfAvailable
: A value that specifies whether Safari should enter Reader mode, if it is available (default: false)
A possible initialtation for example could be:
SafariView(
url: URL(string: "https://apple.com")!,
isBarCollapsing: true,
entersReaderIfAvailable: true)
If you find any bugs or you have ideas for improvements, you can create here an issue. Try to describe your problem as precise as possible. Feel also free to contribute and create a Pull Request.
This project is licensed under the MIT License.
See LICENSE for more information.