SafariView

main

Swift Package to use the functionalities of SFSafariViewController inside SwiftUI
bestler/SafariView

SafariView

license

About

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."

Installation and Usage

Installation

  1. Import the SwiftPackage into XCode
  2. The package should show up in the Navigator on the left panel
  3. Add SafariView to a sheet() or fullscreen() modifier
  4. Rebuild your code

Example 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.

Screenshots

Customization

The only mandatory parameter in the initalization of this view is url. ⚠️ Make sure that you provide a valid 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 screen
  • preferredBarTintColor: 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)

Issues

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.

License

This project is licensed under the MIT License.

See LICENSE for more information.

Description

  • Swift Tools 5.7.0
View More Packages from this Author

Dependencies

  • None
Last updated: Sun Oct 20 2024 23:58:27 GMT-0900 (Hawaii-Aleutian Daylight Time)