article

Sunday, November 7, 2021

SwiftUI Pull to Refresh

SwiftUI Pull to Refresh

In this tutorial I will demonstrate how to use the pull to refresh feature in SwiftUI.

 
//
//  ContentView.swift
//  swiftuidev15ios
//
//  Created by Cairocoders
//

import SwiftUI

struct ContentView: View {

    @State private var posts = [
        NewsItem(id: 0, title: "Want the latest posts?", body: "Pull to refresh!")
    ]
    
    var body: some View {
        NavigationView {
            List(posts) { item in
                VStack(alignment: .leading) {
                    Text(item.title)
                        .font(.headline)
                    Text(item.body)
                        .foregroundColor(.secondary)
                }
            }
            .refreshable {
                do {
                    // Fetch and decode JSON into posts items

                    let url = URL(string: "https://jsonplaceholder.typicode.com/posts")!
                    let (data, _) = try await URLSession.shared.data(from: url)
                    posts = try JSONDecoder().decode([NewsItem].self, from: data)
                } catch {
                    // Something went wrong; clear the news
                    posts = []
                }
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

struct NewsItem: Decodable, Identifiable {
    let id: Int
    let title: String
    let body: String
}

Related Post