SwiftUi Load Local Json Data - json

I actually learning SwiftUi, and at the moment i don't get into my problem.
I try to load Json Data from my local Json File !
I have searched many many many Post but nothing really helped me, that is why i have chosen to make this post.
I show you how i possible tried it.
Path: Data/test.json
[
{
"id": 1,
"name": "",
"trade_name": "",
"short_name": "",
"test": "",
"test1": "",
"test2": "",
"test3": "",
"test4": "",
"test5": "",
"test6": "",
"test7": "",
"test8": [],
"test9": "",
"test10": "",
"test11": "",
"test12": ""
},
]
Path: App/DataLoader.swift
import Foundation
public class DataLoader {
#Published var contentData = [JSONData]()
init(){
load()
sort()
}
func load(){
if let fileLocation = Bundle.main.url(forResource: "test", withExtension: "json"){
do {
let data = try Data(contentsOf: fileLocation)
let jsonDecoder = JSONDecoder()
let dataFromJson = try jsonDecoder.decode([JSONData].self, from: data)
self.contentData = dataFromJson
} catch {
print(error)
}
}
}
func sort(){
self.contentData = self.contentData.sorted(by: { $0.id < $1.id})
}
}
Here in this File i add the variables.
Path: App/JsonData.swift
import Foundation
struct JSONData: Codable {
var id: Int
var name: String
var trade_name: String
var short_name: String
var test: String
var test1: String
var test2: [String:String]
var test3: [String:String]
var test4: String
var test5: String
var test6: [String:String]
var test7: String
var test8: [String:String]
var test9: String
var test10: String
var test11: String
var test12: String
}
I didn't set the Display yet but i want it in a List.
In my ContentView.swift File i would load the Data from that Json File !
Path: App/ContentView.swift
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
Home()
.navigationTitle("Test Interface")
.navigationBarTitleDisplayMode(.inline)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct Home : View {
let data = DataLoader().contentData
var body: some View {
data[IndexPath.row].id
}
}
When i did it like that, it shows some Errors.
Return type of property 'body' requires that 'Int' conform to 'View'
Instance member 'row' cannot be used on type 'IndexPath'; did you mean to use a value of this type instead?
Regards
CreatingBytes

