Created
January 8, 2020 02:15
-
-
Save Gandum2077/d21baeaab3edda8bd5a148948e6c15aa to your computer and use it in GitHub Desktop.
Download file in JSBox using Objective-C APIs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $define({ | |
| type: "DownloadEventHandler: NSObject<NSURLSessionDownloadDelegate>", | |
| props: ["handlers"], | |
| events: { | |
| "URLSession:downloadTask:didFinishDownloadingToURL:": (session, task, location) => { | |
| const data = $objc("NSData").$dataWithContentsOfURL(location).$copy(); | |
| const filename = task.$response().$suggestedFilename(); | |
| const result = { | |
| "url": task.$originalRequest().$URL().$absoluteString().jsValue(), | |
| "data": data.jsValue(), | |
| "status": task.$error() == false, | |
| "response": task.$response().jsValue(), | |
| "error": task.$error().jsValue(), | |
| }; | |
| self.$handlers().finished(result); | |
| }, | |
| "URLSession:downloadTask:didWriteData:totalBytesWritten:totalBytesExpectedToWrite:": (session, task, bytes, written, expected) => { | |
| const progress = written / expected; | |
| self.$handlers().downloading(progress); | |
| }, | |
| "URLSession:task:didCompleteWithError:": (session, task, error) => { | |
| self.$handlers().failed(error ? error.jsValue() : null); | |
| } | |
| } | |
| }); | |
| function download(params) { | |
| params = params || {}; | |
| params.handlers = params.handlers || {}; | |
| const url = params.url; | |
| const method = params.method || "GET"; | |
| const timeout = params.timeout || 60; | |
| const header = params.header || {}; | |
| const body = params.body; | |
| const handlers = params.handlers; | |
| const request = $objc("NSMutableURLRequest").$requestWithURL($objc("NSURL").$URLWithString(url)); | |
| request.$setHTTPMethod(method); | |
| request.$setTimeoutInterval(timeout); | |
| request.$setHTTPShouldUsePipelining(true); | |
| for (const [key, value] of Object.entries(header)) { | |
| request.$addValue_forHTTPHeaderField(value, key); | |
| } | |
| if (body) { | |
| request.$setHTTPBody(body.ocValue()); | |
| } | |
| const config = $objc("NSURLSessionConfiguration").$defaultSessionConfiguration(); | |
| const delegate = $objc("DownloadEventHandler").$new(); | |
| $objc_retain(delegate); | |
| delegate.$setHandlers({ | |
| downloading: progress => { | |
| if (handlers.downloading) { | |
| handlers.downloading(progress); | |
| } | |
| }, | |
| finished: result => { | |
| if (handlers.finished) { | |
| handlers.finished(result); | |
| } | |
| $objc_release(delegate); | |
| }, | |
| failed: error => { | |
| if (handlers.failed && error) { | |
| handlers.failed(error); | |
| } | |
| $objc_release(delegate); | |
| } | |
| }); | |
| const queue = $objc("NSOperationQueue").$mainQueue(); | |
| const session = $objc("NSURLSession").$sessionWithConfiguration_delegate_delegateQueue(config, delegate, queue); | |
| session.$setHTTPMaximumConnectionsPerHost(500); | |
| const task = session.$downloadTaskWithRequest(request); | |
| task.$resume(); | |
| } | |
| // exports.download = download; | |
| download({ | |
| "url": "https://www.apple.com/v/iphone/home/z/images/overview/the_ultimate_iphone_large_2x.jpg", | |
| "method": "GET", // Optional, default to GET | |
| "timeout": 60, // Optional, default to 60 | |
| "header": {}, // Optional, default to {} | |
| "body": null, // Optional, default to null | |
| "handlers": { | |
| downloading: progress => { | |
| console.log(`Downloading: ${progress}`); | |
| }, | |
| finished: result => { | |
| console.log(`Finished: ${JSON.stringify(result)}`); | |
| // props: url, data, status, response, error | |
| const data = result.data; | |
| if (data) { | |
| $share.sheet(data); | |
| } | |
| }, | |
| failed: error => { | |
| console.log(`Failed: ${error}`); | |
| } | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment