作者:mobiledu2502926247 | 来源:互联网 | 2023-05-17 07:37
ImhavingstrangeproblemwithNSURLSessionDelegate.Heresmycodefirst:我对NSURLSessionDelegate有一
I'm having strange problem with NSURLSessionDelegate. Here's my code first:
我对NSURLSessionDelegate有一些奇怪的问题。这是我的代码:
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:
这是我的输出,当我的文件大约是20MB并且我在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.
我已经检查过我的网址有http和它的有效网址。当我在wifi上或文件大小<~10MB或不是iOS10时,此代码可以100%正常工作。文件下载到大约60%,然后每次都失败。我只是出于想法,甚至为自己辩解可能出错的地方。
The problem happens when it's iOS10 File size ~20MB 4G/LTE ONLY If I change any of the above 3 criteria, it works.
问题发生在它的iOS10文件大小~20MB 4G / LTE ONLY如果我改变上述3个标准中的任何一个,它就有效。
I tried Apple code and the problem is same
我试过Apple代码,问题是一样的
1 个解决方案