Skip to content

Commit c6d99e2

Browse files
author
andrei vassiliev
committed
CHATX-1198 Implemented transcript download ability in iOS by way of events
1 parent 5b40a08 commit c6d99e2

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
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.3.10"
4+
spec.version = "1.3.11"
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

+36-2
Original file line numberDiff line numberDiff line change
@@ -307,14 +307,41 @@ extension AdaWebHost: WKNavigationDelegate, WKUIDelegate {
307307
decisionHandler(.allow)
308308
}
309309
}
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+
}
310338
}
311339

312340
extension AdaWebHost: WKScriptMessageHandler {
313341
/// When the webview loads up, it'll pass back a message to here.
314342
/// Fire our initialize methods when that happens.
315343
public func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
316344
let messageName = message.name
317-
318345
if messageName == "embedReady" {
319346
self.webHostLoaded = true
320347
} else if let zdChatterAuthCallback = self.zdChatterAuthCallback, messageName == "zdChatterAuthCallbackHandler" {
@@ -324,7 +351,14 @@ extension AdaWebHost: WKScriptMessageHandler {
324351
} else if messageName == "eventCallbackHandler" {
325352
if let event = message.body as? [String: Any] {
326353
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] {
328362
specificCallback(event)
329363
}
330364
}

0 commit comments

Comments
 (0)