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

若如初见. 提交于 2019-12-08 07:53:30

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/53775848/ios-12-urlsession-breaks-when-http-request-hits-and-suddenly-app-enters-in-backg

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!