How to check internet connection in alamofire?

前端 未结 8 1138
醉酒成梦
醉酒成梦 2020-11-30 22:55

I am using below code for making HTTP request in server.Now I want to know whether it is connected to internet or not. Below is my code

  let request = Alam         


        
8条回答
  •  北海茫月
    2020-11-30 23:08

    If Alamofire.upload result returns success then below is the way to check for internet availibility while uploading an image:

    Alamofire.upload(multipartFormData: { multipartFormData in
    
                    for (key,value) in parameters {
                     multipartFormData.append((value).data(using: .utf8)!, withName: key)
                    }
                      multipartFormData.append(self.imageData!, withName: "image" ,fileName: "image.jpg" , mimeType: "image/jpeg")
                }, to:url)
                { (result) in
    
                    switch result{
    
                    case .success(let upload, _, _):
    
                        upload.uploadProgress(closure: { (progress) in
                         print("Upload Progress: \(progress.fractionCompleted)")
    
                        })
    
                        upload.responseJSON { response in
                            if  let statusCode = response.response?.statusCode{
    
                            if(statusCode == 201){
                             //internet available
                              }
                            }else{
                            //internet not available
    
                            }
                        }
    
                    case .failure(let encodingError):
                        print(encodingError)
    
                    }
    
                }
    

提交回复
热议问题