diff --git a/TalerCommon/Helper/TalerStrings.swift b/TalerCommon/Helper/TalerStrings.swift index bb2f0692..437524ac 100644 --- a/TalerCommon/Helper/TalerStrings.swift +++ b/TalerCommon/Helper/TalerStrings.swift @@ -71,6 +71,26 @@ extension String { return nil } + /// The first Taler link in a piece of pasted text. + /// + /// What this wallet puts on the pasteboard and into the share sheet is two lines -- the + /// `taler://` link and its `https://wallet.taler.net/link#` form -- and text copied from a + /// chat can have more around them. `toURL` makes ONE url of all of it (the line break + /// becomes %0A), which is no link at all. + public var firstTalerLink: URL? { + var webLink: URL? = nil + for part in self.components(separatedBy: .whitespacesAndNewlines) where !part.isEmpty { + guard let url = URL(string: part), let scheme = url.scheme?.lowercased() else { continue } + if scheme == "taler" { + return url + } + if webLink == nil, scheme == "https", url.host == "wallet.taler.net", url.path.hasPrefix("/link") { + webLink = url + } + } + return webLink + } + func deletingPrefix(_ prefix: String) -> String { guard self.hasPrefix(prefix) else { return self } return String(self.dropFirst(prefix.count)) diff --git a/TalerWallet1/Controllers/TalerWallet1App.swift b/TalerWallet1/Controllers/TalerWallet1App.swift index 77bc69b6..a62d1e17 100644 --- a/TalerWallet1/Controllers/TalerWallet1App.swift +++ b/TalerWallet1/Controllers/TalerWallet1App.swift @@ -61,8 +61,11 @@ struct TalerWallet1App: App { func inspectPasteboard(playSound: Bool) { let pasteboard = UIPasteboard.general - // We are only interested in URLs - if !pasteboard.hasURLs { return } + // We are only interested in links -- but a link copied as text is not always a URL to + // the pasteboard: two lines of text, which is what our own Copy and Share produce + // (taler:// and the https form), have no public.url representation, and neither has + // the [QR image, text] pair that Copy writes. + if !pasteboard.hasURLs && !pasteboard.hasStrings { return } // users get asked whether they want to paste, and only if they agree we go to completion pasteboard.detectPatterns(for: [UIPasteboard.DetectionPattern.probableWebURL], @@ -76,17 +79,18 @@ struct TalerWallet1App: App { if detectedPatterns.contains([UIPasteboard.DetectionPattern.probableWebURL]) { // Will match if the pasteboard string has a URL within it - self.pastedWebURL = pasteboard.string - if let string = pasteboard.string { + // every item, not only the first: Copy puts the QR image first, + // and `pasteboard.string` is the first item's string, i.e. nil + let strings = pasteboard.strings ?? [] + let string: String? = strings.isEmpty ? nil : strings.joined(separator: LINEFEED) + self.pastedWebURL = string + if let string { if self.pastedString != string { self.pastedString = string - if let pastedURL = string.toURL { - let scheme = pastedURL.scheme - if scheme?.lowercased() == "taler" { -// print(string) - post(pastedURL) - return - } + if let pastedURL = string.firstTalerLink { +// print(string) + post(pastedURL) + return } } } diff --git a/TalerWallet1/Views/Actions/ActionsSheet.swift b/TalerWallet1/Views/Actions/ActionsSheet.swift index d91e7e16..d5caac95 100755 --- a/TalerWallet1/Views/Actions/ActionsSheet.swift +++ b/TalerWallet1/Views/Actions/ActionsSheet.swift @@ -146,18 +146,17 @@ struct ActionsSheet: View { if #available(iOS 26.0, *) { if !pasteAutomatically { #if TALER_NIGHTLY - PasteButton(supportedContentTypes: [.url]) { providers in + // .plainText as well: the two lines our own Copy and Share produce are + // text without a URL representation, and the button stayed disabled + PasteButton(supportedContentTypes: [.url, .plainText]) { providers in for provider in providers { if provider.canLoadObject(ofClass: String.self) { _ = provider.loadObject(ofClass: String.self) { string, error in guard let string else { return } - if let pastedURL = string.toURL { - let scheme = pastedURL.scheme - if scheme?.lowercased() == "taler" { - let pasteType = PasteType(pastedURL: pastedURL) - let userinfo = [NOTIFICATIONPASTE: pasteType] - NotificationCenter.default.post(name: .PasteAction, object: nil, userInfo: userinfo) - } + if let pastedURL = string.firstTalerLink { + let pasteType = PasteType(pastedURL: pastedURL) + let userinfo = [NOTIFICATIONPASTE: pasteType] + NotificationCenter.default.post(name: .PasteAction, object: nil, userInfo: userinfo) } } } @@ -168,7 +167,7 @@ struct ActionsSheet: View { .buttonBorderShape(.circle) // capsule .accessibility(sortPriority: 1) #else - let isEnabled = UIPasteboard.general.hasURLs + let isEnabled = UIPasteboard.general.hasURLs || UIPasteboard.general.hasStrings TalerPasteButton(accessibilityLabelStr: "Paste") { Task { TalerWallet1App().inspectPasteboard(playSound: true)