6

I have an ios app which works fine with normal and facebook login. Today I integrated firebase and when I write FirebaseApp.configure() in app delegate, my app crashes and this function is highlighted with error code "unexpectedly found nil while unwrapping an optional value":

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {
        return FBSDKApplicationDelegate.sharedInstance().application(app, open: url, sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as! String, annotation: options[UIApplicationOpenURLOptionsKey.annotation])

    }

Code for AppDelegate class is:

//
//  AppDelegate.swift
//  
//
//  Created by Macbook on 01/01/2018.
//  Copyright © 2018 Macbo0k. All rights reserved.
//

import UIKit
import IQKeyboardManagerSwift
import FBSDKCoreKit
import FBSDKLoginKit
import Firebase
import UserNotifications
import FirebaseInstanceID
import FirebaseMessaging

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        let usd = UserDefaults.standard


        if usd.object(forKey: "loginstatus") != nil && (usd.value(forKey: "loginstatus") != nil)  {
            if usd.value(forKey: "loginstatus") as! Bool != false{
                self.window = UIWindow(frame: UIScreen.main.bounds)

                let storyboard = UIStoryboard(name: "Main", bundle: nil)

                let initialViewController = storyboard.instantiateViewController(withIdentifier: "tabbarvc")

                self.window?.rootViewController = initialViewController
                self.window?.makeKeyAndVisible()
            }


        }
        FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)



        let newFont = UIFont(name: "BebasNeueLight", size: 20.0)!
        let color = UIColor(red: 255,green:204, blue:102)

        UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor: color,
                                                            NSAttributedStringKey.font: UIFont(name: "BebasNeueRegular", size: 20)!
        ]



        UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.classForCoder() as! UIAppearanceContainer.Type]).setTitleTextAttributes([NSAttributedStringKey.foregroundColor: color, NSAttributedStringKey.font: newFont], for: .normal)
        UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.classForCoder() as! UIAppearanceContainer.Type]).setTitleTextAttributes([NSAttributedStringKey.foregroundColor: color, NSAttributedStringKey.font: newFont], for: .highlighted)

        //print("fonts are: \(UIFont.familyNames)")
        IQKeyboardManager.sharedManager().enable = true

        FirebaseApp.configure()
        return true
    }

    func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {

        let isFacebookURL = url.scheme != nil && url.scheme!.hasPrefix("fb\(FBSDKSettings.appID())") && url.host == "authorize"
        if isFacebookURL {
            return FBSDKApplicationDelegate.sharedInstance().application(application, open: url as URL!, sourceApplication: sourceApplication, annotation: annotation)
        }
        return false
    }

    func applicationWillResignActive(_ application: UIApplication) {

    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        print("app is in background mode")
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
        FBSDKAppEvents.activateApp()

    }

    func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {
        return FBSDKApplicationDelegate.sharedInstance().application(app, open: url, sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as! String, annotation: options[UIApplicationOpenURLOptionsKey.annotation])

    }
    func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

and log is:

2018-02-13 14:50:30.070051+0500 Sneakersapp[11075:119374] [MC] Reading from private effective user settings.
2018-02-13 14:50:30.113172+0500 Sneakersapp[11075:119374] Could not successfully update network info during initialization.
2018-02-13 14:50:30.824700+0500 Sneakersapp[11075:119470] TIC TCP Conn Failed [6:0x600000169480]: 12:8 Err(-65554)
2018-02-13 14:50:30.827309+0500 Sneakersapp[11075:119444] Task <D3E060F1-6F56-451A-B00C-CD3CFF86CD8F>.<2> HTTP load failed (error code: -1003 [12:8])
2018-02-13 14:50:30.827738+0500 Sneakersapp[11075:119444] Task <D3E060F1-6F56-451A-B00C-CD3CFF86CD8F>.<2> finished with error - code: -1003
2018-02-13 14:50:31.064485+0500 Sneakersapp[11075:119470] TIC Read Status [3:0x0]: 1:57
2018-02-13 14:50:31.064751+0500 Sneakersapp[11075:119470] TIC Read Status [3:0x0]: 1:57
2018-02-13 14:50:31.198410+0500 Sneakersapp[11075:119444] TIC Read Status [1:0x0]: 1:57
2018-02-13 14:50:31.199036+0500 Sneakersapp[11075:119444] TIC Read Status [1:0x0]: 1:57
2018-02-13 14:50:31.808151+0500 Sneakersapp[11075:119469] TIC Read Status [4:0x0]: 1:57
2018-02-13 14:50:31.808292+0500 Sneakersapp[11075:119469] TIC Read Status [4:0x0]: 1:57
(lldb) 

App works fine once i comment the FirebaseApp.configure() from didfinishLaunchingWithOptions function.

Please help.

2
  • try to add exceptional breakpoint and find where is your app actually crash... Feb 13, 2018 at 10:17
  • Possible you need to write Firebase.configure() statement in top of method. Feb 13, 2018 at 10:19

2 Answers 2

4

I know it may be too late to answer this, but I had the same issue and noticed that I forgot to import GoogleService-Info.plist on the main directory of the app.

2
  • Could you describe your solution in more detail? Thanks.
    – CKE
    Aug 17, 2018 at 5:06
  • If you take a look at linke The last step is to "Download a GoogleService-Info.plist file from Firebase console and include it in your app. Note: If you have multiple bundle IDs in your project, each bundle ID must be connected in Firebase console so it can have its own GoogleService-Info.plist file." So basically you shouldn't forget to put the right plist file inside your project. Aug 18, 2018 at 7:39
0

This crash comes when you haven't configure your app with Firebase properly.

Have you register your Project on Firebase? Please find link :- https://console.firebase.google.com/?pli=1

In Project setting, After register your app with firebase you would receive a Google Info plist . download it and import it in to your project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.