How to check internet connection in alamofire?

For swift 5 and Alamofire 5.4.4 ,I created a swift class called Connectivity . Use NetworkReachabilityManager class from Alamofire and configure the isConnectedToInternet() method as per your need. import Foundation import Alamofire class Connectivity { class func isConnectedToInternet() -> Bool { return NetworkReachabilityManager()?.isReachable ?? false } } Usage: if Connectivity.isConnectedToInternet() { print(“Yes! internet is available.”) … Read more

AlamoFire asynchronous completionHandler for JSON request

This is a really good question. Your approach is perfectly valid. However, Alamofire can actually help you streamline this even more. Your Example Code Dispatch Queue Breakdown In you example code, you are jumping between the following dispatch queues: NSURLSession dispatch queue TaskDelegate dispatch queue for validation and serializer processing Main dispatch queue for calling … Read more

How to post nested json by SwiftyJson and Alamofire?

try this func test() { var exampleParameters : [String : Any] = [“b” : “bv”] exampleParameters[“a”] = [“a1”: “v1″,”a2”: “v2″] debugPrint(exampleParameters.description) let devUrlPush = URL.init(string:”yourURL”) var request = URLRequest(url: devUrlPush!) request.httpMethod = “POST” request.setValue(“application/json”, forHTTPHeaderField: “Content-Type”) request.httpBody = try! JSONSerialization.data(withJSONObject: exampleParameters) Alamofire.request(request).responseJSON { (response) in if( response.result.isSuccess) { }else { } } let string = … Read more

Checking for multiple asynchronous responses from Alamofire and Swift

Better than that looping process, which would block the thread, you could use dispatch group to keep track of when the requests were done. So “enter” the group before issuing each of the requests, “leave” the group when the request is done, and set up a “notify” block/closure that will be called when all of … Read more

Getting client certificate to work for mutual authentication using Swift 3 and Alamofire 4

I was able to get it to work. A few issues got into the way. First, you have to allow IOS to accept self signed certificates. This requires to set up AlamoFire serverTrustPolicy: let serverTrustPolicies: [String: ServerTrustPolicy] = [ “your-domain.com”: .disableEvaluation ] self.sessionManager = Alamofire.SessionManager( serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies) ) From there, you have to override … Read more

Swift and using class extension

For me it seems completely reasonable since you can use extensions to expose different parts of logic to different extensions. This can also be used to make class conformance to protocols more readable, for instance class ViewController: UIViewController { … } extension ViewController: UITableViewDelegate { … } extension ViewController: UITableViewDataSource { … } extension ViewController: … Read more

Swift variable name with ` (backtick)

According to the Swift documentation : To use a reserved word as an identifier, put a backtick (`)before and after it. For example, class is not a valid identifier, but `class` is valid. The backticks are not considered part of the identifier; `x` and x have the same meaning. In your example, default is a … Read more

Swift Alamofire: How to get the HTTP response status code

For Swift 3.x / Swift 4.0 / Swift 5.0 users with Alamofire >= 4.0 / Alamofire >= 5.0 response.response?.statusCode More verbose example: Alamofire.request(urlString) .responseString { response in print(“Success: \(response.result.isSuccess)”) print(“Response String: \(response.result.value)”) var statusCode = response.response?.statusCode if let error = response.result.error as? AFError { statusCode = error._code // statusCode private switch error { case .invalidURL(let … Read more

Alamofire 4 upload with parameters

Its working fine on my side. I’m using following code: let parameters = [ “file_name”: “swift_file.jpeg” ] Alamofire.upload(multipartFormData: { (multipartFormData) in multipartFormData.append(UIImageJPEGRepresentation(self.photoImageView.image!, 1)!, withName: “photo_path”, fileName: “swift_file.jpeg”, mimeType: “image/jpeg”) for (key, value) in parameters { multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key) } }, to:”http://sample.com/upload_img.php”) { (result) in switch result { case .success(let upload, _, _): upload.uploadProgress(closure: { … Read more