A tutorial-sized iOS app that wraps WKWebView in SwiftUI and adds the pieces a real browser shell needs: an address bar with a loading bar, back and forward navigation, favourites saved with SwiftData, sharing, and a two-way bridge between Swift and JavaScript.
WKWebViewinside SwiftUI.CustomWebKitViewwraps the web view withUIViewRepresentable. Key-value observation tokens trackestimatedProgress,canGoBack,canGoForwardand the current URL, and feed an@Observablemodel the UI binds to.- An address bar that guesses intent. Plain words become a search, bare domains get
https://, andlocalhostor127.0.0.1gethttp://. A thin progress bar is drawn inside the text field. - Swift to JavaScript and back. The bundled
Simple.htmlpage exposesaddMore(n), which Swift calls withevaluateJavaScript. The page's button posts to aWKScriptMessageHandlernamedIncrementFunction, so JavaScript can call back into Swift. - Favourites with SwiftData. A
@Modelclass stores favourite URLs.@Querylists them in a sheet, and a#Predicateremoves one when you unstar it. - iOS 18 styling.
MeshGradientborders,UnevenRoundedRectangleshapes andShareLinkfor the current page.
- Xcode 16.2 or later
- iOS 18.2 or later (simulator or device)
- Open
webkitViewer-tutorial.xcodeprojin Xcode. - Pick an iPhone simulator.
- Press Run.
The app opens on the bundled Simple.html page. Tap the + button in the toolbar to call into JavaScript, then tap the page's Add Back button to call back into Swift. After that, type a site such as apple.com in the address bar.
| File | Purpose |
|---|---|
ContentView.swift |
Web view, toolbar, favourites sheet and the JavaScript calls |
Observables.swift |
@Observable browser state, the SwiftData FavouritesList model and KVO tokens |
TextFieldView.swift |
Address bar with the built-in progress bar |
Simple.html |
Local page used to demonstrate the Swift and JavaScript bridge |
This is a learning project, not a production browser. Favourites live in an in-memory SwiftData store, so they reset when the app closes, and FavouritesView.swift is still a placeholder.
MIT © 2025 Jeevanjot Singh Vital