From b53eecd78ed0877a7d5c14f56a66881f1cbba3b2 Mon Sep 17 00:00:00 2001 From: dankito Date: Tue, 1 Sep 2020 16:05:16 +0200 Subject: [PATCH] Implemented parsing TAN challenge's HTML --- .../BankingiOSApp/ui/SwiftExtensions.swift | 19 +++++++++++++++++++ .../ui/views/EnterTanDialog.swift | 6 +++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/ui/BankingiOSApp/BankingiOSApp/ui/SwiftExtensions.swift b/ui/BankingiOSApp/BankingiOSApp/ui/SwiftExtensions.swift index d82e1f26..23d20634 100644 --- a/ui/BankingiOSApp/BankingiOSApp/ui/SwiftExtensions.swift +++ b/ui/BankingiOSApp/BankingiOSApp/ui/SwiftExtensions.swift @@ -41,6 +41,25 @@ extension String { } + /// + /// Be aware that this crashes when used in a View's body method. So use it only e.g. in init() method + /// + /// This an htmlToString has been copied from https://stackoverflow.com/a/47230632 + /// + var htmlToAttributedString: NSAttributedString? { + guard let data = data(using: .utf8) else { return nil } + do { + return try NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding:String.Encoding.utf8.rawValue], documentAttributes: nil) + } catch { + return nil + } + } + + var htmlToString: String { + return htmlToAttributedString?.string ?? "" + } + + var isCoreDataId: Bool { return self.starts(with: "x-coredata://") } diff --git a/ui/BankingiOSApp/BankingiOSApp/ui/views/EnterTanDialog.swift b/ui/BankingiOSApp/BankingiOSApp/ui/views/EnterTanDialog.swift index 7e767eb0..567cc951 100644 --- a/ui/BankingiOSApp/BankingiOSApp/ui/views/EnterTanDialog.swift +++ b/ui/BankingiOSApp/BankingiOSApp/ui/views/EnterTanDialog.swift @@ -35,6 +35,8 @@ struct EnterTanDialog: View { private let imageTanChallenge: ImageTanChallenge? + private let messageToShowToUser: String + @State private var enteredTan = "" @@ -57,6 +59,8 @@ struct EnterTanDialog: View { self.flickerCodeTanChallenge = tanChallenge as? FlickerCodeTanChallenge self.imageTanChallenge = tanChallenge as? ImageTanChallenge + self.messageToShowToUser = tanChallenge.messageToShowToUser.htmlToString // parse in init() calling this method in body { } crashes application + if let decodingError = (tanChallenge as? FlickerCodeTanChallenge)?.flickerCode.decodingError { showDecodingTanChallengeFailedErrorDelayed(decodingError) } @@ -97,7 +101,7 @@ struct EnterTanDialog: View { } HStack { - CollapsibleText(tanChallenge.messageToShowToUser) + CollapsibleText(messageToShowToUser) Spacer() } .padding(.top, 6)