NSURLSessionDataDelegate method didReceiveData and others are not called

♀尐吖头ヾ 提交于 2019-12-04 04:42:25

Implement the completion handler in your delegate method

func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveResponse response: NSURLResponse, completionHandler: (NSURLSessionResponseDisposition) -> Void) {
completionHandler(NSURLSessionResponseDisposition.Allow) //.Cancel,If you want to stop the download
}
pmchallam

I'll confirm ChezhianNeo's comment about calling the didRecieveResponse delegate's completionHandler with NSURLSessionResponseAllow, as shown below

- (void) URLSession:(NSURLSession *)session dataTask:    (NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler {

completionHandler(NSURLSessionResponseAllow);
}

This enabled the didRecieveData delegate method to be called as well.

What also works, at least it did for me, is to simply not implement the didReceiveResponse method in your delegate, but do implement the didReceiveData method - "skipping" the didReceiveResponse method allows didReceiveData method to be called, which doesn't seem to make a whole lot of sense, but it works.

For others :
In my case, the delegate didReceiveData was not called even after I :
1. set the delegate in NSURLSession.
2. implement didReceiveResponse with NSURLSessionResponseAllow

The reason :
Using dataTaskWithRequest with completion handler prevent calling the delegate.
you must use the dataTaskWithRequest without completion to use NSURLSession delegates.

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