A simple package for opening URLs within your app using SFSafariViewController
in SwiftUI. It allows you to open URLs, including those in Link
, as in-app browsers using SFSafariViewController
.
- Opens URLs from
Link
within the app usingSFSafariViewController
. - Allows customization of various settings and presentation styles of
SFSafariViewController
. - Default settings can be changed with
@Environment(\.customSafariStyle)
.
- iOS 15.0+
- Xcode 16.0+
You can install InAppSafariKit using Swift Package Manager (SPM).
- Open your project in Xcode.
- Select File > Add Package Dependency...
- Enter
https://github.com/Chronos2500/InAppSafariKit.git
. - Set the version rule and other settings, then click Add Package.
import SwiftUI
import InAppSafariKit
struct ContentView: View {
private let url = URL(string: "https://www.apple.com")!
var body: some View {
NavigationStack{
Form{
Link("Open Default Browser", destination: url)
Link("Open by InAppSafariKit (Default)", destination: url)
.OpenURLInAppSafari()
}
}
}
}
Simply add the .OpenURLInAppSafari()
modifier to a Link
to open the link within the app using SFSafariViewController
.
To apply it to all links within the app, add the .OpenURLInAppSafari()
modifier to the parent View.
By default, SFSafariViewController
is configured as follows:
entersReaderIfAvailable
:false
barCollapsingEnabled
:true
dismissButtonStyle
:.done
preferredBarTintColor
:nil
preferredControlTintColor
:nil
modalPresentationStyle
:.fullScreen
For a detailed example, see the project in the Examples folder.
You can customize the settings and presentation animation of SFSafariViewController
using the .OpenURLInAppSafari()
modifier.
Link("Custom Bar Colors", destination: url)
.OpenURLInAppSafari(
preferredBarTintColor: .purple,
preferredControlTintColor: .white
)
![]() |
![]() |
Open by InAppSafariKit (Default) | preferredBarTintColor = .purple |
![]() |
![]() |
modalPresentationStyle = .pageSheet |
modalPresentationStyle = .overFullScreen |
You can change the default style for child views and beyond by using the customSafariStyle
environment variable on the parent view.
@main
struct InAppSafariKitExampleApp: App {
var body: some Scene {
WindowGroup {
ContentView()
.environment(\.customSafariStyle,CustomSafariStyle(dismissButtonStyle: .cancel,preferredBarTintColor: .gray))
}
}
}
This project is released under the MIT License.
Chronos2500 © 2025