1

I am currently using Ashley Mill's Reachability Class. If the application launches with network connectivity then I am able to toggle between connectivity availability without any issues and able to display a network connectivity Alert Controller properly. However if the application is launched when the app starts without internet connection/on airplane mode it abruptly crashes.

override func viewDidLoad()
{
    super.viewDidLoad()

    setUpReachability (nil)
}

func setUpReachability(hostName: String?)
{
    do
    {
        let  reachability = try hostName == nil ? Reachability.reachabilityForInternetConnection() : Reachability(hostname: hostName!)
        self.reachability = reachability
        try! self.reachability?.startNotifier()
    }
    catch ReachabilityError.FailedToCreateWithAddress(let address)
    {
        print("\(address)")
        return
    } catch {}

    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.reachabilityChanged(_:)), name: ReachabilityChangedNotification, object: reachability)
}

func reachabilityChanged(notification: NSNotification)
{
    let reachability = notification.object as! Reachability

    if  reachability.isReachable()
    {
        if reachability.isReachableViaWiFi()
        {
            connected = true
        }
        else
        {
            connected = true
        }
    }
    else
    {
        let alert = UIAlertController( title: "No Network Connection Available", message:"Try Again", preferredStyle: .Alert)
        alert.addAction(UIAlertAction( title: "Will Do!"  , style: .Default) { _ in } )
        presentViewController        ( alert, animated: true               ) {}
        connected = false
    }
}

What can be done to allow the iPhone application to launch and display an alert saying there is no network connection rather than abruptly crash?

Error Message:

fatal error: unexpectedly found nil while unwrapping an Optional value

But I would think that reachability changed would catch this in the else statement and pop the error message up?

enter image description here

Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
lifewithelliott
  • 1,012
  • 2
  • 16
  • 35
  • 3
    What is the crash message and stack trace? – Paulw11 Apr 08 '16 at 23:20
  • @Paulw11 The Message resulting is that it tried to unwrap a nil value - I am assuming maybe I just don't understand fully in detail the order of which the functions are getting called but The objective is to first check reachability then follow through with the remainder of the app launch. :) – lifewithelliott Apr 09 '16 at 22:27
  • 1
    You should set an exception breakpoint to determine which line the crash occurs on and what is nil. – Paulw11 Apr 09 '16 at 22:30
  • @Paulw11 What I am experiencing is: Line 51 is the closing bracket of my viewDidAppear: If I set a breakpoint there then the app does not crash. If i set the breakpoint on Line 52 on a line of whitespace then the app crashes with the error appearing. The first line in the viewDidLoad is setUpReachability method - what could be the cause of it not stopping the app from continuing and present the alert? – lifewithelliott Apr 09 '16 at 22:40
  • But have you set an exception breakpoint rather than a breakpoint in a line? Go the breakpoint navigator, add a breakpoint and select "exception" – Paulw11 Apr 09 '16 at 22:44
  • @Paulw11 My apologies if I lack competence with debugging! Definitely going to dive deeper into it right away! But I added an image of the result from adding an exception breakpoint - is this helpful in the debugging process? – lifewithelliott Apr 09 '16 at 23:02
  • No need to apologise. Learning to debug is a useful skill and just takes a bit of work. – Paulw11 Apr 09 '16 at 23:37
  • there are a few places in your code when the app could crash easily... follow the `!` marks one-by-one and you will get to your crash-site eventually. – holex Oct 01 '18 at 10:43

2 Answers2

0

Shouldn't the else in the reachability.isReachableViaWiFi() if statement be:connected = false ?

  • the else statement you are referring to is the reachability to cellular networks. So if the network connectivity is.reachable - how is it reachable via wifi else its cellular or if its not reachable then display the alert to the user :D – lifewithelliott Apr 10 '16 at 14:22
0

The error was that I was in fact trying to download data at the launch of the app instead of first allowing the initialization of the app to finish to then send a request to the server to access information.

lifewithelliott
  • 1,012
  • 2
  • 16
  • 35