HomeiOS DevelopmentExploring WebView and WebPage in SwiftUI for iOS 26

Exploring WebView and WebPage in SwiftUI for iOS 26


In iOS 26, SwiftUI lastly launched certainly one of its most extremely anticipated parts: WebView, a local answer for displaying internet content material. Earlier than this replace, SwiftUI builders needed to depend on the UIKit framework, utilizing UIViewRepresentable to wrap WKWebView or SFSafariViewController with a purpose to embed an online view. With the arrival of WebView, Apple now offers a completely native SwiftUI strategy to integrating internet shopping capabilities into apps. On this tutorial, I’ll provide you with a fast overview of the brand new WebView and present you how you can use it in your personal app growth.

The Fundamental Utilization of WebView

To load an online web page utilizing the brand new WebView, you merely import the WebKit framework and instantiate the view with a URL. Right here is an instance:

import SwiftUI
import WebKit

struct ContentView: View {
    var physique: some View {
        WebView(url: URL(string: "https://www.appcoda.com"))
    }
}

With only a single line of code, now you can embed a full-featured cellular Safari expertise instantly in your app—powered by the identical WebKit engine that runs Safari.

swiftui-webview-basics.png

An Various Approach of Loading Internet Content material

Along with WebView, the WebKit framework additionally introduces a brand new class referred to as WebPage. Fairly than passing a URL on to WebView, you possibly can first create a WebPage occasion with the URL after which use it to show the online content material. Beneath is the pattern code that achieves the identical outcome:

struct ContentView: View {
    @State non-public var web page = WebPage()
    
    var physique: some View {
        
        WebView(web page)
            .ignoresSafeArea()
            .onAppear {
                if let pageURL = URL(string: "https://www.appcoda.com") {
                    let urlRequest = URLRequest(url: pageURL)
                    web page.load(urlRequest)
                }
            }
    }
}

Working with WebPage

Usually, when you merely must show internet content material or embed a browser in your app, WebView is essentially the most easy strategy. In case you want finer management over how internet content material behaves and interacts together with your software, WebPage presents extra detailed customization choices like accessing internet web page properties and programmatic navigation.

For instance, you possibly can entry the title property of the WebPage object to retrieve the title of the online web page:

Textual content(web page.title)
Textual content(web page.url)

You may as well use the url property to entry the present URL of the online web page.

swiftui-webview-webpage-properties.png

If you wish to observe the loading progress, the estimatedProgress property provides you an approximate share of the web page’s loading completion.

Textual content(web page.estimatedProgress.formatted(.p.c.precision(.fractionLength(0))))

Aside from accessing its properties, the WebPage class additionally allows you to management the loading conduct of an online web page. For instance, you possibly can name reload() to refresh the present web page, or stopLoading() to halt the loading course of.

Loading Customized HTML Content material Utilizing WebPage

Moreover loading a URLRequest, the WebPage class’s load methodology may deal with customized HTML content material instantly. Beneath is the pattern code for loading a YouTuber participant:

struct ContentView: View {
    
    @State non-public var web page = WebPage()
    
    non-public let htmlContent: String = """
        

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

In case you place the code in Xcode, the preview ought to present you the YouTube participant.

swiftui-webview-youtube.png

Executing Javascript

The WebPage object not solely allows you to load HTML content material—it additionally means that you can execute JavaScript. You should use the callJavaScript methodology and move within the script you wish to run. Right here is an instance:

struct ContentView: View {
    
    @State non-public var web page = WebPage()
    
    non-public let snippet = """
        doc.write("");
        """
    
    var physique: some View {
        
        WebView(web page)
            .activity {
                do {
                    strive await web page.callJavaScript(snippet)
                } catch {
                    print("JavaScript execution failed: (error)")
                }
            }
    }
}

Abstract

The brand new native WebView element in SwiftUI makes it a lot simpler to show internet content material inside iOS apps, eradicating the necessity to depend on UIKit wrappers. SwiftUI builders can select between two key approaches:

  • WebView: Ultimate for easy use circumstances the place you simply must load and show an online web page.
  • WebPage: Provides extra granular management, permitting you to entry web page properties, observe loading progress, reload or cease loading, and even execute JavaScript.

This native SwiftUI answer brings a cleaner, extra streamlined expertise to embedding internet content material in your apps.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments