Using a SwiftUI View in app from a Package

Let's say I have a public "content view" in a package.

Code Block
public struct SwiftUIView: View {
    public var body: some View {
        Text("Hello, world from within a package!").padding()
    }
}

How would I implement that in an app to replace the default ContentView.

Code Block
struct ContentView: View {
    var body: some View {
        Text("Hello, world!").padding()
    }
}

What I am attempting to do is have a complete logic flow within a package, and use this flow inside an app.

With storyboards I am able; in the app, is to access the flow within the framework through the use of storyboard references. As the app launches the framework's view controller is launched.

Is this possible in SwiftUI ?

Thank you
Answered by gbacklin in 620751022
Ok, I figured out how to accomplish what I wanted to do.

First I do not have to remove the original ContentView I just have to modify it to include the new view that is on the package. I did change names a bit so it makes sense (the new Package is called Navigator). So the original ContentView is now:
Code Block
import SwiftUI
import Navigator
struct ContentView: View {
    var body: some View {
        NavigatorView()
    }
}

Now the Package (Navigator) has 2 views. (NavigatorView and DetailView)
Code Block
import SwiftUI
public struct NavigatorView: View {
    let colors = ["Red", "Green", "Blue"]
    public init() {
    }
    @available(iOS 13.0.0, *)
    public var body: some View {
        NavigationView {
            List(colors, id:\.self) {
                color in
                NavigationLink(destination: DetailView(color: color)) {
                    Text(color).padding()
                }
            }
            .navigationBarTitle("Colors")
        }
    }
}
struct NavigatorView_Previews: PreviewProvider {
    @available(iOS 13.0.0, *)
    static var previews: some View {
        NavigatorView()
    }
}

Code Block
import SwiftUI
struct DetailView: View {
    let color: String
    @available(iOS 13.0, *)
    var body: some View {
        Text(color).padding()
            .navigationBarTitle(Text(color), displayMode: .inline)
    }
}
struct DetailView_Previews: PreviewProvider {
    @available(iOS 13.0, *)
    static var previews: some View {
        DetailView(color: "Red")
    }
}

So when the app launches, the NetworkView in the Navigator package takes control and you are now following the logic flow within the Package.

I have 2 branches to the following GitHub repo at TestPackages
  1. internal - has the package within the same project locally

  2. external - is using the external Navigator package

For those who are well versed in SwiftUI, this may be a "of course that is how it works", but I am now trying to get a hold of the more beyond general UI issues coming from the nib, xib, storyboard crowd.

I hope this helps someone !

Take Care

Accepted Answer
Ok, I figured out how to accomplish what I wanted to do.

First I do not have to remove the original ContentView I just have to modify it to include the new view that is on the package. I did change names a bit so it makes sense (the new Package is called Navigator). So the original ContentView is now:
Code Block
import SwiftUI
import Navigator
struct ContentView: View {
    var body: some View {
        NavigatorView()
    }
}

Now the Package (Navigator) has 2 views. (NavigatorView and DetailView)
Code Block
import SwiftUI
public struct NavigatorView: View {
    let colors = ["Red", "Green", "Blue"]
    public init() {
    }
    @available(iOS 13.0.0, *)
    public var body: some View {
        NavigationView {
            List(colors, id:\.self) {
                color in
                NavigationLink(destination: DetailView(color: color)) {
                    Text(color).padding()
                }
            }
            .navigationBarTitle("Colors")
        }
    }
}
struct NavigatorView_Previews: PreviewProvider {
    @available(iOS 13.0.0, *)
    static var previews: some View {
        NavigatorView()
    }
}

Code Block
import SwiftUI
struct DetailView: View {
    let color: String
    @available(iOS 13.0, *)
    var body: some View {
        Text(color).padding()
            .navigationBarTitle(Text(color), displayMode: .inline)
    }
}
struct DetailView_Previews: PreviewProvider {
    @available(iOS 13.0, *)
    static var previews: some View {
        DetailView(color: "Red")
    }
}

So when the app launches, the NetworkView in the Navigator package takes control and you are now following the logic flow within the Package.

I have 2 branches to the following GitHub repo at TestPackages
  1. internal - has the package within the same project locally

  2. external - is using the external Navigator package

For those who are well versed in SwiftUI, this may be a "of course that is how it works", but I am now trying to get a hold of the more beyond general UI issues coming from the nib, xib, storyboard crowd.

I hope this helps someone !

Take Care

Using a SwiftUI View in app from a Package
 
 
Q