jdg / MBProgressHUD

MBProgressHUD + Customizations
http://www.bukovinski.com/
MIT License
16.01k stars 3.56k forks source link

Help for use with share extension in swift 3 #483

Closed ooii closed 7 years ago

ooii commented 7 years ago

Hi,

I'm using MBProgressHUD in my app that offers a share extension, which purpose is to upload a photo directly from the photo album to a server for image analysis.

I'd like that upload progress is shown during upload and, once that done, a message is displayed for 2 seconds stating that the image was correctly uploaded.

I use the following code but (1) it displays only a spinner during upload and (2) the final message is shown very briefly, very less than half a second.

What am I doing wrong?

Thanks a lot for your help.

Alamofire.upload(multipartFormData: { multipartFormData in
    multipartFormData.append(jpgImageData!, withName: "file",fileName: fname, mimeType: mime)
    for (key, value) in parameters {
        multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
    }
},
                 to:url!)
{ (result) in
    switch result {
    case .success(let upload, _, _):
        upload.uploadProgress(closure: { (progress) in
            print("Upload Progress: \(progress.fractionCompleted)")
            let progressHUD = MBProgressHUD.showAdded(to: self.view, animated: true)
            DispatchQueue.main.async(execute: {
                progressHUD.progress = (Float(progress.fractionCompleted))
                progressHUD.show(true)
                progressHUD.hide(animated: true, afterDelay: 2)
            })
        })

        upload.responseJSON { response in
            if response.response?.statusCode == 200{
                if let result = response.result.value {

                    DispatchQueue.main.async(execute: {
                        var message = "Success"
                        let progressHUD = MBProgressHUD.showAdded(to: self.view, animated: true)
                        progressHUD.label.text = message
                        progressHUD.show(animated: true)
                        progressHUD.hide(true, afterDelay: 2)
                        self.extensionContext?.completeRequest(returningItems: nil, completionHandler: nil)

                    })
                }
            }
        }
    case .failure(let encodingError):
        print(encodingError)
    }
}
matej commented 7 years ago

You should ask questions like this on stackoverflow.com or similar sites. This is a bug tracker not a support forum.

That being said, two things I see immediately are that you are not setting the mode property and you are not calling showAdded on the main thread. There are probably other issues. Take a look at the example project.

ooii commented 7 years ago

You're absolutely right. I'm sorry. Thanks for your help.