5

SwiftUI Pull to Refresh Tutorial

 3 years ago
source link: https://www.ioscreator.com/tutorials/swiftui-pull-to-refresh-tutorial
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
SwiftUI Pull to Refresh Tutorial

The Pull to Refresh functionality can be used to refresh contents of a List by pulling down. SwiftUI introduced the refreshablel() modifier. In this tutorial a JSON file will be fetched from the internet and displayed in a list when the pull to refresh is initiated..This tutorial is built for iOS15 and Xcode 13, which can be download at the Apple developer portal.

Open Xcode and either click Create a new Xcode project in Xcode’s startup window, or choose File > New > Project. In the template selector, select iOS as the platform, select App template in the Application section and then click Next.

Enter SwiftUIPullToRefreshTutorial as the Product Name, select SwiftUI as Interface, SwiftUI App as Life Cycle and Swift as Language. Deselect the Include Tests checkbox and click Next. Choose a location to save the project on your Mac.

In the canvas, click Resume to display the preview. If the canvas isn’t visible, select Editor > Editor and Canvas to show it.

In the Project navigator, click to select ContentView.swift. Above the ContentView struct add a new Contact struct to hold the items of the json file which will be loaded.

struct Contact: Decodable, Identifiable { let id: Int let name: String let email: String }

Change the ContentView struct to

struct ContentView: View { // 1. @State private var contacts = [ Contact(id: 0, name: "SwiftUI", email: "Pull to refresh!")] var body: some View { NavigationView { // 2. List(contacts) { item in VStack(alignment: .leading) { Text(item.name) .font(.title) Text(item.email) .font(.subheadline) } } // 3. .refreshable { do { // 4. let url = URL(string: "https://ioscreator.com/s/contacts.json")! let (jsonData, _) = try await URLSession.shared.data(from: url) contacts = try JSONDecoder().decode([Contact].self, from: jsonData) } catch { // error contacts = [] } } } } }
  1. A state propery array of contacts is declared which will hold the contacts. The first item is a stub item which will display the pull to refresh text in the List.

  2. The List is displayed and contains the name and emall of the contact

  3. The refreshable modifier is added to the List and will display an activity indicator while running the code.

  4. The json file is fetched asynchronously and decoded into the Contacts struct.

Go to the Preview and select Live Preview.

Pull down the Pull to refresh item to load and display the contacts.

swiftui-list-json-fetched-preview.png

The source code of the SwiftUIPullToRefreshTutorial can be downloaded at the ioscreator repository on Github.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK