Skip to content

Commit bbe791a

Browse files
committed
Expose dataTask API
1 parent f5117d4 commit bbe791a

File tree

2 files changed

+65
-11
lines changed

2 files changed

+65
-11
lines changed

ios/Classes/ComAppceleratorUrlSessionSessionProxy.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
- (id)downloadTask:(id)args;
2727

28+
- (id)dataTask:(id)args;
29+
2830
- (void)finishTasksAndInvalidate:(id)unused;
2931

3032
- (void)invalidateAndCancel:(id)unused;

ios/Classes/ComAppceleratorUrlSessionSessionProxy.m

Lines changed: 63 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,36 +75,88 @@ - (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+
NSURLSessionUploadTask *task = nil;
116+
NSString *url = nil;
117+
NSString *method = nil;
118+
NSDictionary *headers = nil;
119+
id data = [args objectForKey:@"data"];;
120+
121+
ENSURE_ARG_FOR_KEY(url, args, @"url", NSString);
122+
ENSURE_ARG_OR_NIL_FOR_KEY(method, args, @"method", NSString);
123+
ENSURE_ARG_OR_NIL_FOR_KEY(headers, args, @"requestHeaders", NSDictionary);
124+
125+
if ([url length] != 0) {
126+
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[TiUtils toURL:url proxy:self]];
97127

98-
if ([url length]) {
99-
NSURLSessionDownloadTask *task = [_session downloadTaskWithURL:[NSURL URLWithString:url]];
100-
[task resume];
128+
// HTTP method
129+
[request setHTTPMethod:(method ?: @"POST")];
130+
131+
// Optional request headers
132+
if (headers) {
133+
for (id key in headers) {
134+
ENSURE_TYPE(key, NSString);
135+
ENSURE_TYPE([headers objectForKey:key], NSString);
101136

102-
return NUMINTEGER([task taskIdentifier]);
103-
} else {
104-
NSLog(@"[ERROR] Ti.URLSession: The specified url for background download task is empty. Please provide a proper url.");
137+
[request setValue:[headers objectForKey:key] forHTTPHeaderField:key];
138+
}
105139
}
106140

107-
return nil;
141+
if (data != nil) {
142+
NSError *error = nil;
143+
NSData *postData = [NSJSONSerialization dataWithJSONObject:data options:0 error:&error];
144+
145+
if (error == nil) {
146+
[request setHTTPBody:postData];
147+
} else {
148+
DebugLog(@"[ERROR] Could not append data: %@", error.localizedDescription);
149+
}
150+
}
151+
152+
NSURLSessionDataTask *task = [_session dataTaskWithRequest:request];
153+
154+
return NUMINTEGER([task taskIdentifier]);
155+
} else {
156+
NSLog(@"[ERROR] Ti.URLSession: The specified URL for this data task is empty. Please provide a valid URL.");
157+
}
158+
159+
return nil;
108160
}
109161

110162
- (void)finishTasksAndInvalidate:(id)unused

0 commit comments

Comments
 (0)