Skip to content

Commit

Permalink
fix the downloads and push-token
Browse files Browse the repository at this point in the history
  • Loading branch information
khmyznikov committed Feb 18, 2024
1 parent 19d2ec8 commit d9f5270
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func handleFCMToken(){
checkViewAndEvaluate(event: "push-token", detail: "ERROR GET TOKEN")
} else if let token = token {
print("FCM registration token: \(token)")
checkViewAndEvaluate(event: "push-token", detail: token)
checkViewAndEvaluate(event: "push-token", detail: "'\(token)'")
}
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,13 @@ extension ViewController: WKUIDelegate, WKDownloadDelegate {
}
// restrict navigation to target host, open external links in 3rd party apps
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
if (navigationAction.request.url?.scheme == "about" || navigationAction.request.url?.scheme == "blob") {
if (navigationAction.request.url?.scheme == "about") {
return decisionHandler(.allow)
}
if (navigationAction.shouldPerformDownload || navigationAction.request.url?.scheme == "blob") {
return decisionHandler(.download)
}

if let requestUrl = navigationAction.request.url{
if let requestHost = requestUrl.host {
// NOTE: Match auth origin first, because host origin may be a subset of auth origin and may therefore always match
Expand Down Expand Up @@ -161,25 +165,20 @@ extension ViewController: WKUIDelegate, WKDownloadDelegate {
}
}
} else {
if navigationAction.request.url?.scheme == "blob" {
decisionHandler(.download)
decisionHandler(.cancel)
if (navigationAction.request.url?.scheme == "tel" || navigationAction.request.url?.scheme == "mailto" ){
if (UIApplication.shared.canOpenURL(requestUrl)) {
UIApplication.shared.open(requestUrl)
}
}
else {
decisionHandler(.cancel)
if (navigationAction.request.url?.scheme == "tel" || navigationAction.request.url?.scheme == "mailto" ){
if (UIApplication.shared.canOpenURL(requestUrl)) {
UIApplication.shared.open(requestUrl)
}
}
else {
if requestUrl.isFileURL {
// not tested
downloadAndOpenFile(url: requestUrl.absoluteURL)
}
if (requestUrl.absoluteString.contains("base64")){
downloadAndOpenBase64File(base64String: requestUrl.absoluteString)
}
if requestUrl.isFileURL {
// not tested
downloadAndOpenFile(url: requestUrl.absoluteURL)
}
// if (requestUrl.absoluteString.contains("base64")){
// downloadAndOpenBase64File(base64String: requestUrl.absoluteString)
// }
}
}
}
Expand Down Expand Up @@ -323,31 +322,31 @@ extension ViewController: WKUIDelegate, WKDownloadDelegate {
task.resume()
}

func downloadAndOpenBase64File(base64String: String) {
// Split the base64 string to extract the data and the file extension
let components = base64String.components(separatedBy: ";base64,")

// Make sure the base64 string has the correct format
guard components.count == 2, let format = components.first?.split(separator: "/").last else {
print("Invalid base64 string format")
return
}

// Remove the data type prefix to get the base64 data
let dataString = components.last!

if let imageData = Data(base64Encoded: dataString) {
let documentsUrl: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let destinationFileUrl = documentsUrl.appendingPathComponent("image.\(format)")

do {
try imageData.write(to: destinationFileUrl)
self.openFile(url: destinationFileUrl)
} catch {
print("Error writing image to file url: \(destinationFileUrl): \(error)")
}
}
}
// func downloadAndOpenBase64File(base64String: String) {
// // Split the base64 string to extract the data and the file extension
// let components = base64String.components(separatedBy: ";base64,")

// // Make sure the base64 string has the correct format
// guard components.count == 2, let format = components.first?.split(separator: "/").last else {
// print("Invalid base64 string format")
// return
// }

// // Remove the data type prefix to get the base64 data
// let dataString = components.last!

// if let imageData = Data(base64Encoded: dataString) {
// let documentsUrl: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
// let destinationFileUrl = documentsUrl.appendingPathComponent("image.\(format)")

// do {
// try imageData.write(to: destinationFileUrl)
// self.openFile(url: destinationFileUrl)
// } catch {
// print("Error writing image to file url: \(destinationFileUrl): \(error)")
// }
// }
// }

func openFile(url: URL) {
self.documentController = UIDocumentInteractionController(url: url)
Expand Down

0 comments on commit d9f5270

Please sign in to comment.