I have a barcode generator app where you generate barcode and you can add to the related widget. When you add to the widget, I want to add image and text. For image is ok but I have problem with text under the image
I have a File Manager swift file which is:
import Foundation
extension FileManager {
static let appGroupContainerURL = FileManager.default
.containerURL(forSecurityApplicationGroupIdentifier: "group.com.antoniocristiano.BarWidget")!`
static func clearAllFile() {
let fileManager = FileManager.default
let myDocuments = FileManager.appGroupContainerURL.appendingPathComponent(FileManager.qrCode)
do {
try fileManager.removeItem(at: myDocuments)
} catch {
return
}
}
}
extension FileManager {
static let qrCode = "qrCode.png"
}
extension String {
var isValidURL: Bool {
let detector = try! NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue)
if let match = detector.firstMatch(in: self, options: [], range: NSRange(location: 0, length: self.utf16.count)) {
// it is a link, if the match covers the whole string
return match.range.length == self.utf16.count
} else {
return false
}
}
}
And the code of the widget is:
var body: some View {
VStack {
imageFromFile
.resizable()
.scaledToFit()
Text("\(luckyNumberFromFile)")
}
}
var imageFromFile: Image {
let path = FileManager.appGroupContainerURL.appendingPathComponent(FileManager.qrCode).path
let img = UIImage(contentsOfFile: path) ?? UIImage(named: "qr")!
return Image(uiImage: img)
}
var luckyNumberFromFile: Int {
let url = FileManager.appGroupContainerURL.appendingPathComponent(FileManager.qrCode)
guard let text = try? String(contentsOf: url, encoding: .utf8) else { return 0 }
return Int(text) ?? 0
}
The image will update but not the text
Thank you very much for your help
Best