ios - Cannot get didReceiveData to be called when using NSURLSession -
i'm taking code react native module react-native-file-upload. i'm trying update using nsurlconnection (which deprecated) use nsurlsession , report progress of upload.
i able code switched on nsurlconnection nsurlsession not difficultly i'm struggling getting didreceivedata delegate called. why isn't didreceivedata delegate being called?
and here modified fileupload.m
#import <foundation/foundation.h> #import <mobilecoreservices/mobilecoreservices.h> #import <uikit/uikit.h> #import <photos/photos.h> #import "rctbridgemodule.h" #import "rctlog.h" @interface fileupload : nsobject <rctbridgemodule, nsurlsessiondatadelegate, nsurlsessiondelegate, nsurlsessiontaskdelegate> @end @implementation fileupload rct_export_module(); rct_export_method(upload:(nsdictionary *)obj callback:(rctresponsesenderblock)callback) { nsstring *uploadurl = obj[@"uploadurl"]; nsdictionary *headers = obj[@"headers"]; nsdictionary *fields = obj[@"fields"]; nsarray *files = obj[@"files"]; nsstring *method = obj[@"method"]; if ([method isequaltostring:@"post"] || [method isequaltostring:@"put"]) { } else { method = @"post"; } nsurl *url = [nsurl urlwithstring:uploadurl]; nsmutableurlrequest *req = [nsmutableurlrequest requestwithurl:url]; [req sethttpmethod:method]; // set headers nsstring *formboundarystring = [self generateboundarystring]; nsstring *contenttype = [nsstring stringwithformat:@"multipart/form-data; boundary=%@", formboundarystring]; [req setvalue:contenttype forhttpheaderfield:@"content-type"]; (nsstring *key in headers) { id val = [headers objectforkey:key]; if ([val respondstoselector:@selector(stringvalue)]) { val = [val stringvalue]; } if (![val iskindofclass:[nsstring class]]) { continue; } [req setvalue:val forhttpheaderfield:key]; } nsdata *formboundarydata = [[nsstring stringwithformat:@"--%@\r\n", formboundarystring] datausingencoding:nsutf8stringencoding]; nsmutabledata* reqbody = [nsmutabledata data]; // add fields (nsstring *key in fields) { id val = [fields objectforkey:key]; if ([val respondstoselector:@selector(stringvalue)]) { val = [val stringvalue]; } if (![val iskindofclass:[nsstring class]]) { continue; } [reqbody appenddata:formboundarydata]; [reqbody appenddata:[[nsstring stringwithformat:@"content-disposition: form-data; name=\"%@\"\r\n\r\n", key] datausingencoding:nsutf8stringencoding]]; [reqbody appenddata:[val datausingencoding:nsutf8stringencoding]]; [reqbody appenddata:[@"\r\n" datausingencoding:nsutf8stringencoding]]; } // add files (nsdictionary *file in files) { nsstring *name = file[@"name"]; nsstring *filename = file[@"filename"]; nsstring *filepath = file[@"filepath"]; nsstring *filetype = file[@"filetype"]; nsdata *filedata = nil; nslog(@"filepath: %@", filepath); if ([filepath hasprefix:@"assets-library:"]) { nsurl *asseturl = [[nsurl alloc] initwithstring:filepath]; __block nsdata * tempdata = nil; phfetchresult *result = [phasset fetchassetswithalasseturls:@[asseturl] options:nil]; phasset *asset = result.firstobject; if (asset) { phcachingimagemanager *imagemanager = [[phcachingimagemanager alloc] init]; // request image asset phcachingimagemanager. [imagemanager requestimageforasset:asset targetsize:cgsizemake(100.0f, 100.0f) contentmode:phimagecontentmodeaspectfill options:nil resulthandler:^(uiimage *image, nsdictionary *info) { nslog(@"image: %@", image); tempdata = uiimagepngrepresentation(image); }]; } filedata = tempdata; } else if ([filepath hasprefix:@"data:"] || [filepath hasprefix:@"file:"]) { nsurl *fileurl = [[nsurl alloc] initwithstring:filepath]; filedata = [nsdata datawithcontentsofurl: fileurl]; } else { filedata = [nsdata datawithcontentsoffile:filepath]; } [reqbody appenddata:formboundarydata]; [reqbody appenddata:[[nsstring stringwithformat:@"content-disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", name.length ? name : filename, filename] datausingencoding:nsutf8stringencoding]]; if (filetype) { [reqbody appenddata:[[nsstring stringwithformat:@"content-type: %@\r\n", filetype] datausingencoding:nsutf8stringencoding]]; } else { [reqbody appenddata:[[nsstring stringwithformat:@"content-type: %@\r\n", [self mimetypeforpath:filename]] datausingencoding:nsutf8stringencoding]]; } [reqbody appenddata:[[nsstring stringwithformat:@"content-length: %ld\r\n\r\n", (long)[filedata length]] datausingencoding:nsutf8stringencoding]]; [reqbody appenddata:filedata]; [reqbody appenddata:[@"\r\n" datausingencoding:nsutf8stringencoding]]; } // add end boundary nsdata* end = [[nsstring stringwithformat:@"--%@--\r\n", formboundarystring] datausingencoding:nsutf8stringencoding]; [reqbody appenddata:end]; // send request [req sethttpbody:reqbody]; nsurlsessionconfiguration *sessionconfiguration = [nsurlsessionconfiguration defaultsessionconfiguration]; nsurlsession *session = [nsurlsession sessionwithconfiguration:sessionconfiguration delegate:(id)self delegatequeue:[nsoperationqueue mainqueue]]; nsurlsessiondatatask *task = [session datataskwithrequest:req completionhandler:^(nsdata *data, nsurlresponse *response, nserror *error) { nshttpurlresponse *httpresponse = (nshttpurlresponse *) response; nslog(@"response status code: %ld", (long)[httpresponse statuscode]); callback(@[[nsnull null], [nsstring stringwithformat:@"response status code: %ld", (long)[httpresponse statuscode]]]); }]; [task resume]; } - (nsstring *)generateboundarystring { nsstring *uuid = [[nsuuid uuid] uuidstring]; return [nsstring stringwithformat:@"----%@", uuid]; } - (nsstring *)mimetypeforpath:(nsstring *)filepath { nsstring *fileextension = [filepath pathextension]; nsstring *uti = (__bridge_transfer nsstring *)uttypecreatepreferredidentifierfortag(kuttagclassfilenameextension, (__bridge cfstringref)fileextension, null); nsstring *contenttype = (__bridge_transfer nsstring *)uttypecopypreferredtagwithclass((__bridge cfstringref)uti, kuttagclassmimetype); if (contenttype) { return contenttype; } return @"application/octet-stream"; } - (void)urlsession:(nsurlsession *)session datatask:(nsurlsessiondatatask *)datatask didreceiveresponse:(nsurlresponse *)response completionhandler:(void (^)(nsurlsessionresponsedisposition disposition))completionhandler { completionhandler(nsurlsessionresponseallow); nslog(@"didreceiveresponse"); } - (void)urlsession:(nsurlsession *)session datatask:(nsurlsessiondatatask *)datatask didreceivedata:(nsdata *)data { nslog(@"didreceivedata"); } @end
for delegate pattern nsurlsession, think should create nsurlsession this:
[nsurlsession sessionwithconfiguration:[nsurlsessionconfiguration defaultsessionconfiguration] delegate:self delegatequeue:[nsoperationqueue mainqueue]];
but not:
[nsurlsession sharedsession];
also noticed nsurlsessiondatadelegate, nsurlsessiondelegate, nsurlsessiontaskdelegate go uiviewcontroller; however, nsurlsession , delegate methods implemented in fileupload.m. try edit 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 difference.
Comments
Post a Comment