BrowserKit is a Swift package that provides a simple and flexible way to integrate web views into your iOS, macOS, and visionOS applications. It supports both SFSafariViewController for seamless web browsing on iOS and WKWebView for custom web content handling across platforms.
- Cross-Platform Support: Works with iOS, macOS, visionOS, and Catalyst.
- Safari and WebKit Integration: Choose between
SFSafariViewControllerfor a native browsing experience orWKWebViewfor custom web content. - Customisable Configurations: Easily configure both Safari and WebKit views with flexible closures.
- HTML String Loading: Load HTML content directly into a WKWebView with optional base URLs.
To add BrowserKit to your project, use the Swift Package Manager.
-
Open your project in Xcode.
-
Go to
File > Add Packages. -
In the search bar, enter the URL of the
BrowserKitrepository:https://github.com/markbattistella/BrowserKit
-
Click
Add Package.
You can load a URL using SFSafariViewController on iOS, visionOS, and Catalyst platforms:
import BrowserKit
struct ContentView: View {
var body: some View {
WebView(url: URL(string: "https://markbattistella.com")!)
}
}For custom web content handling, you can use WKWebView on any platform:
import BrowserKit
struct ContentView: View {
var body: some View {
WebView(url: URL(string: "https://markbattistella.com")!, webKitConfiguration: { config in
config.preferences.javaScriptEnabled = true
}, webViewConfiguration: { webView in
webView.navigationDelegate = self // Set your custom navigation delegate
})
}
}You can also load raw HTML content into a WKWebView:
import BrowserKit
struct ContentView: View {
var body: some View {
WebView(htmlString: "<html><body><h1>Hello, World!</h1></body></html>")
}
}You can customise the Safari view controller's configuration with the safariConfiguration closure:
WebView(url: URL(string: "https://markbattistella.com")!) { sfConfiguration in
sfConfiguration.entersReaderIfAvailable = true
}You can also customise the WKWebViewConfiguration or the WKWebView itself:
WebView(
url: URL(string: "https://markbattistella.com")!,
webKitConfiguration: { wkConfig in
wkConfig.allowsInlineMediaPlayback = true
},
webViewConfiguration: { webView in
webView.customUserAgent = "MyCustomUserAgent"
}
)Contributions are welcome! If you have suggestions or improvements, please fork the repository and submit a pull request.
BrowserKit is released under the MIT license. See LICENSE for details.