I am trying to incorporate some SwiftUI into my UIKit based iPhone application. I have a view controller in my Storyboard called ‘SellingNewsViewController’ with an associated “.swift” file. In this “SellingNewsViewController.swift" file, I am using the following code to try and create a UIHostingController to show the SwiftUI code I currently have in another file called “ContentView.swift”.
And I have a SwiftUI file in this directory (the code I am trying to run) named ‘ContentView.swift’ which has the following Syntax:
I understand that I need to use a UIHostingController to show SwiftUI views in my UIKit application, however whenever I load up my ‘SellingNewsViewController’, I just receive a blank screen, as shown here -> h ttps://imgur.com/a/GlyWV9i
Am I doing something wrong to try and create/show the UIHostingController?
Code Block class SellingNewsViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let controller = UIHostingController(rootView: ContentView()) self.addChild(controller) self.view.addSubview(controller.view) controller.didMove(toParent: self) }
And I have a SwiftUI file in this directory (the code I am trying to run) named ‘ContentView.swift’ which has the following Syntax:
Code Block import SwiftUI import SwiftyJSON import SDWebImageSwiftUI import WebKit struct ContentView: View { @ObservedObject var list = getData() var body: some View { NavigationView{ List(list.datas){i in NavigationLink(destination: webView(url: i.url) .navigationBarTitle("", displayMode: .inline)){ HStack(spacing: 15){ VStack(alignment: .leading, spacing: 10){ Text(i.title).fontWeight(.heavy) Text(i.desc) } if i.image != "" { WebImage(url: URL(string: i.image)!, options: .highPriority, context: nil) .resizable() .frame(width: 110, height: 135) .cornerRadius(20).lineLimit(2) } }.padding(.vertical, 15) } }.navigationBarTitle("Headlines") } } } struct ChildHostingController_Previews: PreviewProvider { static var previews: some View { ContentView() } } struct dataType : Identifiable { var id : String var title : String var desc : String var url : String var image : String } class getData : ObservableObject{ @Published var datas = [dataType]() init() { let source = "https://newsapi.org/v2/top-headlines?country=gb&category=business&apiKey=<mykeyishere>" let url = URL(string: source)! let session = URLSession(configuration: .default) session.dataTask(with: url) { (data, _, err) in if err != nil{ print((err?.localizedDescription)!) return } let json = try! JSON(data: data!) for i in json["articles"] { let title = i.1["title"].stringValue let description = i.1["description"].stringValue let url = i.1["url"].stringValue let image = i.1["urlToImage"].stringValue let id = i.1["publishedAt"].stringValue DispatchQueue.main.async { self.datas.append(dataType(id: id, title: title, desc: description, url: url, image: image)) } } }.resume() } } struct webView : UIViewRepresentable { var url : String func makeUIView(context: UIViewRepresentableContext<webView>) -> WKWebView{ let view = WKWebView() view.load(URLRequest(url: URL(string: url)!)) return view } func updateUIView(_ uiView: WKWebView, context: UIViewRepresentableContext<webView>) { } }
I understand that I need to use a UIHostingController to show SwiftUI views in my UIKit application, however whenever I load up my ‘SellingNewsViewController’, I just receive a blank screen, as shown here -> h ttps://imgur.com/a/GlyWV9i
Am I doing something wrong to try and create/show the UIHostingController?