Calling API in Swift 4 with Alamofire 4.0 | Alamofire 4.0

Introduction on Calling API in Swift 4 and with Alamofire 4

Alamofire with its latest update adopted Swift4.0 and swift4 is new update in swift. Calling API (Application Program interface) in mobile is very common and in almost functional apps there is need of calling APIs and receive data from server or post data to server. There are many types HTTP methods while calling APIs you have to mention which one you are required to call and these methods are below

1. GET
2. POST
3. PUT
4. DELETE
5. PATCH

Swift 4

But in most common use, we need only two Methods which are GET, POST in get methods user can make call request through url but cannot send data in HTTPBody while making network request and will receive response from server but whereas in POST method user can make request through url and also send data in HTTPBody and will receive response from server.

Alamofire is third party library which make calling webservice or API in iOS easy and makes your pain less. Some sample of examples of code to call from Alamofire 4.0 in swift 4 language while making get methods call, while making post content on post method and also post content with image or file.

1. ALAMOFIRE 4 GET METHOD CALL API CODE in Swift 4

CODE :-

func callLoginApi_getMethod() -> Void {
Alamofire.request(“Url_here”, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: nil).responseJSON(completionHandler: {
response in
if((response.result.value) != nil){
// print(response.result.value!)
let swiftyJsonVar = JSON(response.result.value!)

if let resData = swiftyJsonVar.dictionaryObject {
if(resData["status"] as! String == "100") {
print(“SUCCESS”)
} else {

}
}
} else {
print("server error")
}
})
}

2. ALAMOFIRE 4.0 POST METHOD WITH ONLY POST CONTENT CALL API CODE IN MULTI FORM DATA in SWIFT 4

CODE :-

func callApi_postMethod() -> Void {

Alamofire.upload(multipartFormData: { multipartFormData in
multipartFormData.append(“value1”.data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName: “key1”)
multipartFormData.append(“value2”.data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName: “key2”)
multipartFormData.append(“value3”.data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName: “key3”)
}, to: “url_here”, encodingCompletion: { result in
print("result = \(result)")
switch result {
case .success(request: let request, streamingFromDisk: false, streamFileURL: nil):
request.responseJSON(completionHandler: { response in
if ((response.result.value) != nil){
print("response1 = \(response)")
} else {
print("server error")
}
})
break

case .failure:
print("failed api calling")
break

case .success( _, true, _):
break
case .success( _, _, .some(_)):
break
}

})
}

3. ALAMOFIRE 4.0 POST METHOD WITH IMAGE FILE UPLOADING API CODE IN MULTI FORM DATA

CODE :-

func callApi_postImageMethod() -> Void {

Alamofire.upload(multipartFormData: { multipartFormData in
if let imageData = UIImagePNGRepresentation( imageLiteral(resourceName: “imageName_here”)) {
multipartFormData.append(imageData, withName: “img123”, fileName: "hello2018.png", mimeType: "image/png")
}
multipartFormData.append(“value1”.data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName: “key1”)
multipartFormData.append(“value2”.data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName: “key2”)

}, to: “url__here”, encodingCompletion: { result in
print("result = \(result)")
switch result {
case .success(request: let request, streamingFromDisk: false, streamFileURL: nil):
request.responseJSON(completionHandler: { response in
if ((response.result.value) != nil){
print("response1 = \(response)")
} else {
print("server error")
}
})
break

case .failure:
print("failed api calling")
break

case .success( _, true, _):
break
case .success( _, _, .some(_)):
break
}

})
}

Leave a Comment