Weps Tech
The world of development
Hey, Guys welcome to this tutorial. In this complete video, I will make you understand the quick trick for Fetch data from server and Show in List in Swift UI. And this is a simple way to achieve our goals. Through this simple video tutorial, you can learn more interesting things about the Fetch data from server and Show in List in Swift UI. In this video tutorial, you can learn the basics for fetching the data in SwiftUI. And also you can get the answer of How to started with Fetch data from server and Show in List in SwiftUI. Letβs dive into he programming and become a best iOS developer. So watch the complete video. And if you watched the complete video then do not forget to give your suggestion in the comment box. Because your feedback & suggestions do matter for me ππππππ»
And finally, I would like to say that, If you like this video and itβs helpful for your project then click on the like button and do not forget to subscribe to this YouTube channel for future updates.
Here you will get the answer of your question that, How to Fetch data from server. In this tutorial I have tell you in very simple way. As you have seen the above video. It’s grate thing. Now below you can find out all the essential code snapshot of the entire video.
// // ServerManager.swift // io.SwiftUI-Fetch-Data-From-Server-V2 // // Created by Pushpendra on 26/12/20. // import Foundation class ServerManage : ObservableObject { @Published var isLoading : Bool = false @Published var dataList = [VehicleTypes.VType]() init() { isLoading = false guard let url = URL(string: "http://my_domain_dot_com/mechanic_type/vehicle_types") else { return } let params = "access_key=\(Constants.Key)" var request = URLRequest(url: url) request.httpMethod = "POST" request.httpBody = params.data(using: .utf8) let task = URLSession.shared.dataTask(with: request) { (data, response, error) in DispatchQueue.main.async { self.isLoading = true } if error == nil { if data != nil { do { let myServiceResponse = try JSONDecoder().decode(VehicleTypes.self, from: data!) DispatchQueue.main.async { self.dataList = myServiceResponse.data } } catch let exception { print(exception) } }else { print("Something went wrong!") } } else { print(error?.localizedDescription ?? "N/A") } } task.resume() } }
Grate job… Above snapshot will help you to call the data from the server and store into model class. Well Below, I am writing the Model class code of this server manager which is named as `VehicleTypes`.
// // VehicleTypes.swift // io.SwiftUI-Fetch-Data-From-Server-V2 // // Created by Pushpendra on 26/12/20. // import Foundation class VehicleTypes: Codable, Identifiable { var status : Bool = false var message : String? = nil var data : [VType] = [VType]() class VType : Codable, Identifiable { var id : String? = nil var type : String? = nil var type_hint : String? = nil var is_selected : Bool = false } }
Wonderful, After that you do not need to do the more work, As I have already mention into the above video. You need to design the video and after that you need to observe the output which you will get from the server call.
In Addition, I would like to tell you that, we will create VehicleTypeRow for handling the item data of the json output. As you can see as mention below.
// // VehicleTypeRow.swift // io.SwiftUI-Fetch-Data-From-Server-V2 // // Created by Pushpendra on 26/12/20. // import Foundation import SwiftUI struct VehicleTypeRow : View { var model : VehicleTypes.VType? = nil var body: some View { VStack(alignment: .leading) { Text(model?.type ?? "N/A") .font(.title) .fontWeight(.bold) Text(model?.type_hint ?? "N/A") .font(.body) } } } struct VehicleTypeRow_Previews : PreviewProvider { static var previews: some View { VehicleTypeRow() } }
Grate, Now you have complete all most. Now at the end check the last snapshot of the code for content view.
// // ContentView.swift // io.SwiftUI-Fetch-Data-From-Server-V2 // // Created by Pushpendra on 26/12/20. // import SwiftUI struct ContentView: View { @ObservedObject var serverManger = ServerManage() var body: some View { NavigationView { VStack { if serverManger.isLoading { if serverManger.dataList.isEmpty { Text("No data found here") } else { List(serverManger.dataList) { model in VehicleTypeRow(model: model) } } } else { Text("Loading...") } } .navigationBarTitle("Vehicles") } } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } }
Itβs completed πππππ
In Conclusion, You have done with this tutorial. And I hope now the concept of Fetch data from server in SwiftUI is clear. Firstly, If you have any doubts regarding the Fetch data from server in SwiftUI. Then you can comment into the comment box. And If you like this tutorial with a complete description of the simple and attractive tutorial in iOS then please like my video on YouTube and do not forget to subscribe to my YouTube channel. Because you will get lots of videos related to iOS development with very sort tricks.
In addition, If you are more curious about the development and knowing about tutorials then follow the below links