SwiftUI API / JSON Tutorial with Async Await Example

  Рет қаралды 25,434

Logan Koshenka

Logan Koshenka

Күн бұрын

This SwiftUI API / JSON / Networking tutorial walks you through how to properly use swiftui json parsing after fetching the data through an API. We create a struct and use swiftui codable, along with async / await to fill our list with the parsed data. SwiftUI is one of the best ways for beginners to learn how to create their own app in iOS / Swift. You will need Xcode to complete this tutorial.
We use a breaking bad API that can be found here:
Want to see more?
The Story Behind My New App: • I Turned This Life-Cha...
How I'd Learn to Code Today: • How a Senior iOS Engin...
RevenueCat / In-App Purchases Tutorial: • Easily Make Money with...
Follow me and say hello!
Instagram: / logankoshenka
Twitter: / logankoshenka​
#SwiftUI #API #JSON #Tutorial #Networking

Пікірлер: 123
@logankoshenka
@logankoshenka 2 жыл бұрын
I recently hit 800 subscribers - you guys are awesome!
@karljay7473
@karljay7473 2 жыл бұрын
Thanks for the tutorial! Can you post a link to the source code? I know it's not a lot of code, but I'm using Goodnotes on an iPad to make summaries of tutorials so I can keep track of all the tutorials and I import PDFs of the code, the mark it up with notes.
@rachelradford5193
@rachelradford5193 2 жыл бұрын
@@karljay7473 Idk if it helps but you could take a pic and import it onto a page in GoodNotes and make notes around it.
@karljay7473
@karljay7473 2 жыл бұрын
@@rachelradford5193 Thanks, that's what I ended up doing. Worked out pretty good.
@DianaFlores-H
@DianaFlores-H 2 жыл бұрын
thanks for the video, I´m from México, and i want to learn more about swiftUI
@logankoshenka
@logankoshenka 2 жыл бұрын
that’s great!!!
@rachelradford5193
@rachelradford5193 2 жыл бұрын
Idk who you are but I've watched a million tutorials about making async/await calls in swift, with varying degrees of success. I started doubting my life as a developer because it was so hard to understand, especially, coming from JavaScript. Idk why others feel its so necessary to over complicate things but I am so thankful for this video that I randomly found on a reddit thread!!
@logankoshenka
@logankoshenka 2 жыл бұрын
so awesome to hear - glad you liked it!!!
@rachelradford5193
@rachelradford5193 2 жыл бұрын
@@logankoshenka can you tell me why tis doesnt work? import SwiftUI import Foundation struct Trivia: Codable, Hashable { var category: String var type: String var difficulty: String var question: String //var isCorrect: Bool //var correct_answer: String ///var incorrect_answers: [String] } struct TriviaApi: View { @State private var trivia = [Trivia]() var body: some View { NavigationView { List(trivia, id: \.self) { trivia in VStack(alignment: .leading) { Text(trivia.question) .font(.headline) .foregroundColor(Color.blue) } }.navigationTitle("Trivia") .task { await fetch() } } } func fetch() async { guard let url = URL(string: "opentdb.com/api.php?amount=10") else { return print("Missing URL")} do { let (data, _) = try await URLSession.shared.data(from: url) if let decodedResponse = try? JSONDecoder().decode([Trivia].self, from: data) { trivia = decodedResponse } }catch { print("Error") } } } struct TriviaApi_Previews: PreviewProvider { static var previews: some View { TriviaApi() } } If not, It's ok!!
@logankoshenka
@logankoshenka 2 жыл бұрын
instead of printing “error” in the catch statement in your fetch function, remove the quotes and call: print(error) … there is a built in error variable there that will be available if it gets to that point and you should be able to get more of an idea of what is going wrong … the majority of my issues w this has been with the URL or the decoding type!
@rachelradford5193
@rachelradford5193 2 жыл бұрын
@@logankoshenka Thanks for answering! Unfortunately that didn't help me out. I have seen ppl use .utf8 encoding in their API calls, I might try that out?? Swift has been so fun but there are some really funky problems I've come across. I really hope they continue to improve async/await. Thanks again!
@chrisking821
@chrisking821 11 ай бұрын
Just thought id help the future Swift learners out there: this was the simplest and only tutorial that actually worked for my api.
@iLoveAppl3947
@iLoveAppl3947 7 ай бұрын
yeah but don't use this tutorial as an example for the real world applications. He doesn't handle errors and response like it should , doesn't follow MVVM and many more...
@logankoshenka
@logankoshenka 7 ай бұрын
this is a tutorial, meant to introduce people to async await, made when async / await came out … you are correct in that error handling is necessary in a real world application, and there are plenty of resources available out there … as far as MVVM, there is no one correct design pattern for SwiftUI or all of iOS for that matter … this is a quick, easy async / await tutorial, that’s it … a specific design pattern is irrelevant here, as several could be used depending on the scenario
@buresdv
@buresdv 2 жыл бұрын
This tutorial is truly amazing. I couldn't wrap my head around the async/await pattern no matter how many articles for "Swift beginners" I've read, but this video finally made it click. Thank you very much for the awesome tutorial :)
@logankoshenka
@logankoshenka 2 жыл бұрын
great to hear! thank you for watching!
@RamazanOzerr
@RamazanOzerr 5 ай бұрын
I have been watching many tutors on youtube and bro you are the funniest, now Im gonna have to watch all of your videos.. thank you bruther :D
@samtree99
@samtree99 2 жыл бұрын
Excellent video. As a retired C++ programmer, following ur example, I was able to make an app for my day trading needs. Thank you so much. Your code is short and precise, right at the points. You must be a great engineer!
@logankoshenka
@logankoshenka 2 жыл бұрын
thank you so much 🤝🙏
@saadsiddiqui9151
@saadsiddiqui9151 Жыл бұрын
Thankyou so much bro! I am a react native developer and just started learning SwiftUI, been looking for a solution to fetch data from an API but the nasty errors in xcode did not leave me alone. This solution worked well!!
@logankoshenka
@logankoshenka Жыл бұрын
great to hear!
@johnpill1
@johnpill1 2 жыл бұрын
Great short tutorial. I expanded on it by adding in a page displaying characters with names and clipped images.
@logankoshenka
@logankoshenka 2 жыл бұрын
nice! thanks for watching!
@mahtabhasan8513
@mahtabhasan8513 2 жыл бұрын
Simple and nice way of explaining Async/await, thanks Logan Appreciate your regular working on Swiftui, helped me a lot.
@logankoshenka
@logankoshenka 2 жыл бұрын
🙏🏼
@alexdiaz1712
@alexdiaz1712 2 жыл бұрын
I always look forward to your videos 😆 Thank you for the great tutorial!
@logankoshenka
@logankoshenka 2 жыл бұрын
I appreciate it!!!
@mattmarshall1834
@mattmarshall1834 2 жыл бұрын
Another great, to the point, explanation. Please keep making these, they're great.
@logankoshenka
@logankoshenka 2 жыл бұрын
thank you!
@deiv319x
@deiv319x 6 ай бұрын
Man I hope you remember me from a previous comment, I’ve been binge watching your videos and they are hilarious (and useful), I’m laughing at every video ending 😂
@logankoshenka
@logankoshenka 6 ай бұрын
hahah I appreciate it man!! thanks for letting me know 🤝
@shinygoldfat
@shinygoldfat 2 жыл бұрын
omg this is soooo clearly explained. I love you
@logankoshenka
@logankoshenka 2 жыл бұрын
😂❤️
@rungxanh2901
@rungxanh2901 2 жыл бұрын
Very intuitive and fun Logan! Much appreciated brother 🙏
@logankoshenka
@logankoshenka 2 жыл бұрын
thanks so much!
@codingkim5714
@codingkim5714 2 жыл бұрын
Awesome! I’d love to your simple example for API call. Thanks a lot!!
@logankoshenka
@logankoshenka 2 жыл бұрын
thanks for watching!
@thomasanderson1416
@thomasanderson1416 Жыл бұрын
Canvas Simulator in XCode is so unnervingly slow, I wonder why they don’t include the Swift Playgrounds previewer in XCode. It’s so much faster.
@ecptavares
@ecptavares Жыл бұрын
Nice video thank you.What if I have a JSON file with all my data can I upload it to a server and how would I read the data from this file and not from an api site? thank u
@logankoshenka
@logankoshenka Жыл бұрын
you should be able to use the URL to your server! if not, reading the JSON file locally is always an option too!
@evennorthug2585
@evennorthug2585 2 жыл бұрын
Good drive and explanations. Keep it up!
@logankoshenka
@logankoshenka 2 жыл бұрын
thank you!
@AnWithTheDrew
@AnWithTheDrew 2 жыл бұрын
Bro ...can u do more videos cuz I like your presentation. And easy to understand. I really like it bro. Keep Going ❤️
@logankoshenka
@logankoshenka 2 жыл бұрын
Of course - thank you so much!
@ronpeschel85
@ronpeschel85 Жыл бұрын
"cause I tried it" hahahahah love the channel man keep up this amazing and informative content :))
@logankoshenka
@logankoshenka Жыл бұрын
hahah thank you! I appreciate it!
@whistlerbrad
@whistlerbrad Жыл бұрын
Such a good tutorial 👏 entertaining as well
@logankoshenka
@logankoshenka Жыл бұрын
means a lot, thank you!!
@wevertonsantiago4305
@wevertonsantiago4305 2 жыл бұрын
Thanks so much! News subs here! Your videos are perfect 🙂👍
@logankoshenka
@logankoshenka 2 жыл бұрын
thank you!!!
@HanjayaWijangga
@HanjayaWijangga Жыл бұрын
i have error like this "Info.plist contained no UIScene configuration dictionary (looking for configuration named "(no name)")" anyone know why ??
@logankoshenka
@logankoshenka Жыл бұрын
hmm I'm not sure what this could be but if I'm able to find an answer I'll reply here.
@reece5863
@reece5863 Жыл бұрын
Thanks for the code, really needed this. Though the api is kaput.
@danuff
@danuff Жыл бұрын
One more question, where would you put .resume if one had too? Thanks again.
@danuff
@danuff 2 жыл бұрын
Hi....Fantastic Tutorial. I'm new to APIs. One quick question, why didn't you include the .resume in the code? Doesn't async need it? Thanks.
@logankoshenka
@logankoshenka 2 жыл бұрын
Yes haha I probably should have … in most instances you’ll be using .resume so it would’ve made sense to show a scenario where you’d need it. My bad!
@ignaciobrambilla7126
@ignaciobrambilla7126 2 жыл бұрын
Great tutorial! Thanks!!!
@logankoshenka
@logankoshenka 2 жыл бұрын
thank you!
@bocetta8936
@bocetta8936 2 жыл бұрын
I love your videos... please, make video to explain how use Google IPA with Oauth 2.0
@BlackLightning0Games
@BlackLightning0Games 2 жыл бұрын
Awesome tutorial. Can you make a video on custom line charts? It would be cool to show how to load data as the user drags the chart to the left or right.
@logankoshenka
@logankoshenka 2 жыл бұрын
Thanks! And yes - I want to look into the new SwiftUI Charts capabilities!
@jerrynkongolo
@jerrynkongolo Жыл бұрын
Thanks for the tutorial but for some reason there is no error but nothing shows up
@logankoshenka
@logankoshenka Жыл бұрын
a few of the comments mentioned that the API was discontinued … I would say to take the structure & concepts from this tutorial and use it for other APIs!
@jerrynkongolo
@jerrynkongolo Жыл бұрын
@@logankoshenka Thank you for the prompted reply. I will keep you posted. Thanks again for the amazing word your way of explaining is so easy to follow. I am looking forward to more tutorials
@logankoshenka
@logankoshenka Жыл бұрын
thanks so much!
@nhermuhlen
@nhermuhlen 2 жыл бұрын
Hey! question how can i reload the data every 1 minute? And by the way good video learned a lot from it.
@logankoshenka
@logankoshenka 2 жыл бұрын
Thanks! You’d have to use some sort of timer … I think if you do a couple timer tutorials, combine what you learned here & w those, and you’ll be able to repeat a task for as long / often as you’d like! But yeah, look into the Timer type.
@thomasanderson1416
@thomasanderson1416 Жыл бұрын
it’s all good but all the guards and try statements they’re daunting.
@logankoshenka
@logankoshenka Жыл бұрын
100%
@nimor2967
@nimor2967 2 жыл бұрын
Very good thank you.
@logankoshenka
@logankoshenka 2 жыл бұрын
thanks 😁
@im_the_raymond
@im_the_raymond 2 жыл бұрын
Good job dude!, im new fan :) Can you do video about testing “swift playground 4” app ? It’s on ipad and mac.
@logankoshenka
@logankoshenka 2 жыл бұрын
thank you! and yes I’ll look into it :)
@jayvalance
@jayvalance 2 жыл бұрын
Great!! Thx 🙏🏼
@logankoshenka
@logankoshenka 2 жыл бұрын
thanks for watching!
@gouthamshiv
@gouthamshiv 2 жыл бұрын
Thank you for sharing this content, simple and to the point. Can you please let me know how to pass header values ?
@logankoshenka
@logankoshenka 2 жыл бұрын
thank you! can you elaborate a little more? what do you mean header values?
@gouthamshiv
@gouthamshiv 2 жыл бұрын
@@logankoshenka a http header, like an api-key?
@logankoshenka
@logankoshenka 2 жыл бұрын
ahhh ok let me look into it
@gouthamshiv
@gouthamshiv 2 жыл бұрын
@@logankoshenka I’m actually a FullStack developer, but I’m just couple of days old in the realm of iOS development 😅. I have some APIs to call, but they require an API Key to be passed as part of the header. I saw some other references, but I really liked your coding style (to the point & no unnecessary complexity) and I wanted to know the simple and right way from you. If possible, can add on what is the best practice to place such keys? In a normal web app, it’ll be injected during the build process
@logankoshenka
@logankoshenka 2 жыл бұрын
love it! and typically in my experiences you’ll see certain APIs requiring keys, and you’ll just plug that in during configuration or when you’re setting up your URL … I usually just store that as a variable and plug it in as needed
@JacobBoyd-f5o
@JacobBoyd-f5o 11 ай бұрын
the api is now going to a web page that requires a gmail sign up just letting you
@JacobBoyd-f5o
@JacobBoyd-f5o 11 ай бұрын
also sending to other pages not sure what's going on there
@logankoshenka
@logankoshenka 11 ай бұрын
mannn I picked the wrong API when making this video!!
@appfree2u907
@appfree2u907 2 жыл бұрын
how to setup admob to swiftui?
@swaragandhi6122
@swaragandhi6122 Жыл бұрын
Your video is amazing. I have a doubt though. How to limit number of quotes based on input from textfield?
@logankoshenka
@logankoshenka Жыл бұрын
for limiting number of quotes, you could do List or ForEach(0…10) for example
@swaragandhi6122
@swaragandhi6122 Жыл бұрын
@@logankoshenka thank you 😊
@logankoshenka
@logankoshenka Жыл бұрын
no problem!
@tarun_reddy
@tarun_reddy Жыл бұрын
This was so tuff
@blackmoses4083
@blackmoses4083 2 жыл бұрын
How do you decode a single object and not an array of objects?
@logankoshenka
@logankoshenka 2 жыл бұрын
Your api endpoint will have to point to a specific object, usually done via a unique ID, and then you just change the @State variable and the array you’re decoding into that single type
@blackmoses4083
@blackmoses4083 2 жыл бұрын
@@logankoshenka How exactly would I declare it as a single type? @State private var pets = Pets ?
@logankoshenka
@logankoshenka 2 жыл бұрын
@State private var pet = Pet … then you won’t be able to use a list, so maybe just display 1 text on the screen since you’ll only be getting a single object
@blackmoses4083
@blackmoses4083 2 жыл бұрын
​@@logankoshenka Can't I use a ForEach loop interchangeably with a List? But yea my state variable didn't work , I was met with a "Expected member name or constructor call after type name" error
@logankoshenka
@logankoshenka 2 жыл бұрын
Yeah you could make a list just repeat the same thing if you want … it may have to be var pet: Pet or var pet = Pet() instead
@TheAshokWB
@TheAshokWB 2 жыл бұрын
Thank YOu
@logankoshenka
@logankoshenka 2 жыл бұрын
thanks for watching!
@markme7092
@markme7092 2 жыл бұрын
Please next tutorial of image api
@logankoshenka
@logankoshenka 2 жыл бұрын
Have you seen my AsyncImage video?
@victorriurean
@victorriurean Жыл бұрын
nice
@logankoshenka
@logankoshenka Жыл бұрын
thank you!
@drnhnc5034
@drnhnc5034 3 ай бұрын
BOOM LOOK AT THAT HUH :D:D
@jovanyjimenez5808
@jovanyjimenez5808 Жыл бұрын
Great attitude, but you need to explain what you are using and why. learning is not about getting to the destination without knowing how we got there.
@logankoshenka
@logankoshenka Жыл бұрын
fair point - appreciate the feedback
@ARKillsZombies1
@ARKillsZombies1 Жыл бұрын
great video sadly I think this api is offline now :/
@logankoshenka
@logankoshenka Жыл бұрын
ahhh dang! I’ll have to look into it.
@nickpavlov4012
@nickpavlov4012 Жыл бұрын
ya doesnt work (((
@nickpavlov4012
@nickpavlov4012 Жыл бұрын
@@logankoshenka lol why did you delete my comment?
@logankoshenka
@logankoshenka Жыл бұрын
@@nickpavlov4012 it’s still there bub, I don’t delete any comments
@logankoshenka
@logankoshenka Жыл бұрын
I wish this API was still active but the same concepts apply to whatever APIs you find / use! 💪🏼
@ashin6651
@ashin6651 2 жыл бұрын
nice nice nice
@logankoshenka
@logankoshenka 2 жыл бұрын
thanks!
Swift API Calls for Beginners (Networking) - Async Await & JSON
25:35
How to use async / await keywords in Swift  | Swift Concurrency #3
20:40
Swiftful Thinking
Рет қаралды 26 М.
Bike Vs Tricycle Fast Challenge
00:43
Russo
Рет қаралды 94 МЛН
Шок. Никокадо Авокадо похудел на 110 кг
00:44
HAH Chaos in the Bathroom 🚽✨ Smart Tools for the Throne 😜
00:49
123 GO! Kevin
Рет қаралды 14 МЛН
Easily Make Money with Apps (SwiftUI RevenueCat Tutorial)
20:09
Logan Koshenka
Рет қаралды 7 М.
Ch. 6.3 API call with URLSession and JSON parsing with SwiftUI (CatchEmAll app)
28:17
SwiftUI MVVM Tutorial: Simple Example with ObservableObject
12:43
Logan Koshenka
Рет қаралды 20 М.
App Devlog 1 - What Could Possibly Go Wrong?
14:08
Logan Koshenka
Рет қаралды 711
What is JSON - JSON Parsing in Swift
24:15
CodeWithChris
Рет қаралды 74 М.
Stop using Spacer() in SwiftUI
6:02
Flo writes Code
Рет қаралды 12 М.
Bike Vs Tricycle Fast Challenge
00:43
Russo
Рет қаралды 94 МЛН