1

I am using Alamofire code for uploading youtube video. Everything works fine. But suppose if while uploading, I switch off my net, my uploading stops but does not give me any error in response. I am posting my code below:

alamoFireManager.upload(multipartFormData: { (MultipartFormData) in

            let snippetValueDictionary = requestType.params
            let statusValueDictionary = ["privacyStatus":"private"]

            let videoMetaDictionary = ["snippet":snippetValueDictionary,"status":statusValueDictionary]

            do{
                let json = try JSONSerialization.data(withJSONObject: videoMetaDictionary, options: JSONSerialization.WritingOptions.prettyPrinted)

                MultipartFormData.append(json, withName: "snippet", mimeType: "application/json")

            }
            catch{

            }

            MultipartFormData.append(localVideoURL, withName: "video", fileName: "video.mp4", mimeType: "application/octet-stream")


        }, to: urlYoutube, method: .post, headers: requestType.headers, encodingCompletion: { (result) in

            switch result {

            case .success(let upload, _, _):
                upload.uploadProgress(closure: { progress in

                    print("Upload Progress: \(progress.fractionCompleted)")

                }).validate().responseJSON(completionHandler: { dataResponse in

                    if dataResponse.result.error != nil {
                        print("Error Description: \(dataResponse.result.error?.localizedDescription)")

                    }
                    else {
                        let json = JSON(dataResponse.result.value!)
                        print(json)
                    }
                })

            case .failure(let encodingError):
                print(encodingError)

            }
        })

When I switch off my net, the uploading stops. When I switch it back on, the uploading restarts again and upload progress is called. When the internet is not available while data is being uploaded, I need some error in response so that user is informed about network! Can you please suggest what I may be doing wrong here?

piyushg098
  • 91
  • 1
  • 6
  • Have you tried a break point at the upload.uploadProgress call? just to know if it is being called once you switch off your network. Besides, I will give values to the validate method like: `validate(statusCode: 200..<300)` – Aitor Pagán Apr 06 '17 at 11:29
  • I placed the breakpoint on the uploadProgress call, but it's not called once the network is off! – piyushg098 Apr 06 '17 at 11:31
  • I found this http://stackoverflow.com/a/38850780/6203030 and this http://stackoverflow.com/q/41137536/6203030 I'll keep on looking for a solution meanwhile – Aitor Pagán Apr 06 '17 at 11:38
  • I also found this API from Alamofire https://github.com/Alamofire/AlamofireNetworkActivityIndicator take a look – Aitor Pagán Apr 06 '17 at 11:42

0 Answers0