Features of Alamofire:
Alamofire is a swift open source library for implementing web services in IOS and Mac Os.
Handling HTTP request-response by serially and concurrently way. We can easily request for the POST and GET method and managing request-response independently.
It also performs some Basic functionality like uploading and downloading the file from the server.
You can install POD as well or can push files into the project and implement it.
It’s a Developer friendly library. Fully developed in SWIFT. Alamofire delivers some additional features of serial request/response function. Parameter encoding is available with URL / JSON / Plist.
We can upload MultipartFormData, Data, Stream. Alamofire provides HTTP Response Validation. Indicates parameters regarding upload and download Progress. It automatically retries Requests.
There are some additional component libraries like AlamofireImage for catching images and priority-based downloading images. Another one is AlamofireNetworkActivityIndicator which manages the visibility of activity indicators.
Implement Almofire:
We can install Alamofire using cocoa pods and Carthage.here is how we can Install using cocoa pods.
source ‘https://github.com/CocoaPods/Specs.git’ platform :ios, ‘10.0’
1 2 3 4 |
use_frameworks! target '<Your Target Name>' do pod 'Alamofire', '~> 4.7' end |
use_frameworks! target ‘<Your Target Name>’ do pod ‘Alamofire’, ‘~> 4.7’ end
Request For POST Method: Inputs:
1) URL -:Web server url. example: www.wayside.com/Api/login/
2) Parameter :- parameter is request authentication String. Example: [“login”:”1″,”email”:”bandalrahul@yahoo.com”,”password”:”password”]
Output: json=Response in string format. dicJson=Response in Dictionary format.
Code:
1 2 3 4 5 6 7 8 |
Alamofire.request(url, method: .post, parameters: ["login":"1","email":"bandalrahul@yahoo.com","password":"password"], encoding: URLEncoding.httpBody).responseJSON { response in if let json = response.result.value as? [String: Any] { let dicJson = json as NSDictionary print(dicJson) let name=dicJson.value(forKey:"name") var str:NSString str=name as! NSString print(str) } } |
Happy coding. Please comment and share this article if you find something useful.