SwiftUI is not UIKit, there are no index paths.
The body property of a View must return some View, nothing else.
First of all adopt Identifiable
struct JSONData: Codable, Identifiable { ...
Then adopt ObservableObject in DataLoader
public class DataLoader : ObservableObject {
You have to observe the data in the root view, replace ContentView with
struct ContentView: View {
#StateObject var data = DataLoader()
var body: some View {
NavigationView {
Home(data: data)
.navigationTitle("Test Interface")
.navigationBarTitleDisplayMode(.inline)
}
}
}
In Home hand over data and use ForEach
struct Home : View {
#ObservedObject var data : DataLoader
var body: some View {
VStack() {
ForEach(data.contentData) { item in
HStack {
Text(item.name)
Spacer()
Text("\(item.id)")
}
}
}
}
}
Alternatively replace ForEach with List to get a table view
I highly recommend to watch the videos of WWDC 2019 and 2020 about SwiftUI

Related

Importing JSON from URL within a view

So, new to swift and was able to peice this together by reading and watching videos however i reached a point after countless hours searching for a solution..
Basically what the app does is scan's a qr code, parses the url it reads from the qr code to get a key, then I am appending that key to the api url, and i want to output the results from the api to the screen. however I am receiving an error Type '()' cannot conform to 'View' in xcode
Here is sample json data
[
{
"id": "160468",
"sport": "BASKETBALL",
"year": "2020",
"brand": "PANINI PRIZM",
"cardNumber": "278",
"playerName": "LaMELO BALL",
"extra": "",
"gradeName": "MINT",
"grade": "9",
"serial": "63585906",
"authDate": "1656406800",
"link": "https://www.example.com/certificate-verification?certificateNumber=63585906"
}
]
here is my contentview
import SwiftUI
import CodeScanner
extension URL {
var components: URLComponents? {
return URLComponents(url: self, resolvingAgainstBaseURL: false)
}
}
extension Array where Iterator.Element == URLQueryItem {
subscript(_ key: String) -> String? {
return first(where: { $0.name == key })?.value
}
}
struct Card: Decodable {
let sport: String
let year: String
let brand: String
let cardNumber: String
let playerName: String
let extra: String
let gradeName: String
let grade: String
let serial: String
}
struct ContentView: View {
#State var isPresentingScanner = false
#State var scannedCode: String = ""
var scannerSheet : some View {
CodeScannerView(
codeTypes: [.qr],
completion: { result in
if case let .success(code) = result {
self.scannedCode = code.string
self.isPresentingScanner = false
}
}
)
}
func getQueryStringParameter(url: String, param: String) -> String? {
guard let url = URLComponents(string: url) else { return nil }
return url.queryItems?.first(where: { $0.name == param })?.value
}
var body: some View {
VStack(spacing: 10) {
Image("logo-white")
.offset(y: -200)
if let urlComponents = URL(string: scannedCode)?.components,
let cert = urlComponents.queryItems?["certificateNumber"] {
//Text(cert)
let apihit = URL(string: "https://app.example.com/api.php?apikey=xxx&cert=\(cert)")!
//Text(apihit.absoluteString)
var request = URLRequest(url: apihit)
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
let task = URLSession.shared.dataTask(with: apihit) { data, response, error in
if let data = data {
if let cards = try? JSONDecoder().decode([Card].self, from: data) {
print(cards)
} else {
print("Invalid Response")
}
} else if let error = error {
print("HTTP Request Failed \(error)")
}
}
}
Button("Scan QR Code") {
self.isPresentingScanner = true
}
.padding()
.background(Color(red: 0, green: 0, blue: 0.5))
.foregroundColor(.white)
.clipShape(Rectangle())
.cornerRadius(20)
.sheet(isPresented: $isPresentingScanner) {
self.scannerSheet
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(.gray)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
I have tried countless tutorials online, however none of them show how to do it within the view, which I believe is where it belongs because I don't get the actual json url until after I read the qr code..
To populate a link within a view, use Webkit.
First you need to import Webkit. Then, create a WebView which conforms to the UIViewRepresentable protocol.
Here is the Apple documentation. This is the easiest way to integrate web content into your app.
I hope this helps!

Parse a Json from a url with entry values and list them in SwiftUI

I'm stuck on this.
I have a json that I'm parsing and that Json has an entry value and I want to do a swift list based in user input that will trigger from the external json different responses.
I have the following code so far
import SwiftUI
import Combine
import Foundation
var lokas : String = "ABCY"
struct ContentView: View {
#State var name: String = ""
#ObservedObject var fetcher = Fetcher()
var body: some View {
VStack {
TextField("Enter Loka id", text: $name)
List(fetcher.allLokas) { vb in
VStack (alignment: .leading) {
Text(movie.device)
Text(String(vb.seqNumber))
.font(.system(size: 11))
.foregroundColor(Color.gray)
}
}
}
}
}
public class Fetcher: ObservableObject {
#Published var allLokas = [AllLoka_Data]()
init(){
load(devices:lokas)
}
func load(devices:String) {
let url = URL(string: "https://xxx.php?device=\(devices)&hours=6")!
URLSession.shared.dataTask(with: url) {(data,response,error) in
do {
if let d = data {
let decodedLists = try JSONDecoder().decode([AllLoka_Data].self, from: d)
DispatchQueue.main.async {
self.allLokas = decodedLists
}
}else {
print("No Data")
}
} catch {
print ("Error")
}
}.resume()
}
}
struct AllLoka_Data: Codable {
var date: String
var time: String
var unix_time: Int
var seqNumber : Int
}
// Now conform to Identifiable
extension AllLoka_Data: Identifiable {
var id: Int { return unix_time }
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
But I just added a Global variable to test it, but how do I pass the variable name to make the function work ?
Thank you

ObservedObject, derived from Json, not loading into Picker, but loads in List. In SwiftUI, Xcode 11

I'm new to mobile development, and in the process of learning SwiftUI.
I've been struggling to figure out what's wrong with my picker. I am successfully returning the data from my URLSession, adding it to my model. I can confirm this by adding my #ObservedObject to a List, which returns all of the items. Putting the same #ObservedObject into a picker returns an empty picker for some reason. Any help would be appreciated. Thanks.
Here's my view with the Picker(). When run, the Picker is empty. I can comment out the Picker(), leaving just the ForEach() with Text(), and the text appears.
import Foundation
import Combine
import SwiftUI
struct ContentView: View {
#ObservedObject var countries = CulturesViewModel()
#State private var selectedCountries = 0
var body: some View {
VStack {
//loop through country array and add them to picker
Picker(selection: $selectedCountries, label: Text("Select Your Country")) {
ForEach(0 ..< countries.cultures.count, id: \.self) { post in
Text(self.countries.cultures[post].Culture).tag(post)
}
}.labelsHidden()
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Here's my ViewModel. It set's the #Published variable to the results of the JSON request in the WebService(). If I hard-code the #Published variable to the value that's begin returned, the Picker works.
import Foundation
import Combine
import SwiftUI
class CulturesViewModel: ObservableObject {
init() {
fetchCultures()
}
#Published var cultures = [Culture](){
didSet {
didChange.send(self)
}
}
private func fetchCultures(){
WebService().GetCultures {
self.cultures = $0
}
}
let didChange = PassthroughSubject<CulturesViewModel, Never>()
}
Here's my WebService(). Unfortunately, I'm unable to share the JSON url, I've added in the json that's returned.
import Foundation
import SwiftUI
import Combine
class WebService {
func GetCultures(completion: #escaping([Culture]) ->()) {
guard let url = URL("")
[
{
"CultureId": 0,
"Culture": "Select Your Country"
},
{
"CultureId": 1078,
"Culture": "English (United States)"
},
{
"CultureId": 6071,
"Culture": "English (Canada)"
}
]
URLSession.shared.dataTask(with: url) { (data,_,_) in
do {
if let data = data {
let culturesList = try JSONDecoder().decode([Culture].self, from: data)
DispatchQueue.main.async {
completion(culturesList)
}
} else {
DispatchQueue.main.async {
completion([])
}
}
} catch {
print(error)
DispatchQueue.main.async {
completion([])
}
}
}.resume()
}
}
Lastly, here's my Model.
import Foundation
struct Culture: Codable, Identifiable {
var id = UUID()
var CultureId: Int
var Culture: String
}
The work around to make the picker refresh is to add a unique id. Refreshing (or) reloading the countries, will create a new UUID for the picker items. This will force the picker to refresh. I've modified your code to include an id.
//loop through country array and add them to picker
Picker(selection: $selectedCountries, label: Text("Select Your Country")) {
ForEach(0 ..< countries.cultures.count, id: \.self) { post in
Text(self.countries.cultures[post].Culture).tag(post)
}
}.labelsHidden()
.id(UUID())
This seems to be a known issue with the picker.

Get data from server with URLSession

everyone. I have a problem. I can't figure out. I want get data ( JSON array ) from my server, it doesn't work correctly. It doesn't show nothing. HTTP header: text/plain . I use this source as example: https://medium.com/#rbreve/displaying-a-list-with-swiftui-from-a-remote-json-file-6b4e4280a076 . It's my code and data:
import Foundation
import SwiftUI
import Combine
struct Medewerker: Codable, Identifiable, Hashable {
public var id : Int
public var naam: String
public var voornaam: String
}
struct SwiftUIView2: View {
#ObservedObject var fetcher = MedewerkerFetcher()
var body: some View {
VStack {
List(fetcher.medewerker) { medewerker in
VStack (alignment: .leading) {
Text(medewerker.naam)
Text(medewerker.voornaam)
.font(.system(size: 11))
.foregroundColor(Color.gray)
}
}
}
}
}
struct SwiftUIView2_Previews: PreviewProvider {
static var previews: some View {
SwiftUIView2()
}
}
public class MedewerkerFetcher: ObservableObject {
#Published var medewerker = [Medewerker]()
init(){
load()
}
func load() {
let url = URL(string: "http://anwin.be/src/public/medewerker")!
var request = URLRequest(url: url)
request.addValue("text/plain", forHTTPHeaderField: "Accept")
URLSession.shared.dataTask(with: request) {(data,response,error) in
if let mimeType = response?.mimeType, mimeType == "text/plain"{
do {
if let d = data {
let decodedLists = try JSONDecoder().decode([Medewerker].self, from: d)
DispatchQueue.main.async {
self.medewerker = decodedLists
}
}else {
print("No Data")
}
} catch {
print ("Error")
}
}
}.resume()
}
}
Data. Json from server:
[
{
"id": 1,
"naam": "Fanoberov",
"voornaam": "Andre"
},
{
"id": 2,
"naam": "Kunitski",
"voornaam": "Dzmitry"
},
{
"id": 7,
"naam": "Karim",
"voornaam": "Nassar"
}
]
Can you help me, please ?
Thank you.
error is:
{Error Domain=kCFErrorDomainCFNetwork Code=-1022 "(null)"}, NSErrorFailingURLKey=http://dtcws.azurewebsites.net/ShowImg.aspx?params=dtc_376_0_True_False_22, NSLocalizedDescription=The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.})
you have to change your info.plist
add this:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>

Error in parsing JSON using codeable [duplicate]

This question already has answers here:
Can comments be used in JSON?
(58 answers)
Closed 4 years ago.
I am trying to pass some data from a JSON file using the new(ish) codeable capability in Swift. I have used the below syntax before without issue. I believe I may have something set up wrong, however, as I can't seem to understand why I keep receiving the below message when the JSON format has been approved by a JSON parser.
The error message:
error:dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: "The given data was not valid JSON.", underlyingError: Optional(Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.})))
The code in my QuestionFactory file...
class QuestionFactory {
func parseJSON(filename fileName: String) -> Quiz? {
if let url = Bundle.main.url(forResource: fileName, withExtension: "json") {
print(url)
do {
let data = try Data(contentsOf: url)
let decoder = JSONDecoder()
print("data received is \(data.count) bytes:\n\(data)")
print(data)
print(data as NSData)
let jsonData = try decoder.decode(Quiz.self, from: data)
print(jsonData)
} catch {
print("error:\(error)")
}
}
return nil
}
}
The code in my initial ViewController:
class LaunchScreen: UIViewController {
private var quiz: Quiz?
private let jsonFileName = "QuizData"
func viewDidLoad() {
super.viewDidLoad()
createQuiz()
}
private func createQuiz() {
let questionFactory = QuestionFactory()
guard let parsedQuiz = questionFactory.parseJSON(filename: jsonFileName) else {
print("Error creating quiz")
return
}
quiz = parsedQuiz
}
func movesToMainMenuScreen() {
let transition = CATransition()
transition.duration = 1.5
transition.type = kCATransitionFade
self.navigationController?.view.layer.add(transition, forKey:nil)
let mainMenuVC: UIViewController = MainMenuViewController(quiz: quiz!) >> I am receiving an error here as well, perhaps due to my mainMenuVC's required init?
navigationController?.pushViewController(mainMenuVC, animated: false)
}
In my mainMenuViewController:
class mainMenuViewController: UIViewController {
private var quiz: Quiz! {
didSet {
tableViewAdapter = AnswerTableViewAdapter(answers: quiz.questions[0].answers) >> Although, it is not obviously reaching this far to read through the JSON.
}
required init(quiz: Quiz) {
super.init(nibName: nil, bundle: nil)
defer {
self.quiz = quiz
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
The JSON looks like this...
{
"questions":[
{
"text": "1. Where will the 2022 World cup be held?",
"answers": [
{
"text": "Qatar",
"isCorrect": true,
"answerType": "2"
},
{
"text": "دولة قطر",
"isCorrect": true,
"answerType": "1"
},
{
"text": "Jamaica",
"isCorrect": false,
"answerType": "0"
},
{
"image":"qatarFlag",
"isCorrect": true,
"answerType": "3"
}
]
}]
}
The Model files....
Quiz.swift
import Foundation
struct Quiz: Decodable {
var questions: [Question]
}
Question.swift
import Foundation
struct Question: Decodable {
var text: String
var answers: [Answer]
}
Answer.swift
import Foundation
struct Answer: Decodable {
var text: String
var image: String
var isCorrect: Bool
var answerType: String
}
There is extra
]
}
added on last two lines remove these both closing brackets and try to parse JSON.
And make your model's properties to optional to avoid nullable crashes.
The Model files....
Quiz.swift
import Foundation
struct Quiz: Decodable {
var questions: [Question]?
}
Question.swift
import Foundation
struct Question: Decodable {
var text: String?
var answers: [Answer]?
}
Answer.swift
import Foundation
struct Answer: Decodable {
var text: String?
var image: String?
var isCorrect: Bool?
var answerType: String?
}