#質問したいこと
Firebaseを使ったアプリを作成していますが
ビルドしても、画面が真っ暗になってしまいます。
#コード
######一番最初の画面
swift
1import UIKit 2import Firebase 3import FirebaseAuth 4 5class AccountViewController: UIViewController, UITextFieldDelegate { 6 var account: FirebaseApp! 7 8 @IBOutlet private weak var nameTextField: UITextField! 9 @IBOutlet private weak var emailTextField: UITextField! 10 @IBOutlet private weak var passwordTextField: UITextField! 11 12 @IBAction private func didTapSignUpButton() { 13 let email = emailTextField.text ?? "" 14 let password = passwordTextField.text ?? "" 15 let name = nameTextField.text ?? "" 16 17 Auth.auth().createUser(withEmail: email, password: password) { [weak self] result, error in 18 guard let self = self else { return } 19 if let user = result?.user { 20 let req = user.createProfileChangeRequest() 21 req.displayName = name 22 req.commitChanges() { [weak self] error in 23 guard let self = self else { return } 24 if error == nil { 25 user.sendEmailVerification() { [weak self] error in 26 guard let self = self else { return } 27 if error == nil { 28 // 仮登録完了画面へ遷移する処理 29 // サインアップ完了のフラグを保持する 30 UserDefaults.standard.set(true, forKey: "appSignUpStatusKey") 31 UserDefaults.standard.synchronize() 32 } 33 self.showErrorIfNeeded(error) 34 } 35 } 36 self.showErrorIfNeeded(error) 37 } 38 } 39 self.showErrorIfNeeded(error) 40 } 41 // サインアップ完了のフラグを保持する 42 UserDefaults.standard.set(true, forKey: "appSignUpStatusKey") 43 // ユーザー名を保存する 44 UserDefaults.standard.set(name, forKey: "userNameKey") 45 UserDefaults.standard.synchronize() 46 if (email == "" || password == "" || name == "") { 47 let message = "全てのフォームに記入して下さい。" 48 let alert = UIAlertController(title: nil, message: message, preferredStyle: .alert) 49 alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil)) 50 self.present(alert, animated: true, completion: nil) 51 } 52 } 53 54 55 private func showErrorIfNeeded(_ errorOrNil: Error?) { 56 // エラーがなければ何もしません 57 guard let error = errorOrNil else { return } 58 let message = "エラーが起きました" // ここは後述しますが、とりあえず固定文字列 59 let alert = UIAlertController(title: nil, message: message, preferredStyle: .alert) 60 alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil)) 61 present(alert, animated: true, completion: nil) 62 } 63 64 65 66 67 // AccountViewControllerの想定です 68 override func viewDidLoad() { 69 super.viewDidLoad() 70 self.navigationController?.navigationBar.isHidden = true 71 72 self.nameTextField.delegate = self 73 self.emailTextField.delegate = self 74 self.passwordTextField.delegate = self 75 76 77 } 78 79 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 80 self.view.endEditing(true) 81 } 82} 83
######AppDelegate
swift
1import UIKit 2import CoreData 3import Firebase 4 5@UIApplicationMain 6class AppDelegate: UIResponder, UIApplicationDelegate { 7 8 9 10 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { 11 // Override point for customization after application launch. 12 FirebaseApp.configure() 13 return true 14 } 15 16 // MARK: UISceneSession Lifecycle 17 18 func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration { 19 // Called when a new scene session is being created. 20 // Use this method to select a configuration to create the new scene with. 21 return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role) 22 } 23 24 func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) { 25 // Called when the user discards a scene session. 26 // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions. 27 // Use this method to release any resources that were specific to the discarded scenes, as they will not return. 28 } 29 30 // MARK: - Core Data stack 31 32 lazy var persistentContainer: NSPersistentContainer = { 33 /* 34 The persistent container for the application. This implementation 35 creates and returns a container, having loaded the store for the 36 application to it. This property is optional since there are legitimate 37 error conditions that could cause the creation of the store to fail. 38 */ 39 let container = NSPersistentContainer(name: "new_timer3") 40 container.loadPersistentStores(completionHandler: { (storeDescription, error) in 41 if let error = error as NSError? { 42 // Replace this implementation with code to handle the error appropriately. 43 // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 44 45 /* 46 Typical reasons for an error here include: 47 * The parent directory does not exist, cannot be created, or disallows writing. 48 * The persistent store is not accessible, due to permissions or data protection when the device is locked. 49 * The device is out of space. 50 * The store could not be migrated to the current model version. 51 Check the error message to determine what the actual problem was. 52 */ 53 fatalError("Unresolved error (error), (error.userInfo)") 54 } 55 }) 56 return container 57 }() 58 59 // MARK: - Core Data Saving support 60 61 func saveContext () { 62 let context = persistentContainer.viewContext 63 if context.hasChanges { 64 do { 65 try context.save() 66 } catch { 67 // Replace this implementation with code to handle the error appropriately. 68 // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 69 let nserror = error as NSError 70 fatalError("Unresolved error (nserror), (nserror.userInfo)") 71 } 72 } 73 } 74 75} 76 77
#エラー
swift
12019-10-14 12:36:34.421069+0900 new_timer3[33996:1789078] - <AppMeasurement>[I-ACS036002] Analytics screen reporting is enabled. Call +[FIRAnalytics setScreenName:setScreenClass:] to set the screen name or override the default screen class name. To disable screen reporting, set the flag FirebaseScreenReportingEnabled to NO (boolean) in the Info.plist 22019-10-14 12:36:34.605127+0900 new_timer3[33996:1789084] 6.10.0 - [Firebase/Analytics][I-ACS023007] Analytics v.60103000 started 32019-10-14 12:36:34.606302+0900 new_timer3[33996:1789084] 6.10.0 - [Firebase/Analytics][I-ACS023008] To enable debug logging set the following application argument: -FIRAnalyticsDebugEnabled (see http://goo.gl/RfcP7r)
質問とは関係のないことですが、
let email = emailTextField.text ?? ""
let password = passwordTextField.text ?? ""
let name = nameTextField.text ?? ""
は無駄が多いかと、
各 textFieldがnilの時用に ?? ""として初期値をわざわざ与えているかと思いますが、
そもそもnilの時はユーザーを作成する試みをする必要はないので、
その時点で、エラーを表示し、終了とすればいいと思います。
guard let email = emailTextField.text, let xxx = xxxtextField.text, let xxx = xxtextField.text else {
showAlert()
return
}
とすれば、なぜか後ろの方にあるif (email == "" || password == "" || .... ) { ... }
のブロックは全て不要になりますよ。
わざわざありがとうございます!!
とっても役に立ちました!!
本当にありがとうございます????
回答1件
あなたの回答
tips
プレビュー