URL+id+iban.swift (3333B)
1 /* 2 * This file is part of GNU Taler, ©2022-25 Taler Systems S.A. 3 * See LICENSE.md 4 */ 5 /** 6 * @author Marc Stibane 7 */ 8 import SwiftUI 9 10 extension URL: Identifiable { 11 public var id: URL {self} 12 } 13 14 extension URL { 15 init(_ string: StaticString) { 16 self.init(string: "\(string)")! 17 } 18 19 var cyclos: String? { 20 /// "payto://cyclos/" host ["/" fpath] "/" account-id [ "?" opts ] 21 if scheme == "payto", let host = host { 22 let path = path.dropFirst(1) 23 if host == "cyclos" { 24 return String(path) 25 } else if host.hasPrefix("cyclos") { 26 let cyclos = host.dropFirst(6) 27 if path.hasPrefix("/") { 28 return String(cyclos + path) 29 } 30 return String(cyclos + "/" + path) 31 } 32 } 33 return nil 34 } 35 36 var iban: String? { 37 /// https://datatracker.ietf.org/doc/rfc8905/ 38 /// payto://iban/DE75512108001245126199?amount=EUR:200.0&message=hello 39 if scheme == "payto" && host == "iban" { 40 return lastPathComponent 41 } 42 return nil 43 } 44 45 var xTaler: String? { 46 /// https://datatracker.ietf.org/doc/rfc8905/ 47 /// payto://iban/DE75512108001245126199?amount=EUR:200.0&message=hello 48 if scheme == "payto" && host == "x-taler-bank" { 49 return lastPathComponent 50 } 51 return nil 52 } 53 54 /// SwifterSwift: Dictionary of the URL's query parameters. 55 var queryParameters: [String:String]? { 56 guard let components = URLComponents(url: self, resolvingAgainstBaseURL: false), 57 let queryItems = components.queryItems else { return nil } 58 59 var items: [String:String] = [:] 60 61 for queryItem in queryItems { 62 items[queryItem.name] = queryItem.value 63 } 64 return items 65 } 66 67 static var docDirUrl: URL? { // accessible by FileSharing 68 if #available(iOS 16.4, *) { 69 return URL.documentsDirectory 70 } else { 71 let docDirNr: FileManager.SearchPathDirectory = .documentDirectory 72 if let documentsDir = FileManager.default.urls(for: docDirNr, in: .userDomainMask).first { 73 return documentsDir 74 } 75 } 76 return nil 77 } 78 79 static var appSuppUrl: URL? { 80 if #available(iOS 16.4, *) { 81 return URL.applicationSupportDirectory 82 } else { 83 let appDirNr: FileManager.SearchPathDirectory = .applicationSupportDirectory 84 if let appSupportDir = FileManager.default.urls(for: appDirNr, in: .userDomainMask).first { 85 return appSupportDir 86 } 87 } 88 return nil 89 } 90 91 func path(withSlash: Bool) -> String { 92 let path: String 93 if #available(iOS 16.4, *) { 94 path = self.path(percentEncoded: false) 95 } else { 96 path = self.path 97 } 98 if let char = path.last { 99 let slash: String.Element = "/" 100 101 if withSlash { 102 if char != slash { 103 return path + String(slash) 104 } 105 } else { 106 if char == slash { 107 return String(path.dropLast()) 108 } 109 } 110 } 111 return path 112 } 113 }