在我的应用程序中,我使用google,facebook,twitter登录。这三个登录都运行良好。
但是对于Twitter登录,我面临一个问题,即单击Twitter登录自定义按钮后,OAuthProvider将打开Twitter认证页面。我取消了身份验证,然后身份验证又返回到应用程序,并在第二秒内自动弹出导航视图。 LoginView关闭,并显示ContentView(启动视图)。
我按照以下简单步骤进行Twitter登录given by firebase
var provider = OAuthProvider(providerID: "twitter.com")
func twitterLogin(){
provider.getCredentialWith(nil) { credential, error in
if error != nil {
// Handle error.
print(error ?? "error")
}
if credential != nil {
Auth.auth().signIn(with: credential!) { authResult, error in
if error != nil {
// Handle error.
}
else{
self.twitterLoginSuccess = true
print("Twitter login success")
}
// User is signed in.
// IdP data available in authResult.additionalUserInfo.profile.
// Twitter OAuth access token can also be retrieved by:
// authResult.credential.accessToken
// Twitter OAuth ID token can be retrieved by calling:
// authResult.credential.idToken
// Twitter OAuth secret can be retrieved by calling:
// authResult.credential.secret
}
}
}
}
登录视图中的按钮操作
Button(action: self.viewModel.twitterLogin){
SignInButton(imageName: "twitter", text: "Sign in with twitter")
}.frame(height: 50).buttonStyle(ButtonStyleSignIn())
启动查看代码
struct ContentView: View {
@State var show = false
@EnvironmentObject var viewModel : LoginViewModel // (/1)
var body: some View {
NavigationView{
ZStack
{
Color("colorPrimaryDark")
.edgesIgnoringSafeArea(.all)
VStack{
NavigationLink(destination: LoginView().environmentObject(viewModel), isActive: $show, label: {
Image("main_logo").renderingMode(.original).frame(width: 100, height: 100)
})
} .navigationBarHidden(true)
.navigationBarTitle(Text("Home"))
.edgesIgnoringSafeArea([.top, .bottom])
}
}//.preferredColorScheme(.dark) // white tint on status bar
.onAppear {
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
self.show.toggle()
}
}
}
}
这里我添加了LoginView代码
import SwiftUI
import Firebase
import FirebaseAuth
import FBSDKCoreKit
import FBSDKLoginKit
struct LoginView: View {
@EnvironmentObject var viewModel : LoginViewModel // (/1)
@State var show = true
var body: some View {
ZStack
{
Color("colorPrimaryDark")
.edgesIgnoringSafeArea(.all)
if self.viewModel.loginSuccess {
NavigationLink(destination: fromLoginNavigationTo(), isActive: self.$show, label: {
EmptyView()
})
}
VStack {
HStack {
Spacer()
Button(action: {
self.viewModel.signInAnonymously()
}){
TextViewBody(text: "Skip").padding()
}
}
VStack {
Button(action: self.viewModel.twitterLogin){
SignInButton(imageName: "twitter", text: "Sign in with twitter")
}.frame(height: 50).buttonStyle(ButtonStyleSignIn())
}.padding(.init(top: 0, leading: 32,bottom: 0, trailing: 32))
.navigationBarHidden(true)
.navigationBarTitle(Text("Home"))
.edgesIgnoringSafeArea([.top, .bottom])
}
}//.preferredColorScheme(.dark) // white tint on status bar
}
}
Xcode:12个测试版
设备:模拟器iOS 14