Swift uses Alamofire for network requests

Source: Internet
Author: User

Alamofire is an HTTP network library written in Swift, developed by author Mattt of the former popular open source project Afnetworking, and can be used very simply for asynchronous network communication.

To get the latest version of Alamofire, go to Https://github.com/Alamofire/Alamofire and click the Download ZIP button to the right of the page. Then open the Starter project folder in the Finder, and then drag the alamofire-master folder into your main project folder.

Open the alamofire-master folder (now it's in your project folder) and then put the alamofire.xcodeprij file (note the blue icon!). Not a white icon! Drag directly into Xcode under the Alamofiredemo project, as shown in:

Next, click the Alamofiredemo item and go to the general window. Scroll to the Embedded Binaries Item, and then click the + sign below it. Select alamofire.framework, and finally Click Add to complete the Add.

Build and run your project to make sure that no errors occur, and then you can go to the next section.

To use Alamofire, you first need to import it. You need to add this ' import ' statement to each file that uses the Alamofire class and functions.

Import Alamofire

Alamofire is a powerful, Swift language HTTP Web Development toolkit. Supports various HTTP method, JSON, file upload, file download and multiple authentication methods.

Let's take a look at the specifics.

GET request

" Http://httpbin.org/get ")

Include parameters

alamofire.request (. GET,                  "http://httpbin.org/get",                  parameters: ["  Foo""bar"])
Contains Response processing
alamofire.request (. GET,                  "http://httpbin.org/get",                  parameters: ["foo  ""bar"])         in                     println ( Request)                     println (response)                     println (Error)                   }

POST request

Let parameters = [    "Foo":"Bar",    "Baz": ["a",1],    "Qux": [        "x":1,        "y":2,        "Z":3]] alamofire.request (. POST,"Http://httpbin.org/post", Parameters:parameters)

Download a file

Alamofire.download (. GET,"http://httpbin.org/stream/100", Destination: {(Temporaryurl, response)inch    ifLet Directoryurl =Nsfilemanager.defaultmanager (). Urlsfordirectory (. Documentdirectory, Indomains:. Userdomainmask) [0]                           as?Nsurl {Let pathcomponent=Response.suggestedfilenamereturndirectoryurl.urlbyappendingpathcomponent (pathcomponent)}returnTemporaryurl})

Uploading files

Let FileURL =Nsbundle.mainbundle (). Urlforresource ("Default", Withextension:"PNG") Alamofire.upload (. POST,"Http://httpbin.org/post", File:fileurl)

Download image

" /article/image/20150128/54c85e49bfa8d25764000f47/small_f3338d9cdf932cc3c0464dcb523361c5.jpg " inch As NSData )                           = Image        }

Now you have learned how to use Alamofire to operate the most basic network requests.

However, it is possible for the framework to fail to run on the real machine. Must be calm, please read the next article!!!

Swift uses Alamofire for network requests

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.