Abhishek Thapliyal
Abhishek Thapliyal

Reputation: 3708

iOS 12 URLSession breaks when HTTP request hits and suddenly app enters in background

For all iOS Version < 12, it is working fine. I'm testing(iOS ~> 12.x) in my app using basic URLSession and also tried with Alamofire.

Test Steps:

1. Hit Any HTTP/API Call.

2. Tap on home button immediately.

On coming after sometime it comes

2018-12-14 13:43:46.968901+0530 NewReader[15364:4847228] Task <519A3F27-90DA-439F-8711-B07EFA62E823>.<1> load failed with error Error Domain=NSPOSIXErrorDomain Code=53 "Software caused connection abort" UserInfo={_NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <519A3F27-90DA-439F-8711-B07EFA62E823>.<1>, _kCFStreamErrorDomainKey=1, _NSURLErrorRelatedURLSessionTaskErrorKey=(
    "LocalDataTask <519A3F27-90DA-439F-8711-B07EFA62E823>.<1>"
), _kCFStreamErrorCodeKey=53} [53]

I don't know how other app manages this issue. Also raised issue here

Let me know fixes or workaround.

P.S.: I already tried with Dispatch or Delay it's not working.

Upvotes: 2

Views: 5355

Answers (1)

Scriptable
Scriptable

Reputation: 19750

You need to configure URLSession with background capability. This has always been the case but previously when going into the background it continued to work for a short time (usually upto 3 minutes). It may have gotten more strict in the latest update, or your request takes longer than the time available.

Firstly you need to able background modes in the capabilities tab.

Here is some sample code to show how to setup the URLSession ready for background

private lazy var bgSession: URLSession = {
    let config = URLSessionConfiguration.background(withIdentifier: Constant.sessionID.rawValue)
    //config.isDiscretionary = true
    config.sessionSendsLaunchEvents = true
    return URLSession(configuration: config, delegate: self, delegateQueue: nil)
}()

Code sample and further information available from the following article

Upvotes: 3

Related Questions