Alamofire is a Swift-based HTTP networking library for iOS and Mac OS X. It is very easy and powerful library which is developed completely in swift so no need of bridging with objective-C like AFNewworking.

In this tutorial we are using  Alamofile library with POD. You may use it directly by downloading and importing in project or also you can use carthage.

You can find all useful information along with POD and carthage in this useful link for Alamofire.

https://github.com/Alamofire/Alamofire

Step 1 – Create new project (Single view application)

Screen Shot 2017-06-03 at 12.34.52 AM

Step 2 : Create pod

  1. Open terminal
  2. Go to project folder by command cd (your project path — you can get this by just dragging project folder to terminal)
  3. write Command pod init
  4. In project folder new pod file created.
  5. open pod file and write Alamofire pod
  6. pod 'Alamofire', '~> 4.4'   (you may use latest or preferable version )

    Screen Shot 2017-06-03 at 12.42.16 AM6. Then install pods into your project by typing in terminal: pod install

    Screen Shot 2017-06-03 at 12.35.18 PM.png

  7. Now close your xcode project and open .xcworkspace xcode project file and start coding

In Project

  1. import Alamofire
  2. import Alamofire

2. API call for GET method

  func newFeedGETrequest(){
        
        //---- GET request ---------
        let url: String = "https://newsapi.org/v1/articles?source=cnn&sortBy=top&apiKey=f94a0d53cd3647c985944689b055025d"
        
        Alamofire.request(url, method: .get, parameters: nil, encoding: JSONEncoding.default)
            .responseJSON { response in
                switch response.result{
                case .success( _):
                    let json = response.result.value as! NSDictionary
                    print("serviceForNewsFeed response \(json)")
                    
                case .failure(let error):
                    print("error \(error)")
                }
        }
    }

3. API call for POST method

    func newFeedPOSTrequest(){
        
        //---- POST request ---------
        let parameter =  ["timezone": "Asia/Kolkata", "username": "xyz@gmail.com", "password": "abcd1234"]
        let url: String = "your post api url"
        
        Alamofire.request(url, method: .post, parameters: parameter, encoding: JSONEncoding.default)
            .responseJSON { response in
                switch response.result{
                case .success( _):
                    let json = response.result.value as! NSDictionary
                    print("json \(json)")
                    
                case .failure(let error):
                    print("error \(error)")
                }
        }
    }

githubDownloadButton.png You can download source code

240_F_77959340_hWLiOY93juohUoXqjKzqPB79552nw8XU

Hope you find this blog useful. Please feel free to contact with me in case you have any query, suggestions.  You can comment, like and follow posts.

You can request any topic related to Swift and iOS development.

Donate any small amount you think for this knowledge to grow this forum.                        

To Donate –  paypal.me/SandeshSardar

download

Advertisements