纯SwiftUI登录,注册,注册流程,可以吗? [英] Pure SwiftUI login, signup, register flow, is it possible?

查看:78
本文介绍了纯SwiftUI登录,注册,注册流程,可以吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是ios开发的新手,我直接使用SwiftUI和Xcode12.我试图从登录屏幕了解登录流程,在输入凭据后,您会看到一个tabview屏幕.

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()
    }
}

我的问题是在应用程序内对服务器进行了调用,结果显示了无效的令牌,我想将用户发送回登录屏幕,但是已经设置了TabView.我还使用了NavigationView,并且不希望登录屏幕上的后退按钮可用.

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.

我发现的教程通常需要使用appDelegate和sceneDelegate,但我认为如果没有这些内容,这是有可能的

The tutorials I have found generally require the use of appDelegate and sceneDelegate but I would assume its possible without these anymore

感谢您的帮助

推荐答案

将UI分层是最干净的IMO.在应用程序主界面中,我将创建不同的层,这些层将负责在显示下一层之前执行某些操作.看看下面的示例代码...

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()
            }
        }
    }
}

}

这篇关于纯SwiftUI登录,注册,注册流程,可以吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