Skip to content

Commit 8c6da98

Browse files
authored
Tbozhikov/fix response code ios (#222)
* fix: read native properties in a new way after ios runtime v5.3 updates * chore: update demos to show responseCode output * chore: add nsconfig.json files to demos for {N} 5.4 compatibility
1 parent 905b23a commit 8c6da98

File tree

7 files changed

+52
-23
lines changed

7 files changed

+52
-23
lines changed

demo-angular/app/home/home.component.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ export class HomeComponent {
8989
error: e.error ? e.error.toString() : e.error,
9090
currentBytes: e.currentBytes,
9191
totalBytes: e.totalBytes,
92-
body: e.data
92+
body: e.data,
93+
responseCode: e.responseCode
9394
})
9495
});
9596
}

demo-angular/nsconfig.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"useLegacyWorkflow": false
3+
}

demo-vue/app/components/Home.vue

+2-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ export default {
114114
error: e.error ? e.error.toString() : e.error,
115115
currentBytes: e.currentBytes,
116116
totalBytes: e.totalBytes,
117-
body: e.data
117+
body: e.data,
118+
responseCode: e.responseCode
118119
})
119120
});
120121

demo-vue/nsconfig.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"useLegacyWorkflow": false
3+
}

demo/app/home/home-view-model.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ export class HomeViewModel extends Observable {
8989
error: e.error ? e.error.toString() : e.error,
9090
currentBytes: e.currentBytes,
9191
totalBytes: e.totalBytes,
92-
body: e.data
92+
body: e.data,
93+
responseCode: e.responseCode
9394
})
9495
});
9596
}

demo/nsconfig.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"useLegacyWorkflow": false
3+
}

src/background-http.ios.ts

+37-20
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function onError(session, nsTask, error) {
2525
const fileManager = utils.ios.getter(NSFileManager, NSFileManager.defaultManager);
2626
fileManager.removeItemAtPathError(task._fileToCleanup);
2727
}
28-
let response = nsTask && nsTask.response ? <NSHTTPURLResponse>nsTask.response : null;
28+
const response = nsTask && <NSHTTPURLResponse>nsTask.performSelector("response");
2929
if (error) {
3030
task.notifyPropertyChange("status", task.status);
3131
task.notify(<common.ErrorEventData>{
@@ -61,20 +61,15 @@ class BackgroundUploadDelegate extends NSObject implements NSURLSessionDelegate,
6161

6262
// NSURLSessionDelegate
6363
URLSessionDidBecomeInvalidWithError(session, error) {
64-
// console.log("URLSessionDidBecomeInvalidWithError:");
65-
// console.log(" - session: " + session);
66-
// console.log(" - error: " + error);
6764
}
6865

6966
URLSessionDidReceiveChallengeCompletionHandler(session, challenge, comlpetionHandler) {
70-
// console.log("URLSessionDidFinishEventsForBackgroundURLSession: " + session + " " + challenge);
7167
const disposition = null;
7268
const credential = null;
7369
comlpetionHandler(disposition, credential);
7470
}
7571

7672
URLSessionDidFinishEventsForBackgroundURLSession(session) {
77-
// console.log("URLSessionDidFinishEventsForBackgroundURLSession: " + session);
7873
}
7974

8075
// NSURLSessionTaskDelegate
@@ -85,7 +80,6 @@ class BackgroundUploadDelegate extends NSObject implements NSURLSessionDelegate,
8580
}
8681

8782
URLSessionTaskDidReceiveChallengeCompletionHandler(session, task, challenge, completionHandler) {
88-
// console.log("URLSessionTaskDidReceiveChallengeCompletionHandler: " + session + " " + task + " " + challenge);
8983
const disposition = null;
9084
const credential = null;
9185
completionHandler(disposition, credential);
@@ -98,28 +92,23 @@ class BackgroundUploadDelegate extends NSObject implements NSURLSessionDelegate,
9892
}
9993

10094
URLSessionTaskNeedNewBodyStream(session, task, need) {
101-
// console.log("URLSessionTaskNeedNewBodyStream");
10295
}
10396

10497
URLSessionTaskWillPerformHTTPRedirectionNewRequestCompletionHandler(session, task, redirect, request, completionHandler) {
105-
// console.log("URLSessionTaskWillPerformHTTPRedirectionNewRequestCompletionHandler");
10698
completionHandler(request);
10799
}
108100

109101
// NSURLSessionDataDelegate
110102
URLSessionDataTaskDidReceiveResponseCompletionHandler(session, dataTask, response, completionHandler) {
111-
// console.log("URLSessionDataTaskDidReceiveResponseCompletionHandler");
112103
const disposition = null;
113104
completionHandler(disposition);
114105
}
115106

116107
URLSessionDataTaskDidBecomeDownloadTask(session, dataTask, downloadTask) {
117-
// console.log("URLSessionDataTaskDidBecomeDownloadTask");
118108
}
119109

120110
URLSessionDataTaskDidReceiveData(session: NSURLSession, dataTask: NSURLSessionDataTask, data: NSData) {
121111
dispatch_async(main_queue, () => {
122-
// console.log("URLSessionDataTaskDidReceiveData");
123112
// we have a response in the data...
124113
const jsTask = Task.getTask(session, dataTask);
125114
const jsonString = NSString.alloc().initWithDataEncoding(data, NSUTF8StringEncoding);
@@ -134,20 +123,16 @@ class BackgroundUploadDelegate extends NSObject implements NSURLSessionDelegate,
134123
}
135124

136125
URLSessionDataTaskWillCacheResponseCompletionHandler() {
137-
// console.log("URLSessionDataTaskWillCacheResponseCompletionHandler");
138126
}
139127

140128
// NSURLSessionDownloadDelegate
141129
URLSessionDownloadTaskDidResumeAtOffsetExpectedTotalBytes(session, task, offset, expects) {
142-
// console.log("URLSessionDownloadTaskDidResumeAtOffsetExpectedTotalBytes");
143130
}
144131

145132
URLSessionDownloadTaskDidWriteDataTotalBytesWrittenTotalBytesExpectedToWrite(session, task, data, written, expected) {
146-
// console.log("URLSessionDownloadTaskDidWriteDataTotalBytesWrittenTotalBytesExpectedToWrite");
147133
}
148134

149135
URLSessionDownloadTaskDidFinishDownloadingToURL(session, task, url) {
150-
// console.log("URLSessionDownloadTaskDidFinishDownloadingToURL");
151136
}
152137
}
153138

@@ -247,8 +232,38 @@ class Session implements common.Session {
247232
}
248233
}
249234

235+
class NativePropertyReader {
236+
private _invocationCache = new Map<string, NSInvocation>();
237+
238+
private getInvocationObject(object: NSObject, selector: string): NSInvocation {
239+
let invocation = this._invocationCache.get(selector);
240+
if (!invocation) {
241+
const sig = object.methodSignatureForSelector(selector);
242+
invocation = NSInvocation.invocationWithMethodSignature(sig);
243+
invocation.selector = selector;
244+
245+
this._invocationCache[selector] = invocation;
246+
}
247+
248+
return invocation;
249+
}
250+
251+
public readProp<T>(object: NSObject, prop: string, type: interop.Type<T>): T {
252+
const invocation = this.getInvocationObject(object, prop);
253+
invocation.invokeWithTarget(object);
254+
255+
const ret = new interop.Reference<T>(type, new interop.Pointer());
256+
invocation.getReturnValue(ret);
257+
258+
return ret.value;
259+
}
260+
}
261+
250262
class Task extends Observable {
251263
public static _tasks = new Map<NSURLSessionTask, Task>();
264+
public static tasksReader = new NativePropertyReader();
265+
private static is64BitArchitecture = interop.sizeof(interop.types.id) === 8;
266+
public static NSIntegerType = Task.is64BitArchitecture ? interop.types.int64 : interop.types.int32;
252267

253268
public _fileToCleanup: string;
254269
private _task: NSURLSessionTask;
@@ -269,18 +284,19 @@ class Task extends Observable {
269284
}
270285

271286
get upload(): number {
272-
return this._task.countOfBytesSent;
287+
return Task.tasksReader.readProp(this._task, "countOfBytesSent", interop.types.int64);
273288
}
274289

275290
get totalUpload(): number {
276-
return this._task.countOfBytesExpectedToSend;
291+
return Task.tasksReader.readProp(this._task, "countOfBytesExpectedToSend", interop.types.int64);
277292
}
278293

279294
get status(): string {
280-
if (this._task.error) {
295+
if (Task.tasksReader.readProp(this._task, "error", Task.NSIntegerType)) {
281296
return "error";
282297
}
283-
switch (this._task.state) {
298+
// NSURLSessionTaskState : NSInteger, so we should pass number format here
299+
switch (Task.tasksReader.readProp(this._task, "state", Task.NSIntegerType) as NSURLSessionTaskState) {
284300
case NSURLSessionTaskState.Running: return "uploading";
285301
case NSURLSessionTaskState.Completed: return "complete";
286302
case NSURLSessionTaskState.Canceling: return "error";
@@ -299,6 +315,7 @@ class Task extends Observable {
299315

300316
return task;
301317
}
318+
302319
public cancel(): void {
303320
this._task.cancel();
304321
}

0 commit comments

Comments
 (0)