My recent project involved implementing a method to upload files in a share extension utilizing the wkwebview and a javascript bridge. This approach handles the upload process, where each part of the file is uploaded successfully before moving on to the next part until the entire file is uploaded. While this method works flawlessly, I am looking to optimize it by performing the upload task in the background without requiring the user to remain inside the share extension window during the upload process. How can I achieve a background upload like this?
On the Swift side of the Javascript Bridge:
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")
}
}
}