Swiftpack.co - Package - stleamist/FullScreenSafariView

Swift: 5.1 version SwiftPM: compatible license contact: @stleamist

FullScreenSafariView

FullScreenSafariView is a clean way to present a full-screen SFSafariViewController with a push transition in SwiftUI.

Motivation

SwiftUI is a strong, intuitive way to build user interfaces, but was released with some part of existing elements missing. One example of those missing elements is the SFSafariViewController.

Fortunately, Apple provides a way to wrap UIKit elements into SwiftUI views. A common approach to place the SFSafariViewController inside SwiftUI is to create a simple view representing an SFSafariViewController, then present it with a sheet(isPresented:onDismiss:content:) modifier or a NavigationLink button (See ContentView.swift in the demo project).

However, there’s a problem in this approach: it can’t present the SFSafariViewController with its default presentation style — a push transition covers full screen. A sheet modifier can present the view only in a modal sheet, and a navigation link shows the two navigation bars at the top so we have to deal with them. This comes down to the conclusion that there’s no option to present it the right way except for using present(_:animated:completion:) method of an UIViewController instance, but it is prohibited and not a good design to access the UIHostingController directly from the SwiftUI view.

FullScreenSafariView clearly achieves this goal by hosting a simple UIViewController to present an SFSafariViewController as a view’s background.

Usage

You can use it easily with a safariView(isPresented:content:) modifier in a similar way to presenting a sheet.

isPresented

A Binding to whether the SFSafariViewController is presented.

content

A closure returning the URL to load.

import SwiftUI
import FullScreenSafariView

struct ContentView: View {
    
    @State private var showingSafariView = false
    
    var body: some View {
        Button(action: {
            self.showingSafariView = true
        }) {
            Text("Show SafariView")
        }
        .safariView(isPresented: $showingSafariView) {
            URL(string: "https://example.com/")!
        }
    }
}

Installation

Swift Package Manager

Add this repository as a dependency in your Package.swift:

// swift-tools-version:5.1

import PackageDescription

let package = Package(
    ...,
    dependencies: [
        .package(url: "https://github.com/stleamist/FullScreenSafariView.git", .upToNextMajor(from: "1.0.0"))
    ],
    ...
)

Xcode

Select File > Swift Packages > Add Package Dependency, then enter the following URL:

https://github.com/stleamist/FullScreenSafariView.git

For more details, see Adding Package Dependencies to Your App.

Demo

You can compare the behavior of FullScreenSafariView with the other two ways above in the demo project. Check out the demo app by opening FullScreenSafariView.xcworkspace.

License

FullScreenSafariView is released under the MIT license. See LICENSE for details.

Github

link
Stars: 3

Dependencies

Used By

Total: 0

Releases

1.0.0 - 2020-05-18 08:56:39

  • Initial release