0

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

pawello2222
  • 46,897
  • 22
  • 145
  • 209
aker10
  • 11
  • 1
  • You need to set the text and image in the timeline provider. You should not be fetching them from storage inside the Widget. They should be passed to the widget via the entry. – Andrew Dec 12 '20 at 07:50
  • Thank you Andrew. Woulod you mind giving me an example please? Thank you very much @Andrew – aker10 Dec 12 '20 at 21:22
  • Actually you can read from file like in the example above, so that's not an issue (probably). Especially if you say the image is updated correctly. But you're suppressing all the errors in `try? String(contentsOf: url, encoding: .utf8)`. Try replacing it with `do-try-catch` instead. – pawello2222 Dec 14 '20 at 17:18
  • @pawello2222Thank you very much. Can you give me an example please? My code is based on your widget app group code on github. Thanks – aker10 Dec 15 '20 at 11:39
  • If you're using `try?` the result will be `nil` if the operation fails and you'll never know why. So first use `try` (or even `try!` for debugging only) to see where you have errors. – pawello2222 Dec 16 '20 at 10:02

0 Answers0