0

I implemented a way to upload a file in a share extension with the wkwebview and a javascript bridge. The bridge handels the upload and if a part was successfully uploaded it start the next part of the file until the files is completly uploaded. This works perfectly but I want to do this task in the background so the user wont have to wait inside the share extension windows during the upload. How can I achieve a background upload like this?

Javascript Bridge Swift Side:

func userContentController(userContentController: WKUserContentController, didReceiveScriptMessage message: WKScriptMessage) {
    if let messageBody:NSDictionary = message.body as? NSDictionary {
        let key:String = messageBody["key"] as String
        switch key {
        case "startUpload":
            fileUploader!.fileProductInstanceId = messageBody["fileProductInstanceId"] as? Int
            fileUploader!.contextId = messageBody["contextId"] as? Int
            fileUploader!.directoryId  = messageBody["directoryId"] as? Int
            fileUploader!.initUpload()
        case "getNextFilePart":
            fileUploader!.sendData()
        case "fileUploadFinished":
            self.myWebView!.evaluateJavaScript("App.UploadNextFile(\(fileUploader!.contextId!),\(fileUploader!.directoryId!))",
                completionHandler: nil)
        default:
            println("unknown command")
        }
    }

}
zanzoken
  • 787
  • 4
  • 12
  • 18

1 Answers1

0

You can dispatch the uploading work to background by using GCD. I suggest you use the XWebView. It can do all dirty works for you, such as bridging and background dispatching.

soflare
  • 751
  • 5
  • 16