How to Call API in swift using Alamofire

if you first time call API in your App than add it in you Info.plist


<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>


than install pod 'Alamofire'

and import it like:
Code :


import Alamofire




func callAPINoParameters()
    {
        let strURL = yourURL
        
        Alamofire.request(strURL).responseJSON { (response) in
            switch response.result {
            case .success(let JSON):
                
                let response = JSON as! NSDictionary
                print(response)
                break
                
            case .failure(let error):
                print("Request failed with error: \(error)")
                break
            }
        }
    }
    func callAPIWithParameters()
    {
        let strURL = yourURL
        var dic = youDictionary

        print(dic)
        Alamofire.request(strURL, method: .post, parameters: dic as! [String : AnyObject]).responseJSON { (response) in
            switch response.result {
            case .success(let JSON):
                
                let response = JSON as! NSDictionary
                print(response)
                break
                
            case .failure(let error):
                print("Request failed with error: \(error)")
                break
            }
        }
    }


Comments

Popular posts from this blog

Windows Keys

how to display popover in TableView cell in swift

Important extensions while create a new App in swift