I'm having strange problem with NSURLSessionDelegate. Here's my code first:
class NetworkHandler: NSObject,NSURLSessionDelegate,NSURLSessionDownloadDelegate {
lazy var downloadsSession: NSURLSession = {
let configuration = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier("abc")
let session = NSURLSession(configuration: configuration, delegate: self, delegateQueue: nil)
return session
}()
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL) {
print("finished")
}
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
print("\(String(format: "%.1f%% of %@", progress * 100, totalSize))" )
}
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didResumeAtOffset fileOffset: Int64, expectedTotalBytes: Int64) {
print("didResumeAtOffset: \(fileOffset)")
}
func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) {
print("didCompleteWithError error=\(error)");
}
func URLSessionDidFinishEventsForBackgroundURLSession(session: NSURLSession) {
if let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate {
if let completionHandler = appDelegate.backgroundSessionCompletionHandler {
appDelegate.backgroundSessionCompletionHandler = nil
dispatch_async(dispatch_get_main_queue(), {
completionHandler()
})
}
}
}
}
Here's my output when my file is ~20MB and I'm on 4G/LTE:
didCompleteWithError error=Optional(Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo={NSLocalizedDescription=unsupported URL})
I've already checked my url has http and its a valid url. This code works 100% of the time when I'm on wifi or when file size is <~10MB or NOT iOS10. The file downloads till about 60% and then fail everytime. I'm just out of ideas to even justify myself what could go wrong.
The problem happens when it's iOS10 File size ~20MB 4G/LTE ONLY If I change any of the above 3 criteria, it works.
I tried Apple code and the problem is same