Skip to content

Commit 28ab4fa

Browse files
authored
Merge pull request #46 from AdaSupport/CHATX-1247-Download-Transcript-React-Native
CHATX-1247: Refactor Download Transcript
2 parents a963cc3 + ecac38f commit 28ab4fa

File tree

2 files changed

+62
-37
lines changed

2 files changed

+62
-37
lines changed

AdaEmbedFramework.podspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Pod::Spec.new do |spec|
22

33
spec.name = "AdaEmbedFramework"
4-
spec.version = "1.4.0"
4+
spec.version = "1.4.1"
55
spec.summary = "Embed the Ada Support SDK in your app."
66
spec.description = "Use the Ada Support SDK to inject the Ada support experience into your app. Visit https://ada.support to learn more."
77
spec.homepage = "https://github.com/AdaSupport/ios-sdk"

EmbedFramework/AdaWebHost.swift

+61-36
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class AdaWebHost: NSObject {
2525
public var greeting = ""
2626
public var webViewTimeout = 30.0
2727

28+
2829
/// Metafields can be passed in during init; use `setMetaFields()` and `setSensitiveMetafields()`
2930
/// to send values in at runtime
3031
private var metafields: [String: Any] = [:]
@@ -88,6 +89,7 @@ public class AdaWebHost: NSObject {
8889
self.metafields = metafields
8990
// we always want to append the sdkType
9091
self.metafields["sdkType"] = "IOS"
92+
self.metafields["sdkSupportsDownloadLink"] = true
9193
self.sensitiveMetafields = sensitiveMetafields
9294
self.openWebLinksInSafari = openWebLinksInSafari
9395
self.appScheme = appScheme
@@ -340,7 +342,22 @@ extension AdaWebHost {
340342
}
341343
}
342344

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+
344361
public func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
345362
/// Whena reset method is built - we will need to set this back to false
346363
self.hasError = true
@@ -385,7 +402,13 @@ extension AdaWebHost: WKNavigationDelegate, WKUIDelegate {
385402

386403
// Used for processing all other navigation
387404
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 {
389412
if let url = navigationAction.request.url {
390413
openUrl(webView: webView, url: url)
391414
}
@@ -395,34 +418,42 @@ extension AdaWebHost: WKNavigationDelegate, WKUIDelegate {
395418
decisionHandler(.allow)
396419
}
397420
}
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)
398426

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+
}
402456

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-
}
426457
}
427458

428459
extension AdaWebHost: WKScriptMessageHandler {
@@ -439,14 +470,8 @@ extension AdaWebHost: WKScriptMessageHandler {
439470
} else if messageName == "eventCallbackHandler" {
440471
if let event = message.body as? [String: Any] {
441472
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] {
450475
specificCallback(event)
451476
}
452477
}

0 commit comments

Comments
 (0)