Close Menu
geekfence.comgeekfence.com
    What's Hot

    Customer experience management (CXM) predictions for 2026: How customers, enterprises, technology, and the provider landscape will evolve 

    December 28, 2025

    What to Know About the Cloud and Data Centers in 2026

    December 28, 2025

    Why Enterprise AI Scale Stalls

    December 28, 2025
    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

    SwiftText | Cocoanetics

    December 28, 2025

    Uniquely identifying views – The.Swift.Dev.

    December 27, 2025

    Unable to upload my app with Transporter. Some kind of version mismatch? [duplicate]

    December 26, 2025

    Experimenting with Live Activities – Ole Begemann

    December 25, 2025

    Announcing Mastering SwiftUI for iOS 18 and Xcode 16

    December 24, 2025

    Grouping Liquid Glass components using glassEffectUnion on iOS 26 – Donny Wals

    December 22, 2025
    Top Posts

    Understanding U-Net Architecture in Deep Learning

    November 25, 20258 Views

    Microsoft 365 Copilot now enables you to build apps and workflows

    October 29, 20258 Views

    Here’s the latest company planning for gene-edited babies

    November 2, 20257 Views
    Don't Miss

    Customer experience management (CXM) predictions for 2026: How customers, enterprises, technology, and the provider landscape will evolve 

    December 28, 2025

    After laying out our bold CXM predictions for 2025 and then assessing how those bets played out…

    What to Know About the Cloud and Data Centers in 2026

    December 28, 2025

    Why Enterprise AI Scale Stalls

    December 28, 2025

    New serverless customization in Amazon SageMaker AI accelerates model fine-tuning

    December 28, 2025
    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

    Customer experience management (CXM) predictions for 2026: How customers, enterprises, technology, and the provider landscape will evolve 

    December 28, 2025

    What to Know About the Cloud and Data Centers in 2026

    December 28, 2025

    Subscribe to Updates

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

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