0

When I try to submit a download URL by using a background session created in this way:

func backgroundSession() -> NSURLSession?{
    var session:NSURLSession?;
    var configuration:NSURLSessionConfiguration!;
    if NSURLSessionConfiguration.respondsToSelector(Selector("backgroundSessionConfigurationWithIdentifier:")){
        configuration = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier("myBackgroundSessionIdentifier\(++counter)")
    }
    else{
        configuration = NSURLSessionConfiguration.backgroundSessionConfiguration("myBackgroundSessionIdentifier\(++counter)")
    }
    configuration.allowsCellularAccess = true;
    configuration.timeoutIntervalForRequest = 20.0;
    configuration.timeoutIntervalForResource = 40.0;
    configuration.HTTPMaximumConnectionsPerHost = 1;
    configuration.networkServiceType = .NetworkServiceTypeBackground;
    configuration.discretionary = false;
    let queue=NSOperationQueue.mainQueue()
    session = NSURLSession(configuration:configuration, delegate:self, delegateQueue:queue)
    return session;
}

The app crashes with:

CFNetwork SSLHandshake failed (-9810)

when I submit the relative task with:

let url=NSURL(fileURLWithPath: escapedUrlString!);
let task = backgroundSession()?.downloadTaskWithURL(url!, completionHandler: {[weak self](data, reponse, error) in
        //UIApplication.sharedApplication().applicationIconBadgeNumber = 3;
    })
task!.resume()

While if I do the same by using a standard session created with:

 NSURLSession.sharedSession()

the connection goes without problems, but of course stops being submitted when the app becomes inactive.

What might be the problem at the base of the error?

Fabrizio Bartolomucci
  • 4,948
  • 8
  • 43
  • 75
  • which ios sdk you are using ios9? – Özgür Ersil Jun 18 '15 at 12:38
  • Yes, an iPad 2 with iOS 9; now I tried executing it on my iOS 7 iPhone 4 and it crashes without saying anything. Unfortunately I do not own a iOS 8 device. – Fabrizio Bartolomucci Jun 18 '15 at 12:58
  • I guess it is a local WIFI security problem, try it again on another network (3G is the quickest solution) – Özgür Ersil Jun 18 '15 at 13:00
  • I tried executing it on my iOS 7 iPhone 4 and it crashes without saying anything. When executed on the simulator, it crashes the second time it is called. Unfortunately I do not own a iOS 8 device. Now I am trying executing it disabling wifi. – Fabrizio Bartolomucci Jun 18 '15 at 13:05
  • And "luckily" - having an app only working on 3G would be impossible - it also crashes on the 3G network. – Fabrizio Bartolomucci Jun 18 '15 at 13:08
  • i thought [that](http://stackoverflow.com/questions/19922717/how-to-handle-cfnetwork-sslhandshake-failed-in-ios) could be your problem. – Özgür Ersil Jun 18 '15 at 13:10
  • That solution is for objective-c, and the code worked for me when implemented in that language. This is a new issue emerged with Swift. – Fabrizio Bartolomucci Jun 18 '15 at 13:12
  • Also I have the same problem both in http and https while, if I use a standard session, both work. – Fabrizio Bartolomucci Jun 18 '15 at 13:15
  • Yet, the crash report for the iPhone was enlightening: *** Terminating app due to uncaught exception 'NSGenericException', reason: 'Completion handler blocks are not supported in background sessions. Use a delegate instead.' So it look as if the two problems are distinct. – Fabrizio Bartolomucci Jun 18 '15 at 13:17
  • And in fact, once remove the closure, the iPhone installation does not crash any longer. Now I try again on the iPad. – Fabrizio Bartolomucci Jun 18 '15 at 13:25
  • Now I am getting: Invalid URL scheme for background downloads: file. Valid schemes are http or https. Apparently the way yo create a url is not: NSURL(fileURLWithPath:), porting software to Swift seems a game of magic. Before I enter also in this issue, could you tell me how to create a url that remains a url.... – Fabrizio Bartolomucci Jun 18 '15 at 13:58
  • Just for a change, when I run the code on the simulator it hangs at: public func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error:NSError){ println("url session error \(error.localizedDescription) \(error.localizedRecoveryOptions)"); } like there were a breakpoint. – Fabrizio Bartolomucci Jun 18 '15 at 14:26

1 Answers1

-2

I lost the reference to this thread, but the problem got resolved in some way.

Fabrizio Bartolomucci
  • 4,948
  • 8
  • 43
  • 75
  • The problem got resolved in some way isn't really a valid answer – Matt Rees Oct 07 '15 at 11:05
  • In fact it was not even solved: I keep handing the url in the Today Notifications by means of background sessions, they work single shot. The first time it is called, it is regularly executed, the next ones it is not. – Fabrizio Bartolomucci Oct 08 '15 at 09:49