Swift app crashes if in airplane mode on start

时间:2015-09-01 21:18:59

标签: swift reachability

I used Reachability to check if the device has internet access or not.

The app will crash if I open the app and the Reachability status is kNOTREACHABLE. What is wrong?

AppDelegate.swift

import SystemConfiguration

let kREACHABLEWITHWIFI = "ReachableWithWIFI"
let kNOTREACHABLE = "NotReachable"
let kREACHABLEWITHWWAN = "ReachableWithWWAN"

var reachability: Reachability?
var reachabilityStatus = kREACHABLEWITHWIFI

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var internetReach: Reachability?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "reachabilityChanged:", name: kReachabilityChangedNotification, object: nil)
        internetReach = Reachability.reachabilityForInternetConnection()
        internetReach?.startNotifier()
        if internetReach != nil {
            self.statusChangeWithReachability(internetReach!)
        }

        //registering for sending user various kinds of notifications
        let settings = UIUserNotificationSettings(forTypes: [UIUserNotificationType.Sound, UIUserNotificationType.Alert, UIUserNotificationType.Badge], categories: nil)
        UIApplication.sharedApplication().registerUserNotificationSettings(settings)
        UIApplication.sharedApplication().beginReceivingRemoteControlEvents()

        // Override point for customization after application launch.
        return true

    }

    func reachabilityChanged(notification: NSNotification) {
        reachability = notification.object as? Reachability
        self.statusChangeWithReachability(reachability!)
    }

    func statusChangeWithReachability(currentReachablilityStatus: Reachability) {
        let networkStatus: NetworkStatus = currentReachablilityStatus.currentReachabilityStatus()

        if networkStatus.rawValue == NotReachable.rawValue {
            reachabilityStatus = kNOTREACHABLE
        } else if networkStatus.rawValue == ReachableViaWiFi.rawValue {
            reachabilityStatus = kREACHABLEWITHWIFI
        } else if networkStatus.rawValue == ReachableViaWWAN.rawValue {
            reachabilityStatus = kREACHABLEWITHWWAN
        }

        NSNotificationCenter.defaultCenter().postNotificationName("ReachStatusChanged", object: nil)
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
        NSNotificationCenter.defaultCenter().removeObserver(self, name: kReachabilityChangedNotification, object: nil)
    }
}

ListenLive.swift

override func viewDidLoad() {
    super.viewDidLoad()
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "reachabilityStatusChanged", name: "ReachStatusChanged", object: nil)
    self.reachabilityStatusChanged()
}

func reachabilityStatusChanged() {
    if reachabilityStatus == kNOTREACHABLE {
        print("NOT REACHABLE")
    } else if reachabilityStatus == kREACHABLEWITHWIFI {
        print("WIFI REACHABLE")
    } else if reachabilityStatus == kREACHABLEWITHWWAN {
        print("WWAN REACHABLE")
    }
}

deinit {
    NSNotificationCenter.defaultCenter().removeObserver(self, name: "ReachStatusChanged", object: nil)
}

0 个答案:

没有答案
相关问题