ios - Background Add will not be working in Swift
von Satoshi Nakamoto

I used to be making an attempt implement a code the place a API will likely be referred to as shortly after a file has been uploaded into AWS server nevertheless it should be in background mode. Whereas the AWS sdk manages importing file into their server in background mode however the next code will not be working.
ViewController.swift
func add(_ mediaData:Knowledge)
//AWS methodology to add a file
AWSS3UploadImageData(mediaData!, strImageName: "individual.jpg", strContentType: "img/*", (isSuccess, outcome, strMessage) in
if isSuccess
let arrPost = outcome as! ]
//Name customized webservice
VaultUploadWebService.shared.callVaultUploadWebService(Params: arrPost)
else
print("Unsuccess")
)
VaultWebService.swift
class VaultUploadWebService: NSObject
static let shared = VaultUploadWebService()
var savedCompletionHandler: (() -> Void)?
func callVaultUploadWebService(Params: ])
startRequest(for: "www.instance.com", param: Params)
func startRequest (for urlString: String, param: ])
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 = "put up"
request.setValue("utility/json", forHTTPHeaderField: "Content material-Kind")
request.httpBody = json(from: param)?.information(utilizing: .utf8)
session.uploadTask(withStreamedRequest: request).resume()
//Additionally tried utilizing the next however no luck
/*guard let documentDirectoryUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else return
let fileUrl = documentDirectoryUrl.appendingPathComponent("Individuals.json")
let jsonEncoder = JSONEncoder()
do
let jsonData = attempt jsonEncoder.encode(param)
attempt jsonData.write(to: fileUrl, choices: )
catch let error
print(error.localizedDescription)
session.uploadTask(with: request, fromFile: fileUrl).resume()*/
extension VaultUploadWebService: URLSessionDelegate
func urlSessionDidFinishEvents(forBackgroundURLSession session: URLSession)
DispatchQueue.important.async
self.savedCompletionHandler?()
self.savedCompletionHandler = nil
extension VaultUploadWebService: URLSessionTaskDelegate
func urlSession(_ session: URLSession, activity: URLSessionTask, didCompleteWithError error: Error?)
if (error != nil)
print(error?.localizedDescription ?? "error")
else
print(activity.response)
And final.. Appdelegate.swift
func utility(_ utility: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void)
let id = identifier as NSString
if id.accommodates("com.amazonaws")
AWSS3TransferUtility.interceptApplication(utility, handleEventsForBackgroundURLSession: identifier, completionHandler: completionHandler)
else
VaultUploadWebService.shared.savedCompletionHandler = completionHandler
However this delegate methodology by no means will get referred to as whereas its being referred to as for AWS add. I feel that is the principle motive why background uploadTask not working for me. Caught for two days. Any assist will likely be appreciated.
Source link
Read the full article
Satoshi Nakamoto
Keine Verbindung
Verbindung wird wiederhergestellt
Etwas ist schiefgelaufen
Wir sind gleich wieder da