1

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

Hussain Mansoor
  • 2,934
  • 2
  • 27
  • 40

1 Answers1

1

This error typically indicates that the URL scheme is unsupported (i.e. it is something other than http:// https:// or ftp://) or that they depend on an NSURLProtocol subclass that is part of your app (which is not allowed in a background session).

If you're getting that error with a supported URL scheme, unless you're doing something weird like subclassing NSURL or NSURLRequest, the problem is probably not something you can work around. File a bug with Apple and provide a minimal code snippet that reproduces the bug.

And if you are subclassing NSURL or NSURLRequest, just know that these subclasses will not work correctly with NSURLSession—particularly with background sessions.

Edit: Apparently in iOS 10, there are known bugs that cause this error code instead of the expected one whenever the network drops out. The fix is to use the provided resume data to continue the transfer whenever it happens.

dgatwood
  • 10,129
  • 1
  • 28
  • 49