Skip to content

About

SwiftUI browser shell: WKWebView, a Swift and JavaScript bridge, and SwiftData favourites (iOS 18).

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Latest commit

 

History

6 Commits

Folders and files

Repository files navigation

WebKit Viewer: a small SwiftUI browser

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.

WebKit Viewer running on iPhone

What it shows

  • WKWebView inside SwiftUI. CustomWebKitView wraps the web view with UIViewRepresentable. Key-value observation tokens track estimatedProgress, canGoBack, canGoForward and the current URL, and feed an @Observable model the UI binds to.
  • An address bar that guesses intent. Plain words become a search, bare domains get https://, and localhost or 127.0.0.1 get http://. A thin progress bar is drawn inside the text field.
  • Swift to JavaScript and back. The bundled Simple.html page exposes addMore(n), which Swift calls with evaluateJavaScript. The page's button posts to a WKScriptMessageHandler named IncrementFunction, so JavaScript can call back into Swift.
  • Favourites with SwiftData. A @Model class stores favourite URLs. @Query lists them in a sheet, and a #Predicate removes one when you unstar it.
  • iOS 18 styling. MeshGradient borders, UnevenRoundedRectangle shapes and ShareLink for the current page.

Requirements

  • Xcode 16.2 or later
  • iOS 18.2 or later (simulator or device)

Run it

  1. Open webkitViewer-tutorial.xcodeproj in Xcode.
  2. Pick an iPhone simulator.
  3. 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.

Project layout

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

Limits

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.

License

MIT © 2025 Jeevanjot Singh Vital

About

SwiftUI browser shell: WKWebView, a Swift and JavaScript bridge, and SwiftData favourites (iOS 18).

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages