Swift API Calls for Beginners (Networking) - Async Await & JSON

Поділитися
Вставка
  • Опубліковано 29 чер 2024
  • Go to squarespace.com/seanallen to save 10% off your first purchase of a website or domain using code SEANALLEN.
    Learning how to write network calls as a beginner in Swift can be daunting. I remember it taking me a long time to fully grasp them. In this video I break down the basics of how network calls work and introduce you to the concept of JSON (JavaScript Object Notation).
    I show you how to download data from the GitHub API with a network call and display a basic user profile page using that data. For this we use Swift Concurrency with Async Await.
    This tutorial was created using Xcode 14 and iOS 16.
    My iOS Dev Courses:
    seanallen.teachable.com/
    iOS Developer Interview Questions Playlist:
    • Swift Interview Tips
    Twitter:
    Sean Allen - / seanallen_dev
    Hired.com:
    hired.com/x/1n01g
    Book and learning recommendations that help out the channel if you decide to purchase (Affiliate Links):
    Paul Hudson's Hacking With Swift:
    gumroad.com/a/762098803
    Donny Wals - Combine:
    gumroad.com/a/909014131
    Mark Moeyken’s SwiftUI Books:
    www.bigmountainstudio.com/swiftui-views-book/fzc51
    Objc.io Books (Thinking in SwiftUI & Advanced Swift):
    gumroad.com/a/656585843
    #swift #softwaredeveloper #iosdeveloper
    Timestamps:
    0:00 - Networking in iOS Apps
    0:32 - This is hard!
    1:06 - What is JSON?
    2:06 - Using CURL to test a response
    3:39 - Open API, OAuth, API Keys - Out of Scope
    4:14 - My 4 Step Process for API Calls
    5:10 - Using JSON to inform your design
    5:46 - Create Models from JSON
    9:13 - Network Call Code
    18:10 - Networking Code Review
    20:02 - Using the network call
    22:48 - Connecting the UI
  • Наука та технологія

КОМЕНТАРІ • 165

  • @seanallen
    @seanallen  Рік тому +8

    Want to dive deeper into iOS development? Check out my iOS Dev courses (UIKit & SwiftUI) at seanallen.teachable.com

    • @abdulrb786
      @abdulrb786 11 місяців тому +1

      Really informative. If possible could you create a playlist for different type of network calls that you have encountered example of which are, dependent n/w call, paginations etc. And best ways to call them, such as using dispatch group etc. that would really help the Developer across globe as all the developer do make n/w all the time.

    • @FriendlyNeighborhoodBlindKiid
      @FriendlyNeighborhoodBlindKiid 5 місяців тому

      Is there any way that you can share a link to that file, especially because it's just a single file? I'm blind so I can't follow along with the video super well. I love how you explain the concepts, but there's a couple things with the symbols and stuff that I want to get my computer to read.

  • @parkerrex
    @parkerrex 8 місяців тому +20

    Coming from web world. Finally found a good creator for swift. Time to binge….

  • @mingy7017
    @mingy7017 Рік тому +14

    Thank you SO MUCH Sean this goes straight to the point and very informative and easy to follow the logic behind. Very very well conveyed! ❤

    • @seanallen
      @seanallen  Рік тому

      Thanks Ming! Glad you liked it.

  • @jaylensmith6308
    @jaylensmith6308 Рік тому +5

    I’m moving away from completion-type networks calls and I am happy that this video is out

    • @seanallen
      @seanallen  Рік тому +1

      Happy to help with this transition.

  • @ArthasFede
    @ArthasFede 5 місяців тому +4

    Simple, quick and streigth to the point. Love this tutorial!!
    Thanks Sean! 🙏🏻

    • @seanallen
      @seanallen  5 місяців тому

      Glad you liked it!

  • @GouravSinghalMusiTech
    @GouravSinghalMusiTech 6 місяців тому +2

    You should keep making videos... it should be a request from whole iOS community! Kudos to you!

  • @AngleIron426
    @AngleIron426 11 місяців тому

    I coded along and it worked beautifully! Learned a lot. Wonderful introduction to async and URL session. Thank you!

  • @manwithllama
    @manwithllama Рік тому +2

    9:45 Sean! I love that you talk about being confused [as a beginner] by things being all over the place in different directories/files.
    That happens for me too.
    This type of "compassion" is rare. So many teachers just assume, and don't have the ability-or don't care to-reach back into the past and remember what it was like to start out.
    Keep doing this. It sets you apart as a teacher.
    A professional writer friend of mine-who used to have an excellent podcast-said this all the time: "You're not alone." He peppered it in his writing. And often started articles that way. It was his go-to.
    Why is this so important? Because ultimately people feel alone. [Most won't admit it.]
    We want to belong.
    To know *you* struggled at one point just like I am today? That makes me included. Like i'm not just some idiot that will never get this. It's empowering: "I got this."
    Thanks again. Love learning from you (and @twostraws).

    • @SHOLINGER
      @SHOLINGER 4 місяці тому

      bro your twitter handle ?
      and that comment was very nice of you !

  • @bleulejour
    @bleulejour Рік тому

    Best tutorial ever for Networking and API calls in Swift UI!

  • @iLoveAppl3947
    @iLoveAppl3947 5 місяців тому +2

    yeah i have watched 20+ videos about this topic and yet this is the only video that worked for me. Thanks bud

    • @seanallen
      @seanallen  5 місяців тому +1

      I was the same way when I tried learning network calls. It's a tough topic. Happy to hear my video resonated with ya!

  • @WestCoastAce27
    @WestCoastAce27 Рік тому +2

    Great job. So much easier than the Obj-C.
    If you haven’t, since you brought it up, you might do a pros/cons video on multiple files. I’m sure there are going to be many opinions on ‘how big is too big’ for 1 file.

  • @patgoo2
    @patgoo2 9 місяців тому +1

    Very good training video and an important training video to watch. I like how you demonstrate network calls and syncing the API data, coding the JSON, and creating a starter page all on one Xcode file. This makes it much easier to follow.

    • @seanallen
      @seanallen  9 місяців тому

      Glad it was helpful, Pat!

  • @tharindudarshana6480
    @tharindudarshana6480 Рік тому +2

    Thank you ! I'm learning swift as an experienced Spring/Java developer and your tutorials help me to grab the fundaments of Swift with actual functionality which is interesting to follow. Simply great & please keep up posting

    • @seanallen
      @seanallen  Рік тому +1

      Glad you like them!

    • @mario_luis_dev
      @mario_luis_dev 11 місяців тому

      I’m on the same boat as you! Spring -> Swift. Really digging everything about Swift development so far, I only wish I had jumped into this sooner!

  • @cheeesecake6989
    @cheeesecake6989 10 місяців тому

    Thanks so much. It is true - for a beginner having all the code in one place makes it easier to understand.

  • @ash_9651
    @ash_9651 11 місяців тому +1

    sean thank you soooo much, this cleared up a lot for me. i love how you progessivly recap the code it really helps me understand!

    • @seanallen
      @seanallen  11 місяців тому

      Glad it helped!

  • @KalDillard
    @KalDillard 10 місяців тому

    Thank you! I started out learning iOS until I went to Flutter! I’m looking to learn more Native iOS and you made this easy for me. This is pretty similar to making network calls in Flutter.

  • @ouadiae2706
    @ouadiae2706 Рік тому +3

    i was looking for something like this for ages, thank you

  • @thanasisathanasi4965
    @thanasisathanasi4965 6 місяців тому

    I am mostly a web developer using Javascript, PHP, MySQL and Apache server and I developed my own framework using these tools. I have decided to expand my framework to handshake with Swift and Kotlin ecosystems so I can develop apps that communicate with my other framwork! This video was my starting point. !! Thanks

  • @samr.4692
    @samr.4692 Рік тому +1

    Great video. Covered multiple subtle but important topics. Thank you.

  • @davidcbeaudoin
    @davidcbeaudoin Рік тому +1

    I've been doing Apps with network requests for a while, but I haven't messed around with using async await yet. This lesson will be really useful for trying to apply it in a new App I'm building. Thank you for a clear explanation as always!

    • @seanallen
      @seanallen  Рік тому +1

      Glad you liked it, David!

  • @billblair3155
    @billblair3155 19 днів тому

    Good stuff Sean. Thanks much! One of the best presentations I've seen. And I've seen a lot. Thx again.

  • @MassonLifestyle
    @MassonLifestyle 5 місяців тому +1

    Great video. I suspect I will be saving this and watching it a couple of times. Thanks!

    • @seanallen
      @seanallen  5 місяців тому

      Glad it was helpful!

  • @SB-jq4jc
    @SB-jq4jc 9 місяців тому +2

    You explained this so well! Thank you for this!

    • @seanallen
      @seanallen  8 місяців тому +1

      Glad it was helpful!

  • @dineshdas9983
    @dineshdas9983 Рік тому +1

    What a video🤩 , Im very much happy to watch this video, 25 minutes goes like a minute great tutor i ever had

  • @rmessentials4048
    @rmessentials4048 5 місяців тому +1

    This is a nice and inspiring tutorial. Thanks for the video SeanAllen I will definitely watch it again.

    • @seanallen
      @seanallen  5 місяців тому

      Glad it was helpful!

  •  Рік тому +1

    Thank you so much. I didn't know that we can also use async await keywords on Swift and on networking. I was doing my all networking codes with dataTask. It gave me a new perspective. Also It's nice to see Paul here too.

    • @seanallen
      @seanallen  Рік тому

      Happy to show you something new!

  • @rodrigorgpsk8
    @rodrigorgpsk8 Місяць тому

    What an amazing video!
    With that I did make a small app with navigation views to check bio and followers in order to get more expertises about async api calls and screen navigations!
    Thanks Sean! Keep doing these awesome videos! 😎

  • @antoniovuono4521
    @antoniovuono4521 Місяць тому

    The best tutorial i already see ! very usefull, thanks dude !

  • @mranjithamranjitha6724
    @mranjithamranjitha6724 6 місяців тому

    Ur notes&teaching is very clearly... thank u

  • @user-wk8tq8qc1p
    @user-wk8tq8qc1p 5 місяців тому +1

    Nice! Very detailed explanation. I am a seasoned software engineer just getting my hands dirty with Swift, and all of these concepts make a lot of sense to me. Thanks.

    • @seanallen
      @seanallen  5 місяців тому +1

      Glad it was helpful!

  • @hustlerking4295
    @hustlerking4295 Рік тому +1

    Thank you! Thank you! Thank you!
    Very helpful video! Much wanted!

  • @kal5211
    @kal5211 11 місяців тому +1

    Excellent tutorial! Thanks Sean.

    • @seanallen
      @seanallen  11 місяців тому

      Glad it was helpful!

  • @user-uy8rw4yw1t
    @user-uy8rw4yw1t Рік тому +2

    It's the best video about network calls !!!

  • @FranciscoHernandez-ij3kd
    @FranciscoHernandez-ij3kd 5 місяців тому +1

    Thank you Sean, I appreciate your work.

  • @sebasyanni1258
    @sebasyanni1258 3 місяці тому +1

    Im taking the course of angela, basics of swift. And i look up for your videos to have a certain idea of what im going to see so im not fully lost haha. Thanks for sharing your knowledge

  • @taylorenglish6237
    @taylorenglish6237 3 місяці тому +1

    Thank you for the clear video!!

  • @absar66
    @absar66 Рік тому +1

    Great refresher..Thanks Allen

  • @ericka.montanez6821
    @ericka.montanez6821 6 місяців тому

    Fantastic explanation.

  • @marinab.6647
    @marinab.6647 Місяць тому +1

    omg this worked!!! thank you!

  • @ivanmatkovic
    @ivanmatkovic Рік тому

    Excellent content. Truth be told, I'm not a huge fan of excessive do { } catch blocks as they tend to make me anxious for some reason 😅. I usually lean towards using closures or creating wrappers around do/catch, maybe it's just a force of habit.
    However, each new release of Swift & SwiftUI seems to be improving tremendously. I'm optimistic that they'll soon address the navigation issues - I'm aware of NavigationStack, but sadly it's only compatible with iOS16+ and I haven't had the chance to try it out yet.
    A minor suggestion for this kind of videos would be to include a gist with a code in the description.

  • @jkeee
    @jkeee 11 місяців тому

    Thank you man, you are the bestest

  • @crackedpistachio6001
    @crackedpistachio6001 8 місяців тому +1

    You are a legend man

    • @seanallen
      @seanallen  7 місяців тому

      Haha, happy to help :)

  • @user-qi8pw4bv4u
    @user-qi8pw4bv4u Рік тому +1

    Hello from Ukraine. Nice stuff! Thanks a lot for sharing your knowledge and wisdom! Keep it going! You are great!

  • @filipecanzi
    @filipecanzi Рік тому

    Thanks Sean!!

  • @Bobin13
    @Bobin13 7 місяців тому +1

    OG video, Thanks for the help.

  • @bklyntrev6774
    @bklyntrev6774 Рік тому +2

    My guy 💪🏿

  • @umairnazim3565
    @umairnazim3565 4 місяці тому +1

    Amazing explanation

    • @seanallen
      @seanallen  4 місяці тому +1

      Glad you liked it

  • @mr.sergio8773
    @mr.sergio8773 Рік тому +1

    Thank you for the video. I would like to know more about Network Calls. How to work with an array of users?

  • @LazarofShalev
    @LazarofShalev Рік тому

    thank you sean!

  • @bosch5053
    @bosch5053 Рік тому +1

    Helpful!

  • @AbdeliOS
    @AbdeliOS Рік тому +1

    This is a gold one!

    • @seanallen
      @seanallen  Рік тому +1

      Glad you enjoyed it, Abdel!

  • @edwardhasted3849
    @edwardhasted3849 Рік тому +2

    Many thanks for tackling the real world example that is the most likely bottle neck for Async Await in a workable entirety.

  • @cylenunemaker2444
    @cylenunemaker2444 2 місяці тому +1

    fantastic video

  • @AvantjerTube
    @AvantjerTube Рік тому +1

    Thanks, Sean 🙏

  • @CostyaT
    @CostyaT Рік тому +1

    My first time watching your videos with 29 comments. Thanks for new video 😅.

  • @MegaOlzo
    @MegaOlzo 11 місяців тому +1

    Thank you!

  • @user-uo9nx4tz8u
    @user-uo9nx4tz8u Рік тому +1

    The best of the best .....:)

    • @seanallen
      @seanallen  Рік тому

      Thanks for the kind words :)

  • @viniciushenriquepellegrini3534
    @viniciushenriquepellegrini3534 7 місяців тому +1

    thank you !

  • @alexsalinas2733
    @alexsalinas2733 6 місяців тому +1

    Amazing content, was able to follow along and replicate the code. Would greatly appreciate it if there is a follow up video regarding oauth API calls to fetch and refresh access token, thanks again for the great content.

  • @ssaai
    @ssaai 9 місяців тому

    please make video about access and refresh token, Where to save in the iOS, how to call the api

  • @JRHague
    @JRHague Рік тому

    I would like to see how people typically handle tests with network calls. Would there be test to see if the api your calling has changed and would you make a dummy service that mimics the api for your own tests?

  • @user-si4bh4dl4r
    @user-si4bh4dl4r 4 місяці тому +1

    cool video)

    • @seanallen
      @seanallen  4 місяці тому

      Glad you liked it!

  • @Moch117
    @Moch117 3 місяці тому

    Hey Sean, where do you make the API calls ? It seems like a lot of tutorials online show it in the view model but others say to make the call in the Model layer..
    This is for MVVM architecture

  • @muhammednadeemma3553
    @muhammednadeemma3553 9 місяців тому

    Hi Sean, Out of subject can we create an xcframework with third-party dependencies like Alamofire, lottie-ios etc..

  • @alinusari2033
    @alinusari2033 2 місяці тому

    Thanks so much. It worked, but How can I edited for an array json data?

  • @NathanBudd
    @NathanBudd 6 місяців тому +1

    This is a very well explained video.
    Do you have one that includes auth and how to save tokens using SwiftUI? Either in a video, or one of your courses?

    • @seanallen
      @seanallen  6 місяців тому

      Thanks. Glad you liked it! I don't have a video or course that covers saving tokens, unfortunately.

  • @andreim7808
    @andreim7808 Рік тому

    Hi Sean! What are your thoughts on iOS development vs QA automation? Any ideas what the future holds for iOS developers for 2024 job market is US considering all the recent massive layoffs in the big tech? Trying to make a decision here, as I think QA market is over saturated at this point... Thanks!

  • @omerklbkr
    @omerklbkr Рік тому

    I was waiting for the second API call with Array json. Do u gonna make it ?

  • @killua3939
    @killua3939 Рік тому

    Been unemployed for 2 months and been actively looking for Swift / React native jobs but the demand is so small this year because of the no-to-low code app builders. its really frustrating pursuing ios beyond.

  • @victorriurean
    @victorriurean Рік тому +2

    🔥

  • @BeyzilMusics
    @BeyzilMusics 9 місяців тому +1

    My brain hurts, thanks for the video!

  • @DivyanshArya-bc2dx
    @DivyanshArya-bc2dx 2 місяці тому

    As an flutter developer swift is amazing

  • @4bctrujillog1112
    @4bctrujillog1112 Рік тому +1

    Hi! I really like your videos! You are my iOS university so thanks. Could you make a video about the same thing but using Alamofire? Thank you.

    • @seanallen
      @seanallen  Рік тому

      Glad you like my content! I haven't used Alamofire since 2016. Unfortunately, I'm not familiar with it anymore.

    • @4bctrujillog1112
      @4bctrujillog1112 Рік тому +1

      @@seanallen don’t worry Sean! Thank you so much for responding to me. Never stop please, I’m from Spain but you are literally the best teacher.

  • @soheilpakgohar6630
    @soheilpakgohar6630 3 місяці тому

    awesome

    • @seanallen
      @seanallen  3 місяці тому

      Glad it was helpful!

  • @Olden2610
    @Olden2610 Рік тому

    Hey Sean, great video as always!
    As an experienced developer wanna ask you - do you think it's okay to use classic Result in closure way of network request instead of async/await in new UIKit based apps?
    I tried to use async/aways and it didn't feel much better, still need to wrap this code in Task, call it with and expect response asynchronously. What do you think?
    Due to i currently making new app from scratch, and still feeling unease cause i use UIKit :D
    And not using async/await is making me feel like those ObjC devs i was looking at when i started iOS 5 years ago

    • @seanallen
      @seanallen  Рік тому

      My opinion is that it's perfectly fine to use the classic closure based way of doing network calls. Async/Await has its benefits and is more "modern". But at the end of the day... as long as your code works and the codebase is easy to read and work in.. you're all good as far as I'm concerned.

  • @thomasfrank8379
    @thomasfrank8379 10 місяців тому

    Do you know how to get the redirect url when getting status code 301? I need to add query items to the new url. Thanks :)

  • @nexj4904
    @nexj4904 5 місяців тому

    What is the best way to handle null responses from the server? my bio was null and it failed to parse the data.

  • @mirzausama7458
    @mirzausama7458 Рік тому +2

    Great sir please make more video on api using uikit

  • @mkb18577
    @mkb18577 4 місяці тому

    I tried to recreate this exactly as done in the video to test myself. I can only get it to show the placeholders. It won't pull up any user info? Any ideas why?

  • @ccstreams7706
    @ccstreams7706 6 місяців тому

    i get so many errors in swift since it works so differantly from other programming languages. what is the best method to debug? for example when a FOREACH accepts arrays and i where to print that 0..

  • @airysm
    @airysm Рік тому

    Could async await be used with uikit too?

  • @moku7787
    @moku7787 4 місяці тому

    hi i want to ask something, i already have APIService, model data and already call API like image, text to UI but i need to call link from API with hyperlink text. like example a text (Read more) clickable but the link from API, how to call it?

  • @gjermundification
    @gjermundification Рік тому +1

    Maybe I'll start playing with async await now.

    • @seanallen
      @seanallen  Рік тому

      Let me know what you think!

  • @AvantjerTube
    @AvantjerTube Рік тому

    FYI... When I tried using my own account url, ending in "avantjer", the app was showing only placeholders for all three fields (but no errors).
    I discovered that I had no "bio" on GitHub. ("bio": null)
    Once I added a bio to my GitHub account, everything worked fine.
    Interesting that I wasn't at least shown my login, and avatar, even though I had no bio.
    Maybe Codable "silently" failed because "bio" was null?

    • @AvantjerTube
      @AvantjerTube Рік тому

      The code can be updated to handle cases in which the user has no bio.
      Just make "bio" an optional
      struct GitHubUser: Codable {
      let login: String
      let avatarUrl: String
      let bio: String?
      }

  • @fattoofittoo7907
    @fattoofittoo7907 9 місяців тому

    how to fix "Function declares an opaque return type, but has no return statements in its body from which to infer an underlying type" i create exactly like video but got this one error, already google it cant really found the solution.

  • @DarylMarcano
    @DarylMarcano 4 місяці тому

    how can i use this for an array of users?

  • @shippoboston7974
    @shippoboston7974 11 місяців тому

    Literally I watch your videos the past week and realized I wasn't subscribed. Shame on me!

  • @danyjamous5986
    @danyjamous5986 4 місяці тому

    Does someone know why is try not in a docatch block at : 12:09

  • @hamzahimi1681
    @hamzahimi1681 Рік тому

    is there any difference between doing in swift ui or core swift

    • @seanallen
      @seanallen  Рік тому

      SwiftUI is a user interface framework. Swift is a programming language.

  • @DavidShaw-dc4re
    @DavidShaw-dc4re 11 місяців тому

    It fails for me and I get invalid data error code., but a return of 1385 bytes My raw data has twostraws's data response, i can read clearly but my code fails
    EDIT: The problem was my struct GitHubUser had avatar_url rather than avatarUrl

  • @vanylaskywalker
    @vanylaskywalker Рік тому

    How would you persist the downloaded JSON data using SwiftData?

    • @seanallen
      @seanallen  Рік тому

      I haven't worked with SwiftData yet, but this is a larger topic that would require another video. Can't be answered in a comment.

  • @vazzhh
    @vazzhh 11 місяців тому

    its ok. I understand. but i try to use a JSON from Woocommerce. ( products, price, description ) but not working. i dont know whats the problem

    • @eldarkulic4834
      @eldarkulic4834 Місяць тому

      maybe it's what he mentioned on the start (auth)?
      did you solved your problem?

  • @HussainAli-bf5vw
    @HussainAli-bf5vw Місяць тому

    I want the same tutorial for swift not SwiftUI 🥺

    • @Jan-fn7yd
      @Jan-fn7yd Місяць тому

      All the network logic is plain swift.

  • @rahuljamba5846
    @rahuljamba5846 Рік тому +1

    Thanks for sharing

  • @thematrix2829
    @thematrix2829 9 місяців тому +1

    Good work

  • @joshuascorca550
    @joshuascorca550 10 місяців тому

    Hi Sean, Thanks for the video!
    Question: Why don't you need to wrap this line in a "do catch" ?
    let (data, response) = try await URLSession.shared.data(from:url)

  • @TheWealthyIdiots
    @TheWealthyIdiots Рік тому

    Great video! How would you handle passing request states like “loading” back to the UI? This variable will change a couple of times during the process so you couldn’t just pass it back as a return from your request, so just curious how you would handle it.

    • @TheWealthyIdiots
      @TheWealthyIdiots Рік тому

      I guess you could just add it before and after the await call 🤔. That would work I think.

    • @seanallen
      @seanallen  Рік тому +1

      Yeah, that's how it's typically handled. I usually use something like an "isLoading" boolean that gets flipped to true right before the API call, and then flipped back to false when the API call returns.

    • @TheWealthyIdiots
      @TheWealthyIdiots Рік тому +1

      @@seanallen I ended up doing this and adding a activity progress view to my button. It came out great! Thank you!

  • @samha1513
    @samha1513 Рік тому

    What is the difference between the URLSession.shared.data() vs URLSession.shared.dataTask()?

    • @bijayaadhikari442
      @bijayaadhikari442 Рік тому +2

      with data(), you will get data and response in tuple or individual if need, whereas with .dataTask you will get data, response and error to be handled and return is task which need to be resume like task.resume().

    • @seanallen
      @seanallen  Рік тому +1

      It's also the difference between the old closure based way of doing network calls and using Swift Concurrency and async await.

    • @samha1513
      @samha1513 Рік тому

      So if you have more complex json and more customization use dataTask() ?

  • @alextoma402
    @alextoma402 9 місяців тому

    I was so shook when you just wrote this code out and basically didn't explain any of it:
    guard let response = response as? HTTPURLResponse, response.statusCode == 200 else {
    throw GHError.invalidResponse
    }

  • @ianfrye8988
    @ianfrye8988 8 місяців тому

    Me writing a network call in python in 3 lines 🫣