I was trying implement a code where a API will be called shortly after a file has been uploaded into AWS server but it must be in background mode. Whereas the AWS sdk manages uploading file into their server in background mode but the following code is not working.
ViewController.swift
func upload(_ mediaData:Data){
//AWS method to upload a file
AWSS3UploadImageData(mediaData!, strImageName: "person.jpg", strContentType: "img/*", { (isSuccess, result, strMessage) in
if isSuccess {
let arrPost = result as! [[String : String]]
//Call custom webservice
VaultUploadWebService.shared.callVaultUploadWebService(Params: arrPost)
}
else {
print("Unsuccess")
}
})
}
VaultWebService.swift
class VaultUploadWebService: NSObject {
static let shared = VaultUploadWebService()
var savedCompletionHandler: (() -> Void)?
func callVaultUploadWebService(Params: [[String : String]]) {
startRequest(for: "www.example.com", param: Params)
}
func startRequest (for urlString: String, param: [[String : String]]) {
let identifier = "com.com.background" + "\(NSDate().timeIntervalSince1970 * 1000)"
let configuration = URLSessionConfiguration.background(withIdentifier:identifier)
let session = URLSession(configuration: configuration, delegate: self, delegateQueue: nil)
let url = URL(string: urlString)!
var request = URLRequest(url: url, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 180)
request.httpMethod = "post"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
do {
let paramsData = try JSONSerialization.data(withJSONObject:param, options:[])
request.httpBody = paramsData
session.uploadTask(withStreamedRequest: request).resume()
}catch {
print("JSON serialization failed: ", error)
return
}
//Also tried using the following but no luck
/*guard let documentDirectoryUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return }
let fileUrl = documentDirectoryUrl.appendingPathComponent("Persons.json")
let jsonEncoder = JSONEncoder()
do {
let jsonData = try jsonEncoder.encode(param)
try jsonData.write(to: fileUrl, options: [])
}
catch let error {
print(error.localizedDescription)
}
session.uploadTask(with: request, fromFile: fileUrl).resume()*/
}
}
extension VaultUploadWebService: URLSessionDelegate {
func urlSessionDidFinishEvents(forBackgroundURLSession session: URLSession) {
DispatchQueue.main.async {
self.savedCompletionHandler?()
self.savedCompletionHandler = nil
}
}
}
extension VaultUploadWebService: URLSessionTaskDelegate{
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
if (error != nil){
print(error?.localizedDescription ?? "error")
}
else{
print(task.response)
}
}
}
And last.. Appdelegate.swift
func application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) {
let id = identifier as NSString
if id.contains("com.amazonaws") {
AWSS3TransferUtility.interceptApplication(application, handleEventsForBackgroundURLSession: identifier, completionHandler: completionHandler)
}else{
VaultUploadWebService.shared.savedCompletionHandler = completionHandler
}
}
But this delegate method never gets called whereas its being called for AWS upload. I think that's the main reason why background uploadTask
not working for me. Stuck for 2 days. Any help will be appreciated.