问题
Im new to ios development and I have come straight into SwiftUI and Xcode 12. I'm trying to understand the flow for login from a login screen that after you input your credentials you are presented with a tabview screen.
When the app first loads, its presented with the login and after login is successful the token from the server is returned and saved and further starting of the app checks for token and displays appropriate view
WindowGroup {
if token == nil {
LoginView()
} else {
TabView()
}
}
My problem is within the app a call is made to the server and the result shows invalid token, I want to send the user back to the login screen, but TabView had already been set. I also use the NavigationView and dont want the back button available on the login screen.
The tutorials I have found generally require the use of appDelegate and sceneDelegate but I would assume its possible without these anymore
Any help is appreciated
回答1:
Layering your UI would be the cleanest IMO. In you're app main I would create different layers that would be responsible for doing something before the next layer is shown. Take a look at the below sample code...
WindowGroup {
ZStack {
Color.background.edgesIgnoringSafeArea(.all)
// this layer will take care of sign up and authentication
AuthenticationLayer { account: AccountStore in
// city select will take care of a default location for the app
CitySelectLayer { city, citySelect in
// this layer will be responsible for displaying overlaid media
MediaLayer { mediaVM in
// main screen
TabbedScreen()
.environmentObject(account)
.environmentObject(city)
.environmentObject(citySelect)
.environmentObject(mediaVM)
}
}
}
}
}
I've tried several different methods and this is the cleanest. The layer view would look something like this...
struct MediaLayer<Content: View> : View {
// content
let content : (ImagesOverlayVM) -> Content
// view model
@StateObject var viewModel = ImagesOverlayVM()
var exitButton : some View {
Button(action: viewModel.dismissImages) {
Image(systemName: "xmark.circle")
.font(.system(.title3, design: .rounded))
.foregroundColor(.secondary)
}
}
var header : some View {
HStack {
exitButton
Spacer()
}.padding()
}
var loadingView : some View {
Image(systemImage: .radiowaves)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 32, height: 32)
.foregroundColor(.secondary)
.spin()
}
var placeholder : some View {
Image(systemName: "photo")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 32, height: 32)
.foregroundColor(.secondary)
}
func tabView(images: [S3ImageLoader]) -> some View {
TabView {
ForEach(images) { vm in
S3ImageView(viewModel: vm, contentMode: .fit) {
loadingView
} placeholder: {
placeholder
}
}
}.tabViewStyle(PageTabViewStyle(indexDisplayMode: .automatic))
}
var body : some View {
ZStack {
content(viewModel)
.isHidden(viewModel.images != nil)
if let images = viewModel.images {
VStack(spacing: 0) {
header
tabView(images: images)
header
.hidden()
}
}
}
}
}
来源:https://stackoverflow.com/questions/65333372/pure-swiftui-login-signup-register-flow-is-it-possible