Created
January 7, 2020 02:40
-
-
Save wata/0297a772fcba63c9d5544c1a04df8950 to your computer and use it in GitHub Desktop.
Add sign-in to SwiftUI iOS app with FirebaseUI.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
import FirebaseUI | |
struct SignInView: View { | |
@State private var user: FirebaseAuth.User? | |
var body: some View { | |
SignInViewController(user: $user) | |
} | |
} | |
struct SignInView_Previews: PreviewProvider { | |
static var previews: some View { | |
SignInView() | |
} | |
} | |
struct SignInViewController: UIViewControllerRepresentable { | |
@Binding var user: FirebaseAuth.User? | |
func makeCoordinator() -> Coordinator { | |
Coordinator(self) | |
} | |
func makeUIViewController(context: Context) -> _SignInViewController { | |
return _SignInViewController.makeInstance(delegate: context.coordinator) | |
} | |
func updateUIViewController(_ uiViewController: _SignInViewController, context: Context) {} | |
class Coordinator: NSObject, FUIAuthDelegate { | |
var parent: SignInViewController | |
init(_ viewController: SignInViewController) { | |
self.parent = viewController | |
} | |
func authUI(_ authUI: FUIAuth, didSignInWith authDataResult: AuthDataResult?, error: Error?) { | |
parent.user = authDataResult?.user | |
} | |
} | |
} | |
final class _SignInViewController: UIViewController { | |
private let authUI = FUIAuth.defaultAuthUI()! | |
static func makeInstance(delegate: FUIAuthDelegate) -> _SignInViewController { | |
let vc = R.storyboard.signIn.signInViewController()! | |
vc.authUI.delegate = delegate | |
return vc | |
} | |
@IBAction func didTapSignInButton(_ sender: Any) { | |
authUI.providers = [FUIPhoneAuth(authUI: authUI)] | |
let provider = authUI.providers.first as! FUIPhoneAuth | |
provider.signIn(withPresenting: self, phoneNumber: nil) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
let vc = R.storyboard.signIn.signInViewController()!
I am new to Swift and apple dev. What is R? I created a swift view with your code but Xcode does not know what R is and either do I. A little help please!