@@ -47,7 +47,7 @@ - (id)uploadTask:(id)args
4747 NSString *method = nil ;
4848 NSURL *fileURL = nil ;
4949 NSDictionary *headers = nil ;
50- id data = [args objectForKey: @" data" ];;
50+ id data = [args objectForKey: @" data" ];
5151
5252 ENSURE_ARG_FOR_KEY (url, args, @" url" , NSString );
5353 ENSURE_ARG_OR_NIL_FOR_KEY (method, args, @" method" , NSString );
@@ -75,36 +75,89 @@ - (id)uploadTask:(id)args
7575 } else if ([data isMemberOfClass: [TiBlob class ]]) {
7676 task = [_session uploadTaskWithRequest: request fromData: [data data ]];
7777 } else {
78- NSLog (@" [ERROR] Ti.URLSession: The specified data for background upload task is incorrect. Please provide a file path or a blob." );
78+ NSLog (@" [ERROR] Ti.URLSession: The specified data for upload task is incorrect. Please provide a file path or a blob." );
7979 return [NSNull null ];
8080 }
8181
8282 [task resume ];
8383
8484 return NUMINTEGER ([task taskIdentifier ]);
8585 } else {
86- NSLog (@" [ERROR] Ti.URLSession: The specified URL for this background upload task is empty. Please provide a valid URL." );
86+ NSLog (@" [ERROR] Ti.URLSession: The specified URL for this upload task is empty. Please provide a valid URL." );
8787 }
8888
8989 return nil ;
9090}
9191
9292- (id )downloadTask : (id )args
9393{
94- ENSURE_SINGLE_ARG (args, NSDictionary );
94+ ENSURE_SINGLE_ARG (args, NSDictionary );
95+
96+ NSString *url = [TiUtils stringValue: @" url" properties: args];
97+
98+ if ([url length ]) {
99+ NSURLSessionDownloadTask *task = [_session downloadTaskWithURL: [NSURL URLWithString: url]];
100+ [task resume ];
95101
96- NSString *url = [TiUtils stringValue: @" url" properties: args];
102+ return NUMINTEGER ([task taskIdentifier ]);
103+ } else {
104+ NSLog (@" [ERROR] Ti.URLSession: The specified url for download task is empty. Please provide a proper url." );
105+ }
106+
107+ return nil ;
108+ }
109+
110+
111+ - (id )dataTask : (id )args
112+ {
113+ ENSURE_SINGLE_ARG (args, NSDictionary );
114+
115+ NSString *url = nil ;
116+ NSString *method = nil ;
117+ NSDictionary *headers = nil ;
118+ id data = [args objectForKey: @" data" ];
119+
120+ ENSURE_ARG_FOR_KEY (url, args, @" url" , NSString );
121+ ENSURE_ARG_OR_NIL_FOR_KEY (method, args, @" method" , NSString );
122+ ENSURE_ARG_OR_NIL_FOR_KEY (headers, args, @" requestHeaders" , NSDictionary );
123+
124+ NSURL *nativeURL = [TiUtils toURL: url proxy: self ];
125+
126+ if (nativeURL == nil ) {
127+ NSLog (@" [ERROR] Ti.URLSession: The specified URL for this data task is empty. Please provide a valid URL." );
128+ return nil ;
129+ }
130+
131+ NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: nativeURL];
132+
133+ // HTTP method
134+ [request setHTTPMethod: (method ?: @" POST" )];
135+
136+ // Optional request headers
137+ if (headers) {
138+ for (id key in headers) {
139+ ENSURE_TYPE (key, NSString );
140+ ENSURE_TYPE ([headers objectForKey: key], NSString );
141+
142+ [request setValue: [headers objectForKey: key] forHTTPHeaderField: key];
143+ }
144+ }
145+
146+ if (data != nil ) {
147+ NSError *error = nil ;
148+ NSData *postData = [NSJSONSerialization dataWithJSONObject: data options: 0 error: &error];
97149
98- if ([url length ]) {
99- NSURLSessionDownloadTask *task = [_session downloadTaskWithURL: [NSURL URLWithString: url]];
100- [task resume ];
101-
102- return NUMINTEGER ([task taskIdentifier ]);
150+ if (error == nil ) {
151+ [request setHTTPBody: postData];
103152 } else {
104- NSLog (@" [ERROR] Ti.URLSession: The specified url for background download task is empty. Please provide a proper url. " );
153+ DebugLog (@" [ERROR] Could not append data: %@ " , error. localizedDescription );
105154 }
106-
107- return nil ;
155+ }
156+
157+ NSURLSessionDataTask *task = [_session dataTaskWithRequest: request];
158+ [task resume ];
159+
160+ return NUMINTEGER ([task taskIdentifier ]);
108161}
109162
110163- (void )finishTasksAndInvalidate : (id )unused
0 commit comments