Now that you have your helper built, you need to actually fetch the list of starships from a film. Add the following inside your extension:. Build and run, then tap any film. Neat, right? The app is starting to look pretty solid. For the search to work, you need a list of the starships that match the search criteria. To accomplish this, you need to send the search criteria to the endpoint for getting starships.
The swapi. To do this, you use a key of search with the search criteria as the value. But before you dive into that, you need to set up a new model called Starships so that you can decode the response just like you do with the other responses.
Create a new Swift file in the Networking group. Name it Starships. Next, open MainTableViewController. This code gets the text typed into the search bar and calls the new searchStarships for: method you just implemented. When the user cancels a search, you want to redisplay the list of films. Add the following property at the top of the class to cache the list of films:. Next, add the following code after the guard statement in fetchFilms :.
Here, you remove any search text entered, hide the keyboard using resignFirstResponder and reload the table view, which causes it to show films again. Search for wing. If you tap one of the ships, the list of films that ship appears in is empty. This is easy to fix thanks to all the work you did before.
Right now, it only knows how to fetch the list associated with a film. You need to fetch the list for a starship. Add the following just before the default: label in the switch statement:. Search for a starship. Select it. You can download the completed project using the Download Materials button at the top or bottom of this article. You learned that Alamofire can make networking calls with very little setup and how to make basic calls using the request function by sending just the URL string.
You learned how to use request chaining and request validation, how to convert the response into JSON and how to convert the response data into a custom data model. This article covered the very basics. I hope you enjoyed this tutorial. Please share any comments or questions about this article in the forum discussion below! The raywenderlich. Get a weekly digest of our tutorials and courses, and receive a free in-depth email course as a bonus! Introducing unlimited access to all video courses, all books, and our new monthly live professional development series!
He has over 20 years of software Jayven is an Apple scholar who contributes his best work to passion, fitness training, and nutrition. Passion makes problem For simplicity we will use an image bundled with our application, the tree. Usually, no server will let you upload an image without some form of authentication.
In order to be able to use this local server, we need to add an exception for our localhost. In Xcode, right click on Info.
Copy the next lines just before the last closing dict tag:. Be sure that you have write rights on your target server, otherwise the upload will fail.
Here is the response from the server, the jsonResponse variable from the Swift code, as seen in the output window of Xcode:. Swift Alamofire tutorial uploading and downloading images Posted on May 2, by Paul This is a short tutorial about uploading and downloading images with Alamofire from an iOS, Swift 3, application. Open a Terminal and install CocoaPods : 1 sudo gem install cocoapods The above command will use the outdated Ruby 2. Now, execute the next command in Terminal: 1 pod install From now on, you will use AlamofireExample.
Chose a descriptive name, something like downloadImage and press Connect : Import the Alamofire module in ViewController. Add the following method to the ViewController extension below upload image:progress:completion: :. Next, go back to upload image:progress:completion: and replace the call to the completion handler in the success condition with the following:. Build and run your project; select a photo and you should see something similar to the following appear:.
Finally, go back to upload image:progress:completion: and replace the call to downloadTags contentID: in the success condition with the following:. Build and run your project again; this time, you should see the returned color tags when you select the Colors button:.
This uses the RGB colors you mapped to PhotoColor structs to change the background color of the view. You've now successfully uploaded an image to Imagga and fetched data from two different endpoints.
You've come a long way, but there's some room for improvement in how you're using Alamofire in PhotoTagger. You probably noticed some repeated code in PhotoTagger. Similarly, if your authorization token changed you'd be updating it all over the place. Alamofire provides a simple method to eliminate this code duplication and provide centralized configuration.
The technique involves creating a struct conforming to URLRequestConvertible and updating your upload and request calls. Click Next , name the file ImaggaRouter. Go back to ViewController.
Next replace the call for Alamofire. Finally, update the call to Alamofire. Build and run for the final time; everything should function just as before, which means you've refactored everything without breaking your app. However, you don't have to go through your entire source code if anything on the Imagga integration ever changes: APIs, your authorization token, parameters, etc.
Awesome job! You can download the completed version of the project using the Download Materials button at the top or bottom of this tutorial. Don't forget to replace your authorization token as appropriate! This tutorial covered the very basics. The raywenderlich. Get a weekly digest of our tutorials and courses, and receive a free in-depth email course as a bonus!
Introducing unlimited access to all video courses, all books, and our new monthly live professional development series!
Ron is the iOS lead at Wisdo, a platform for experience-based social networking. He's been developing iOS for the past 6 years, Scott is an iOS developer at Lyft and an avid open source contributor. Final Pass Editor for raywenderlich.
0コメント