2

Using: XCode 7, iPhone 5 (iOS 8.3), WiFi, Reachability (check the internet connection). While my app is in background and I click to open my app it checks the conncetion and load some functions and in 1 of the functions I try to sign:

imageView.image = UIImage(named: "imagename")

error: fatal error: unexpectedly found nil while unwrapping an Optional value

This happens only when my app change an IBOutlet value in applicationWillEnterForeground with function to primary view controller self.viewController.functionName()

class AppDelegate: UIResponder, UIApplicationDelegate {
     var viewController:ViewController = ViewController()
     func applicationWillEnterForeground(application: UIApplication) {
        self.viewController.checkConn()
    }
}

checkConn() check the connection with Reachability and change IBOutlets values like .image and .text

Is there any way to fix it?

Bogdan Bogdanov
  • 882
  • 11
  • 36
  • 79
  • You should secure your code like that : `if let image = UIImage(named : "imagename") { imageView.image = image }`. But still, can you show us the whole code of your AppDelegate ? – Randy Jul 07 '15 at 14:25
  • @Randy no need to do this unless you want to know if it fails to load image or not.`UIImage(named:)` is failable initializer and if it returns `None` (image not found), it shouldn't crash, because type of `image` property of `UIImageView` is `UIImage?`. You sure it crashes on this line? – zrzka Jul 07 '15 at 14:26
  • yes and I deleted it and crash on my next line which is UILabel.. I isolate the problem with userDefaults when It comes to background and return back to foreground to prevent casting and it works but when You continue your actions to the app it crashes... It crashes when I try to sign IBOutlets - imageView.image and label.text – Bogdan Bogdanov Jul 07 '15 at 14:35
  • Do you reference `imageView.image!` anywhere? – Aaron Brager Jul 07 '15 at 14:38
  • no I tried with ! but XCode gives me an error and I can't compile – Bogdan Bogdanov Jul 07 '15 at 14:39
  • can you post the code for "functionName()"? – the_pantless_coder Jul 07 '15 at 23:59
  • the code changes language bgLang() - Bulgarian enLang() - English and chage the values .text , .image – Bogdan Bogdanov Jul 08 '15 at 11:23

2 Answers2

2

After a lot of tests I found this method which works great:

class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    var viewController:ViewController?
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
        viewController = self.window!.rootViewController as? ViewController
        return true
    }
    func applicationWillEnterForeground(application: UIApplication) {
        viewController!.checkConn()
    }
}
Bogdan Bogdanov
  • 882
  • 11
  • 36
  • 79
  • Glad you figured it out. Just one thing, why not just call the "checkConn()" function from your root controller in the "viewDidLoad" or "viewWillAppear"? – the_pantless_coder Jul 08 '15 at 21:16
  • because I need to check if user stops his internet connection and came in application again thats why I have to trigger the function in applicationWillEnterForeground method – Bogdan Bogdanov Jul 09 '15 at 00:23
  • I am pretty sure it will work using the "viewWillAppear". One more thing, if your app is reliant on internet access. Do have a way to check if the users internet drops out while your app is running in the foreground. What I am saying .. are you also checking that internet is connected before you try to access it in your functions? – the_pantless_coder Jul 09 '15 at 00:36
  • I don't access internet in my functions. I made this to work only with internet connection for loading iAds and If you don't have internet connection to load iAds you can't use the app :D Everything is fine thank you :) – Bogdan Bogdanov Jul 09 '15 at 01:30
0

I am assuming that this is where your crash is happening.

imageView.image = UIImage(named: "imagename")

If you are using "Images.xcassets" to manage your image files. Make sure that "imagename" exists.

the_pantless_coder
  • 2,297
  • 1
  • 17
  • 30
  • it exists.. everythings works, but when I execute something in applicationWillEnterForeground and I tried to change values like .text of Label and .image of ImageView it crash... – Bogdan Bogdanov Jul 08 '15 at 11:24