Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. import UIKit
  2. import Firebase
  3. import FirebaseAuth
  4. import GoogleSignIn
  5.  
  6. class LoginViewController: UIViewController, UITextFieldDelegate,GIDSignInDelegate, GIDSignInUIDelegate {
  7.  
  8. @IBOutlet var mailField: UITextField!
  9. @IBOutlet var passwdField: UITextField!
  10.  
  11. @IBOutlet weak var googleSignIn: GIDSignInButton!
  12.  
  13. override func viewDidLoad() {
  14. super.viewDidLoad()
  15.  
  16. mailField.delegate = self
  17. passwdField.delegate = self
  18.  
  19. GIDSignIn.sharedInstance().clientID = FIRApp.defaultApp()?.options.clientID
  20. GIDSignIn.sharedInstance().delegate = self
  21. GIDSignIn.sharedInstance().uiDelegate = self
  22. }
  23.  
  24. @IBAction func googleSignInClicked(sender: AnyObject) {
  25. GIDSignIn.sharedInstance().signIn()
  26. }
  27.  
  28.  
  29. func signIn(signIn: GIDSignIn!, didSignInForUser user: GIDGoogleUser!,
  30. withError error: NSError!) {
  31. print("Google Sing In didSignInForUser")
  32. if let error = error {
  33. print(error.localizedDescription)
  34. return
  35. }
  36. let authentication = user.authentication
  37. let credential = FIRGoogleAuthProvider.credential(withIDToken: (authentication?.idToken)!,accessToken: (authentication?.accessToken)!)
  38. //ユーザ登録後の処理....
  39.  
  40. }
  41.  
  42. func signIn(signIn: GIDSignIn!, didDisconnectWithUser user:GIDGoogleUser!,
  43. withError error: NSError!) {
  44. print("Google Sing In didDisconnectWithUser")
  45. // Perform any operations when the user disconnects from app here.
  46. // ...
  47. }
  48.  
  49. override func didReceiveMemoryWarning() {
  50. super.didReceiveMemoryWarning()
  51. // Dispose of any resources that can be recreated.
  52. }
  53.  
  54. @objc(textFieldShouldReturn:) func textFieldShouldReturn(_ mailField: UITextField) -> Bool{
  55. mailField.resignFirstResponder()
  56. return true
  57. }
  58.  
  59. func passwdFieldShouldReturn(_ passwdField: UITextField) -> Bool{
  60. passwdField.resignFirstResponder()
  61. return true
  62. }
  63.  
  64.  
  65. @IBAction func loginBtn(_ sender: Any) {
  66. var mailAdress = mailField.text
  67. var password = passwdField.text
  68.  
  69. //ワーニングが出るため別の方法を使用したほうがいい可能性有り
  70.  
  71. FIRAuth.auth()?.signIn(withEmail: mailAdress!, password: password!) { (user, error) in
  72. if let error = error {
  73. print("サインインできません (error)")
  74. let alert: UIAlertController = UIAlertController(title: "エラー", message: "メールアドレスまたはパスワードが間違っています", preferredStyle: UIAlertControllerStyle.alert)
  75. let defaultAction: UIAlertAction = UIAlertAction(title: "リトライ", style: UIAlertActionStyle.default, handler:{
  76. (action: UIAlertAction!) -> Void in
  77. //処理部分だが、そのままAlertを閉じさせればいいためコードなし
  78.  
  79. })
  80. alert.addAction(defaultAction)
  81. self.present(alert, animated: true, completion: nil)
  82. return
  83. //ログイン失敗時の処理
  84. }
  85.  
  86. if let user = user {
  87. print("user : (user.email!) サインインできました")
  88. let storyboard: UIStoryboard = self.storyboard!
  89. let nextView = storyboard.instantiateViewController(withIdentifier: "joined") as!JoinedViewController
  90. self.present(nextView, animated: true, completion: nil)
  91. //ログイン成功時の処理
  92. }
  93. }
  94.  
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement