@@ -25,6 +25,7 @@ public class AdaWebHost: NSObject {
25
25
public var greeting = " "
26
26
public var webViewTimeout = 30.0
27
27
28
+
28
29
/// Metafields can be passed in during init; use `setMetaFields()` and `setSensitiveMetafields()`
29
30
/// to send values in at runtime
30
31
private var metafields : [ String : Any ] = [ : ]
@@ -88,6 +89,7 @@ public class AdaWebHost: NSObject {
88
89
self . metafields = metafields
89
90
// we always want to append the sdkType
90
91
self . metafields [ " sdkType " ] = " IOS "
92
+ self . metafields [ " sdkSupportsDownloadLink " ] = true
91
93
self . sensitiveMetafields = sensitiveMetafields
92
94
self . openWebLinksInSafari = openWebLinksInSafari
93
95
self . appScheme = appScheme
@@ -340,7 +342,22 @@ extension AdaWebHost {
340
342
}
341
343
}
342
344
343
- extension AdaWebHost : WKNavigationDelegate , WKUIDelegate {
345
+ extension AdaWebHost : WKNavigationDelegate , WKUIDelegate , WKDownloadDelegate {
346
+ @available ( iOS 14 . 5 , * )
347
+ public func download( _ download: WKDownload , decideDestinationUsing response: URLResponse , suggestedFilename: String , completionHandler: @escaping ( URL ? ) -> Void ) {
348
+
349
+ let localFileURL = FileManager . default. temporaryDirectory. appendingPathComponent ( suggestedFilename)
350
+
351
+ completionHandler ( localFileURL)
352
+
353
+ DispatchQueue . main. async { [ self ] in
354
+ // present activity viewer
355
+ let items = [ localFileURL]
356
+ let ac = UIActivityViewController ( activityItems: items, applicationActivities: nil )
357
+ findViewController ( from: self . webView!) ? . present ( ac, animated: true )
358
+ }
359
+ }
360
+
344
361
public func webView( _ webView: WKWebView , didFailProvisionalNavigation navigation: WKNavigation ! , withError error: Error ) {
345
362
/// Whena reset method is built - we will need to set this back to false
346
363
self . hasError = true
@@ -385,7 +402,13 @@ extension AdaWebHost: WKNavigationDelegate, WKUIDelegate {
385
402
386
403
// Used for processing all other navigation
387
404
public func webView( _ webView: WKWebView , decidePolicyFor navigationAction: WKNavigationAction , decisionHandler: @escaping ( WKNavigationActionPolicy ) -> Swift . Void ) {
388
- if navigationAction. navigationType == WKNavigationType . linkActivated {
405
+ if #available( iOS 14 . 5 , * ) , navigationAction. shouldPerformDownload{
406
+ decisionHandler ( . download)
407
+ } else if navigationAction. request. url!. absoluteString. range ( of: " /transcript/ " ) != nil {
408
+ downloadUrl ( url: navigationAction. request. url!, fileName: " chat_transcript.txt " )
409
+ decisionHandler ( . cancel)
410
+ }
411
+ else if navigationAction. navigationType == WKNavigationType . linkActivated {
389
412
if let url = navigationAction. request. url {
390
413
openUrl ( webView: webView, url: url)
391
414
}
@@ -395,34 +418,42 @@ extension AdaWebHost: WKNavigationDelegate, WKUIDelegate {
395
418
decisionHandler ( . allow)
396
419
}
397
420
}
421
+
422
+
423
+ // Download the file from the given url and store it locally in the app's temp folder and present the activity viewer.
424
+ private func downloadUrl( url downloadUrl : URL , fileName: String ) {
425
+ let localFileURL = FileManager . default. temporaryDirectory. appendingPathComponent ( fileName)
398
426
399
- // Download the file from the given url and store it locally in the app's temp folder and present the activity viewer.
400
- private func downloadBlobURL( url downloadUrl : URL , fileName: String ) {
401
- let localFileURL = FileManager . default. temporaryDirectory. appendingPathComponent ( fileName)
427
+ URLSession . shared. dataTask ( with: downloadUrl) { data, response, err in
428
+ guard let data = data, err == nil else {
429
+ debugPrint ( " Error downloading from url= \( downloadUrl. absoluteString) : \( err. debugDescription) " )
430
+ return
431
+ }
432
+ if let httpResponse = response as? HTTPURLResponse {
433
+ debugPrint ( " HTTP Status= \( httpResponse. statusCode) " )
434
+ }
435
+ // write the downloaded data to a temporary folder
436
+ do {
437
+ try data. write ( to: localFileURL, options: . atomic) // atomic option overwrites it if needed
438
+ DispatchQueue . main. async { [ self ] in
439
+ // present activity viewer
440
+ let items = [ localFileURL]
441
+ let ac = UIActivityViewController ( activityItems: items, applicationActivities: nil )
442
+ findViewController ( from: self . webView!) ? . present ( ac, animated: true )
443
+ }
444
+ } catch {
445
+ debugPrint ( error)
446
+ return
447
+ }
448
+ } . resume ( )
449
+ }
450
+
451
+ @available ( iOS 14 . 5 , * )
452
+ public func webView( _ webView: WKWebView , navigationAction: WKNavigationAction , didBecome download: WKDownload ) {
453
+
454
+ download. delegate = self
455
+ }
402
456
403
- URLSession . shared. dataTask ( with: downloadUrl) { data, response, err in
404
- guard let data = data, err == nil else {
405
- debugPrint ( " Error downloading from url= \( downloadUrl. absoluteString) : \( err. debugDescription) " )
406
- return
407
- }
408
- if let httpResponse = response as? HTTPURLResponse {
409
- debugPrint ( " HTTP Status= \( httpResponse. statusCode) " )
410
- }
411
- // write the downloaded data to a temporary folder
412
- do {
413
- try data. write ( to: localFileURL, options: . atomic) // atomic option overwrites it if needed
414
- DispatchQueue . main. async { [ self ] in
415
- // present activity viewer
416
- let items = [ localFileURL]
417
- let ac = UIActivityViewController ( activityItems: items, applicationActivities: nil )
418
- findViewController ( from: self . webView!) ? . present ( ac, animated: true )
419
- }
420
- } catch {
421
- debugPrint ( error)
422
- return
423
- }
424
- } . resume ( )
425
- }
426
457
}
427
458
428
459
extension AdaWebHost : WKScriptMessageHandler {
@@ -439,14 +470,8 @@ extension AdaWebHost: WKScriptMessageHandler {
439
470
} else if messageName == " eventCallbackHandler " {
440
471
if let event = message. body as? [ String : Any ] {
441
472
if let eventName = event [ " event_name " ] as? String {
442
- if eventName == " adaDownloadTranscript " {
443
- if let urlstr = event [ " url " ] as? String , let name = event [ " name " ] as? String {
444
- if let url = URL ( string: urlstr. replacingOccurrences ( of: " " , with: " " ) ) {
445
- downloadBlobURL ( url: url, fileName: name)
446
- }
447
- }
448
- }
449
- else if let specificCallback = self . eventCallbacks ? [ eventName] {
473
+
474
+ if let specificCallback = self . eventCallbacks ? [ eventName] {
450
475
specificCallback ( event)
451
476
}
452
477
}
0 commit comments