Close Menu
geekfence.comgeekfence.com
    What's Hot

    Open Cosmos launches first satellites for new LEO constellation

    January 25, 2026

    Achieving superior intent extraction through decomposition

    January 25, 2026

    How UX Research Reveals Hidden AI Orchestration Failures

    January 25, 2026
    Facebook X (Twitter) Instagram
    • About Us
    • Contact Us
    Facebook Instagram
    geekfence.comgeekfence.com
    • Home
    • UK Tech News
    • AI
    • Big Data
    • Cyber Security
      • Cloud Computing
      • iOS Development
    • IoT
    • Mobile
    • Software
      • Software Development
      • Software Engineering
    • Technology
      • Green Technology
      • Nanotechnology
    • Telecom
    geekfence.comgeekfence.com
    Home»iOS Development»Exploring WebView and WebPage in SwiftUI for iOS 26
    iOS Development

    Exploring WebView and WebPage in SwiftUI for iOS 26

    AdminBy AdminDecember 3, 2025No Comments4 Mins Read1 Views
    Facebook Twitter Pinterest LinkedIn Telegram Tumblr Email
    Exploring WebView and WebPage in SwiftUI for iOS 26
    Share
    Facebook Twitter LinkedIn Pinterest Email


    In iOS 26, SwiftUI finally introduced one of its most highly anticipated components: WebView, a native solution for displaying web content. Before this update, SwiftUI developers had to rely on the UIKit framework, using UIViewRepresentable to wrap WKWebView or SFSafariViewController in order to embed a web view. With the arrival of WebView, Apple now provides a fully native SwiftUI approach to integrating web browsing capabilities into apps. In this tutorial, I’ll give you a quick overview of the new WebView and show you how to use it in your own app development.

    The Basic Usage of WebView

    To load a web page using the new WebView, you simply import the WebKit framework and instantiate the view with a URL. Here is an example:

    import SwiftUI
    import WebKit
    
    struct ContentView: View {
        var body: some View {
            WebView(url: URL(string: "
        }
    }
    

    With just a single line of code, you can now embed a full-featured mobile Safari experience directly in your app—powered by the same WebKit engine that runs Safari.

    swiftui-webview-basics.png

    An Alternative Way of Loading Web Content

    In addition to WebView, the WebKit framework also introduces a new class called WebPage. Rather than passing a URL directly to WebView, you can first create a WebPage instance with the URL and then use it to display the web content. Below is the sample code that achieves the same result:

    struct ContentView: View {
        @State private var page = WebPage()
        
        var body: some View {
            
            WebView(page)
                .ignoresSafeArea()
                .onAppear {
                    if let pageURL = URL(string: " {
                        let urlRequest = URLRequest(url: pageURL)
                        page.load(urlRequest)
                    }
                }
        }
    }
    

    Working with WebPage

    In most cases, if you simply need to display web content or embed a browser in your app, WebView is the most straightforward approach. If you need finer control over how web content behaves and interacts with your application, WebPage offers more detailed customization options like accessing web page properties and programmatic navigation.

    For example, you can access the title property of the WebPage object to retrieve the title of the web page:

    Text(page.title)
    Text(page.url)
    

    You can also use the url property to access the current URL of the web page.

    swiftui-webview-webpage-properties.png

    If you want to track the loading progress, the estimatedProgress property gives you an approximate percentage of the page’s loading completion.

    Text(page.estimatedProgress.formatted(.percent.precision(.fractionLength(0))))
    

    Other than accessing its properties, the WebPage class also lets you control the loading behavior of a web page. For example, you can call reload() to refresh the current page, or stopLoading() to halt the loading process.

    Loading Custom HTML Content Using WebPage

    Besides loading a URLRequest, the WebPage class’s load method can also handle custom HTML content directly. Below is the sample code for loading a YouTuber player:

    struct ContentView: View {
        
        @State private var page = WebPage()
        
        private let htmlContent: String = """
            

    """ var body: some View { WebView(page) .onAppear { page.load(html: htmlContent, baseURL: URL(string: "about:blank")!) } } }

    If you place the code in Xcode, the preview should show you the YouTube player.

    swiftui-webview-youtube.png

    Executing Javascript

    The WebPage object not only lets you load HTML content—it also allows you to execute JavaScript. You can use the callJavaScript method and pass in the script you want to run. Here is an example:

    struct ContentView: View {
        
        @State private var page = WebPage()
        
        private let snippet = """
            document.write("");
            """
        
        var body: some View {
            
            WebView(page)
                .task {
                    do {
                        try await page.callJavaScript(snippet)
                    } catch {
                        print("JavaScript execution failed: \(error)")
                    }
                }
        }
    }
    

    Summary

    The new native WebView component in SwiftUI makes it much easier to display web content within iOS apps, removing the need to rely on UIKit wrappers. SwiftUI developers can choose between two key approaches:

    • WebView: Ideal for straightforward use cases where you just need to load and display a web page.
    • WebPage: Offers more granular control, allowing you to access page properties, track loading progress, reload or stop loading, and even execute JavaScript.

    This native SwiftUI solution brings a cleaner, more streamlined experience to embedding web content in your apps.



    Source link

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email

    Related Posts

    A Deep Dive into SwiftData migrations – Donny Wals

    January 24, 2026

    AI, find me some work…

    January 23, 2026

    Swift adapter design pattern – The.Swift.Dev.

    January 22, 2026

    Text is not visible when the button is in disabled state

    January 21, 2026

    What’s New in SwiftUI for iOS 18

    January 19, 2026

    WWDC 2023: A Reflection on Apple’s “Spatial Computing” Journey

    January 17, 2026
    Top Posts

    Understanding U-Net Architecture in Deep Learning

    November 25, 202511 Views

    Hard-braking events as indicators of road segment crash risk

    January 14, 20269 Views

    Microsoft 365 Copilot now enables you to build apps and workflows

    October 29, 20258 Views
    Don't Miss

    Open Cosmos launches first satellites for new LEO constellation

    January 25, 2026

    Press Release Open Cosmos, the company building satellites to understand and connect the world, has…

    Achieving superior intent extraction through decomposition

    January 25, 2026

    How UX Research Reveals Hidden AI Orchestration Failures

    January 25, 2026

    ByteDance steps up its push into enterprise cloud services

    January 25, 2026
    Stay In Touch
    • Facebook
    • Instagram
    About Us

    At GeekFence, we are a team of tech-enthusiasts, industry watchers and content creators who believe that technology isn’t just about gadgets—it’s about how innovation transforms our lives, work and society. We’ve come together to build a place where readers, thinkers and industry insiders can converge to explore what’s next in tech.

    Our Picks

    Open Cosmos launches first satellites for new LEO constellation

    January 25, 2026

    Achieving superior intent extraction through decomposition

    January 25, 2026

    Subscribe to Updates

    Please enable JavaScript in your browser to complete this form.
    Loading
    • About Us
    • Contact Us
    • Disclaimer
    • Privacy Policy
    • Terms and Conditions
    © 2026 Geekfence.All Rigt Reserved.

    Type above and press Enter to search. Press Esc to cancel.