HomeiOS Developmentios - The way to make replace View when use UIViewRepresentable in...

ios – The way to make replace View when use UIViewRepresentable in swift mission


I’m engaged on exhibiting the React-native-View within the swift mission. I obtain the bunlde file via asynchronous operation, save the bundle file within the app listing, and use it to create the RCTROOTVIEW. My present downside is that when I attempt to present the view, I see a clean display as a result of the method of downloading and saving the file is asynchronous. How can I clear up this downside?

my supply

import UIKit
import React
import SwiftUI
import Basis

struct ReactNativeView: UIViewRepresentable {
  
    let mockData: NSDictionary = ["scores": [["name": "Alex", "value": "42"], ["name": "Joel", "value": "10"]]]
    let jsCodeLocation = URL(string: "https://CDN.com/important.jsbundle")!
  
    func getDocumentsDirectory() -> URL {
        return FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
    }

    func downloadBundle(url: URL, completion: @escaping (URL?, Error?) -> Void) {
      let documentsUrl = getDocumentsDirectory()
      let destinationUrl = documentsUrl.appendingPathComponent(url.lastPathComponent)
         URLSession.shared.downloadTask(with: url) { (tempLocalUrl, response, error) in
             guard let tempLocalUrl = tempLocalUrl, error == nil else {
                 print("Error downloading bundle: (String(describing: error))")
                 completion(nil, error)
                 return
             }

             do {
                 strive FileManager.default.moveItem(at: tempLocalUrl, to: destinationUrl)
                 print("File moved to vacation spot [(destinationUrl.path)]")
                 completion(destinationUrl, nil)
             } catch {
                 print("Error transferring file to vacation spot: (error)")
                 completion(nil, error)
             }
         }.resume()
    }
 
    func makeUIView(context: Context) -> UIView {
      var rootView : RCTRootView?
 
      downloadBundle(url: jsCodeLocation) { _url, error in
        guard let localURL = _url, error == nil else {
            print("error saving file")
            return
        }
        rootView = RCTRootView(
            bundleURL: localURL,
            moduleName: "RNHighScores",
            initialProperties: mockData as [NSObject: AnyObject],
            launchOptions: nil
        )
      }
      

      
      return rootView ?? UIView()
    }

    func updateUIView(_ uiView: UIView, context: Context) {}
  
}

Lastly, if you have already got a downloaded file, I wish to erase or overwrite it.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments