Cannot get didReceiveData to be called when using NSURLSession
For delegate pattern with NSURLSession, I think you should create NSURLSession by something like this:
[NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]
delegate:self
delegateQueue:[NSOperationQueue mainQueue]];
But not:
[NSURLSession sharedSession];
Also I noticed your NSURLSessionDataDelegate, NSURLSessionDelegate, NSURLSessionTaskDelegate go with UIViewController; however, your NSURLSession and delegate methods are implemented in FileUpload.m. Try to edit this line:
@interface FileUpload : NSObject <RCTBridgeModule>
to:
@interface FileUpload : NSObject <RCTBridgeModule, NSURLSessionDataDelegate, NSURLSessionDelegate, NSURLSessionTaskDelegate>
Then create NSURLSessionDataTask without completion handler:
NSURLSessionDataTask *task = [session dataTaskWithRequest:req];
And see if there is any difference.
NSURLSessionDataDelegate not called
class ViewController: UIViewController, NSURLSessionDelegate, NSURLSessionDataDelegate, NSURLSessionTaskDelegate {
var session:NSURLSession!
override func viewDidLoad() {
super.viewDidLoad()
let requestUrl = "https://www.google.com"
let request = NSURLRequest(URL: NSURL(string: requestUrl)!)
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
self.session = NSURLSession(
configuration: configuration,
delegate: self,
delegateQueue: NSOperationQueue.mainQueue()
)
let task = session.dataTaskWithRequest(request)
task.resume()
}
func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveData data: NSData) {
print("Did receive data!")
}
func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveResponse response: NSURLResponse, completionHandler: (NSURLSessionResponseDisposition) -> Void) {
print("Response!!")
}
}
NSURLSessionDataTask delegate methods are not called
Use the method
+ (NSURLSession *)sessionWithConfiguration:(NSURLSessionConfiguration *)configuration
delegate:(id<NSURLSessionDelegate>)delegate
delegateQueue:(NSOperationQueue *)queue
to set the delegate properly.
Update:
And you have to call the completion handler of didReceiveResponse
completionHandler
A completion handler that your code calls to continue the transfer,
passing a constant to indicate whether the transfer should continue as
a data task or should become a download task.
• If you passNSURLSessionResponseAllow
, the task continues normally.
• If you passNSURLSessionResponseCancel
, the task is canceled.
• If you passNSURLSessionResponseBecomeDownload
as the disposition, your delegate’sURLSession:dataTask:didBecomeDownloadTask:
method is called to provide you with the new download task that supersedes the current task.
For example
- (void) URLSession:(NSURLSession *)session
dataTask:(NSURLSessionDataTask *)dataTask
didReceiveResponse:(NSURLResponse *)response
completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler
{
completionHandler(NSURLSessionResponseAllow);
}
NSURLSesssionDelegate NSURLSessionDataDelegate functions not triggering with dataTask
After quite some research, I found the problem and thought, I will post an answer in case anybody has the same problem.
Take a look at this function in the code:
func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveResponse response: NSURLResponse, completionHandler (NSURLSessionResponseDisposition) -> Void) {
self.startTime = NSDate()
self.endTime = self.startTime
}
A look in the corresponding Apple Doc gives the solution.
You have to call the completion handler like this to continue the transfer:
completionHandler(NSURLSessionResponseDisposition.Allow)
Related Topics
How to Use Key-Value Observing with Smart Keypaths in Swift 4
How to Load the Photo Library into Uicollectionview? Swift
Draw Mkpointannotation with Title in Mksnapshot Image
Closure Use of Non-Escaping Parameter - Swift 3 Issue
Create CSV File in Swift and Write to File
Swiftui, Shadow Only for Container
Swift Packages and Conflicting Dependencies
Building for Arm64E on Apple Silicon
How to Add Custom Init for String Extension
Swiftui MACos Commands (Menu Bar) and View
How to Convert Text to Speech for Osx in Swift Playground
iOS 12 Errors: Appears to Be from a Different Nsmanagedobjectmodel Than This Context'S
How to Cast an @Binding in Swift
Xcode Swift. How to Programmatically Select Cell in View-Based Nstableview
Hide Status Bar in Launch Screen
Firebase Retrieve Image from Url Save with Firebase Database
In What Situation Would One Use Expectationfornotification in Swift Testing