简体   繁体   中英

URLSession datatask return 0 bytes of data in Swift4.2/iOS12

We are accessing the PDF URL from our application and getting the PDF data. After converting our code base Swift2.3 to Swift4.2 we are getting 0 bytes of data as response. But when we open the same URL in the browser it is showing the PDF file. We are not getting any error also. In Swift2.3 code base, we are getting exact data value as valid bytes. Is there anything we need to configure in URLSession for getting the exact data bytes in Swift4.2.

//url - PDF server url value

let session:Foundation.URLSession = Foundation.URLSession(configuration:URLSessionConfiguration.default delegate:self delegateQueue:nil)
let task = session.dataTask(with: url, completionHandler:{(data,response,error) in 
    // here data coming as 0 bytes 
})
task.resume()

URLSession.dataTask sometimes returns with no error but zero bytes. My workaround is to use URLSession.downloadTask instead, and use the contents of localURL to create a Data buffer. Incidentally, creating a String from this buffer using .utf8 encoding fails, while .ascii works fine. I therefore suspect that encoding of the URL contents may be the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM