@@ -307,14 +307,41 @@ extension AdaWebHost: WKNavigationDelegate, WKUIDelegate {
307
307
decisionHandler ( . allow)
308
308
}
309
309
}
310
+
311
+ // Download the file from the given url and store it locally in the app's temp folder and present the activity viewer.
312
+ private func downloadBlobURL( url downloadUrl : URL , fileName: String ) {
313
+ let localFileURL = FileManager . default. temporaryDirectory. appendingPathComponent ( fileName)
314
+
315
+ URLSession . shared. dataTask ( with: downloadUrl) { data, response, err in
316
+ guard let data = data, err == nil else {
317
+ debugPrint ( " Error downloading from url= \( downloadUrl. absoluteString) : \( err. debugDescription) " )
318
+ return
319
+ }
320
+ if let httpResponse = response as? HTTPURLResponse {
321
+ debugPrint ( " HTTP Status= \( httpResponse. statusCode) " )
322
+ }
323
+ // write the downloaded data to a temporary folder
324
+ do {
325
+ try data. write ( to: localFileURL, options: . atomic) // atomic option overwrites it if needed
326
+ DispatchQueue . main. async { [ self ] in
327
+ // present activity viewer
328
+ let items = [ localFileURL]
329
+ let ac = UIActivityViewController ( activityItems: items, applicationActivities: nil )
330
+ findViewController ( from: self . webView!) ? . present ( ac, animated: true )
331
+ }
332
+ } catch {
333
+ debugPrint ( error)
334
+ return
335
+ }
336
+ } . resume ( )
337
+ }
310
338
}
311
339
312
340
extension AdaWebHost : WKScriptMessageHandler {
313
341
/// When the webview loads up, it'll pass back a message to here.
314
342
/// Fire our initialize methods when that happens.
315
343
public func userContentController( _ userContentController: WKUserContentController , didReceive message: WKScriptMessage ) {
316
344
let messageName = message. name
317
-
318
345
if messageName == " embedReady " {
319
346
self . webHostLoaded = true
320
347
} else if let zdChatterAuthCallback = self . zdChatterAuthCallback, messageName == " zdChatterAuthCallbackHandler " {
@@ -324,7 +351,14 @@ extension AdaWebHost: WKScriptMessageHandler {
324
351
} else if messageName == " eventCallbackHandler " {
325
352
if let event = message. body as? [ String : Any ] {
326
353
if let eventName = event [ " event_name " ] as? String {
327
- if let specificCallback = self . eventCallbacks ? [ eventName] {
354
+ if eventName == " adaDownloadTranscript " {
355
+ if let urlstr = event [ " url " ] as? String , let name = event [ " name " ] as? String {
356
+ if let url = URL ( string: urlstr. replacingOccurrences ( of: " " , with: " " ) ) {
357
+ downloadBlobURL ( url: url, fileName: name)
358
+ }
359
+ }
360
+ }
361
+ else if let specificCallback = self . eventCallbacks ? [ eventName] {
328
362
specificCallback ( event)
329
363
}
330
364
}
0 commit comments