Skip to content

Commit

Permalink
Merge pull request #46 from AdaSupport/CHATX-1247-Download-Transcript…
Browse files Browse the repository at this point in the history
…-React-Native

CHATX-1247: Refactor Download Transcript
  • Loading branch information
michaellazz authored Mar 30, 2022
2 parents a963cc3 + ecac38f commit 28ab4fa
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 37 deletions.
2 changes: 1 addition & 1 deletion AdaEmbedFramework.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |spec|

spec.name = "AdaEmbedFramework"
spec.version = "1.4.0"
spec.version = "1.4.1"
spec.summary = "Embed the Ada Support SDK in your app."
spec.description = "Use the Ada Support SDK to inject the Ada support experience into your app. Visit https://ada.support to learn more."
spec.homepage = "https://github.com/AdaSupport/ios-sdk"
Expand Down
97 changes: 61 additions & 36 deletions EmbedFramework/AdaWebHost.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class AdaWebHost: NSObject {
public var greeting = ""
public var webViewTimeout = 30.0


/// Metafields can be passed in during init; use `setMetaFields()` and `setSensitiveMetafields()`
/// to send values in at runtime
private var metafields: [String: Any] = [:]
Expand Down Expand Up @@ -88,6 +89,7 @@ public class AdaWebHost: NSObject {
self.metafields = metafields
// we always want to append the sdkType
self.metafields["sdkType"] = "IOS"
self.metafields["sdkSupportsDownloadLink"] = true
self.sensitiveMetafields = sensitiveMetafields
self.openWebLinksInSafari = openWebLinksInSafari
self.appScheme = appScheme
Expand Down Expand Up @@ -340,7 +342,22 @@ extension AdaWebHost {
}
}

extension AdaWebHost: WKNavigationDelegate, WKUIDelegate {
extension AdaWebHost: WKNavigationDelegate, WKUIDelegate, WKDownloadDelegate {
@available(iOS 14.5, *)
public func download(_ download: WKDownload, decideDestinationUsing response: URLResponse, suggestedFilename: String, completionHandler: @escaping (URL?) -> Void) {

let localFileURL = FileManager.default.temporaryDirectory.appendingPathComponent(suggestedFilename)

completionHandler(localFileURL)

DispatchQueue.main.async { [self] in
// present activity viewer
let items = [localFileURL]
let ac = UIActivityViewController(activityItems: items, applicationActivities: nil)
findViewController(from: self.webView!)?.present(ac, animated: true)
}
}

public func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
/// Whena reset method is built - we will need to set this back to false
self.hasError = true
Expand Down Expand Up @@ -385,7 +402,13 @@ extension AdaWebHost: WKNavigationDelegate, WKUIDelegate {

// Used for processing all other navigation
public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Swift.Void) {
if navigationAction.navigationType == WKNavigationType.linkActivated {
if #available(iOS 14.5, *), navigationAction.shouldPerformDownload{
decisionHandler(.download)
} else if navigationAction.request.url!.absoluteString.range(of: "/transcript/") != nil{
downloadUrl(url: navigationAction.request.url!, fileName: "chat_transcript.txt")
decisionHandler(.cancel)
}
else if navigationAction.navigationType == WKNavigationType.linkActivated {
if let url = navigationAction.request.url {
openUrl(webView: webView, url: url)
}
Expand All @@ -395,34 +418,42 @@ extension AdaWebHost: WKNavigationDelegate, WKUIDelegate {
decisionHandler(.allow)
}
}


// Download the file from the given url and store it locally in the app's temp folder and present the activity viewer.
private func downloadUrl(url downloadUrl : URL, fileName: String) {
let localFileURL = FileManager.default.temporaryDirectory.appendingPathComponent(fileName)

// Download the file from the given url and store it locally in the app's temp folder and present the activity viewer.
private func downloadBlobURL(url downloadUrl : URL, fileName: String) {
let localFileURL = FileManager.default.temporaryDirectory.appendingPathComponent(fileName)
URLSession.shared.dataTask(with: downloadUrl) { data, response, err in
guard let data = data, err == nil else {
debugPrint("Error downloading from url=\(downloadUrl.absoluteString): \(err.debugDescription)")
return
}
if let httpResponse = response as? HTTPURLResponse {
debugPrint("HTTP Status=\(httpResponse.statusCode)")
}
// write the downloaded data to a temporary folder
do {
try data.write(to: localFileURL, options: .atomic) // atomic option overwrites it if needed
DispatchQueue.main.async { [self] in
// present activity viewer
let items = [localFileURL]
let ac = UIActivityViewController(activityItems: items, applicationActivities: nil)
findViewController(from: self.webView!)?.present(ac, animated: true)
}
} catch {
debugPrint(error)
return
}
}.resume()
}

@available(iOS 14.5, *)
public func webView(_ webView: WKWebView, navigationAction: WKNavigationAction, didBecome download: WKDownload) {

download.delegate = self
}

URLSession.shared.dataTask(with: downloadUrl) { data, response, err in
guard let data = data, err == nil else {
debugPrint("Error downloading from url=\(downloadUrl.absoluteString): \(err.debugDescription)")
return
}
if let httpResponse = response as? HTTPURLResponse {
debugPrint("HTTP Status=\(httpResponse.statusCode)")
}
// write the downloaded data to a temporary folder
do {
try data.write(to: localFileURL, options: .atomic) // atomic option overwrites it if needed
DispatchQueue.main.async { [self] in
// present activity viewer
let items = [localFileURL]
let ac = UIActivityViewController(activityItems: items, applicationActivities: nil)
findViewController(from: self.webView!)?.present(ac, animated: true)
}
} catch {
debugPrint(error)
return
}
}.resume()
}
}

extension AdaWebHost: WKScriptMessageHandler {
Expand All @@ -439,14 +470,8 @@ extension AdaWebHost: WKScriptMessageHandler {
} else if messageName == "eventCallbackHandler" {
if let event = message.body as? [String: Any] {
if let eventName = event["event_name"] as? String {
if eventName == "adaDownloadTranscript" {
if let urlstr = event["url"] as? String, let name = event["name"] as? String {
if let url = URL(string: urlstr.replacingOccurrences(of: " ", with: "")) {
downloadBlobURL(url: url, fileName: name)
}
}
}
else if let specificCallback = self.eventCallbacks?[eventName] {

if let specificCallback = self.eventCallbacks?[eventName] {
specificCallback(event)
}
}
Expand Down

0 comments on commit 28ab4fa

Please sign in to comment.