Skip to content

Commit

Permalink
Add iOS 14 Support (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
vsanthanam authored Jul 17, 2023
1 parent 0f0da22 commit ac7cd47
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 16 deletions.
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import PackageDescription
let package = Package(
name: "SafariView",
platforms: [
.iOS(.v15),
.macCatalyst(.v15)
.iOS(.v14),
.macCatalyst(.v14)
],
products: [
.library(
Expand Down
2 changes: 1 addition & 1 deletion Sources/SafariView/Environment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

import SwiftUI

@available(iOS 15.0, macCatalyst 15.0, *)
@available(iOS 14.0, macCatalyst 14.0, *)
public extension EnvironmentValues {

/// The additional activies to include the share sheet displayed inside a ``SafariView``
Expand Down
2 changes: 1 addition & 1 deletion Sources/SafariView/ExcludedActivityTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import Foundation
import UIKit

@available(iOS 15.0, macCatalyst 15.0, *)
@available(iOS 14.0, macCatalyst 14.0, *)
public extension SafariView {

/// A struct used to exclude activity types from the share sheet of a ``SafariView``.
Expand Down
2 changes: 1 addition & 1 deletion Sources/SafariView/IncludedActivities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import Foundation
import UIKit

@available(iOS 15.0, macCatalyst 15.0, *)
@available(iOS 14.0, macCatalyst 14.0, *)
public extension SafariView {

/// A struct used to include custom activities in the share sheet of a ``SafariView``
Expand Down
2 changes: 1 addition & 1 deletion Sources/SafariView/Modifiers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

import SwiftUI

@available(iOS 15.0, macCatalyst 15.0, *)
@available(iOS 14.0, macCatalyst 14.0, *)
public extension View {

/// Set the automatic reader behavior of safari views within this view
Expand Down
2 changes: 1 addition & 1 deletion Sources/SafariView/Presentation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

import SwiftUI

@available(iOS 15.0, macCatalyst 15.0, *)
@available(iOS 14.0, macCatalyst 14.0, *)
public extension View {

/// Presents a ``SafariView`` when a binding to a Boolean value that you provide is `true`.
Expand Down
6 changes: 1 addition & 5 deletions Sources/SafariView/SafariView.docc/View.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,10 @@ A SafariView is best presented by using one the `safari` view modifiers, or via

### Creating a Safari View

- ``init(url:onInitialLoad:onInitialRedirect:onOpenInBrowser:)``
- ``init(url:activityButton:onInitialLoad:onInitialRedirect:onOpenInBrowser:)``
- ``init(url:activityButton:eventAttribution:onInitialLoad:onInitialRedirect:onOpenInBrowser:)``

### Configuring the View

- ``ActivityButton``
- ``DismissButtonStyle``

### Custom Activities

- ``IncludedActivities``
Expand Down
36 changes: 32 additions & 4 deletions Sources/SafariView/SafariView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,42 @@ import SwiftUI
import UIKit

/// A wrapper for `SFSafariViewController` in SwiftUI
@available(iOS 15.0, macCatalyst 15.0, *)
@available(iOS 14.0, macCatalyst 14.0, *)
public struct SafariView: View {

// MARK: - Initializers

/// Create a SafariView
/// - Parameters:
/// - url: URL to load
/// - onInitialLoad: Closure to execute on initial load
/// - onInitialRedirect: Closure to execute on intial redirect
/// - onOpenInBrowser: Closure to execute if a user moves from a `SafariView` to `Safari.app`
public init(
url: URL,
onInitialLoad: ((_ didLoadSuccessfully: Bool) -> Void)? = nil,
onInitialRedirect: ((_ url: URL) -> Void)? = nil,
onOpenInBrowser: (() -> Void)? = nil
) {
self.url = url
activityButton = nil
eventAttribution = nil
self.onInitialLoad = onInitialLoad
self.onInitialRedirect = onInitialRedirect
self.onOpenInBrowser = onOpenInBrowser
}

/// Create a SafariView with a custom activity button
/// - Parameters:
/// - url: URL to load
/// - activityButton: Custom activity button to include in the safari view
/// - onInitialLoad: Closure to execute on initial load
/// - onInitialRedirect: Closure to execute on intial redirect
/// - onOpenInBrowser: Closure to execute if a user moves from a `SafariView` to `Safari.app`
@available(iOS 15.0, macCatalyst 15.0, *)
public init(
url: URL,
activityButton: ActivityButton? = nil,
activityButton: ActivityButton?,
onInitialLoad: ((_ didLoadSuccessfully: Bool) -> Void)? = nil,
onInitialRedirect: ((_ url: URL) -> Void)? = nil,
onOpenInBrowser: (() -> Void)? = nil
Expand Down Expand Up @@ -88,11 +109,13 @@ public struct SafariView: View {
public typealias DismissButtonStyle = SFSafariViewController.DismissButtonStyle

/// A convenience typealias for [`SFSafariViewController.ActivityButton`](https://developer.apple.com/documentation/safariservices/sfsafariviewcontroller/activitybutton)
@available(iOS 15.0, macCatalyst 15.0, *)
public typealias ActivityButton = SFSafariViewController.ActivityButton

/// A convenience typealias for [`SFSafariViewController.PrewarmingToken`](https://developer.apple.com/documentation/safariservices/sfsafariviewcontroller/prewarmingtoken)
///
/// You can generate prewarming tokens for invalidation using the ``prewarmConnections(to:)`` static method.
@available(iOS 15.0, macCatalyst 15.0, *)
public typealias PrewarmingToken = SFSafariViewController.PrewarmingToken

/// Prewarm the connection to a list of provided URLs
Expand All @@ -107,6 +130,7 @@ public struct SafariView: View {
///
/// - Parameter URLs: The URLs to prewarm
/// - Returns: A prewarming token for the provided URLs.
@available(iOS 15.0, macCatalyst 15.0, *)
@discardableResult
public static func prewarmConnections(to URLs: [URL]) -> PrewarmingToken {
SFSafariViewController.prewarmConnections(to: URLs)
Expand All @@ -127,6 +151,7 @@ public struct SafariView: View {

// MARK: - View

@_documentation(visibility: internal)
public var body: some View {
Safari(parent: self)
.ignoresSafeArea(.container, edges: .all)
Expand Down Expand Up @@ -155,7 +180,7 @@ public struct SafariView: View {
@Environment(\.safariViewExcludedActivityTypes)
private var excludedActivityTypes: ExcludedActivityTypes

private let activityButton: ActivityButton?
private let activityButton: AnyObject?
private let eventAttribution: AnyObject?
private let url: URL
private let onInitialLoad: ((Bool) -> Void)?
Expand All @@ -172,7 +197,10 @@ public struct SafariView: View {
let configuration = SFSafariViewController.Configuration()
configuration.entersReaderIfAvailable = entersReaderIfAvailable
configuration.barCollapsingEnabled = barCollapsingEnabled
configuration.activityButton = activityButton
if #available(iOS 15.0, macCatalyst 15.0, *),
let activityButton {
configuration.activityButton = unsafeDowncast(activityButton, to: ActivityButton.self)
}
if #available(iOS 15.2, *),
let eventAttribution {
configuration.eventAttribution = unsafeDowncast(eventAttribution, to: UIEventAttribution.self)
Expand Down

0 comments on commit ac7cd47

Please sign in to comment.