检查Swift中的网络状态,不会调用observer

时间:2014-07-02 07:29:35

标签: swift

我已将 Reachability.h .m 文件(例如写入here)导入到我的Swift项目中,但是观察者/事件处理程序在启动后不会被调用,为什么?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "reachabilityChanged:", name: kReachabilityChangedNotification, object: nil)

    let hostReachability = Reachability(hostName: "www.apple.com");
    hostReachability.startNotifier();

    return true
}

func reachabilityChanged(note: NSNotification) { // <- DOES NOT GET CALLED

    let reachability: Reachability = note.object as Reachability;

    if(reachability.currentReachabilityStatus() != .NotReachable) {

    }
}

3 个答案:

答案 0 :(得分:11)

返回的Reachability对象是一个自动释放的对象,因此它已取消分配,因此通知不会触发。您可以通过对其进行强有力的引用来解决问题。

创建一个属性来保存Reachability对象

var reachability:Reachability?

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
        // Override point for customization after application launch.

        NSNotificationCenter.defaultCenter().addObserver(self, selector: "reachabilityChanged:", name:kReachabilityChangedNotification, object: nil)

        reachability = Reachability(hostName: "www.apple.com");
        reachability?.startNotifier();

        return true
    }

这将解决您的问题。

观察者的名称从ReachabilityChangedNotification更改为kReachabilityChangedNotification。注意&#34; k&#34;

答案 1 :(得分:0)

如其他答案中所述,您需要维护对Reachability对象的强引用。如果您没有检查特定主机,请调用reachabilityForInternetConnection:

let internetReachability = Reachability.reachabilityForInternetConnection()

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    // Override point for customization after application launch.

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "reachabilityChanged:", name: kReachabilityChangedNotification, object: internetReachability)
    internetReachability.startNotifier()
    let hasInternet = internetReachability.currentReachabilityStatus().value == 0 ? false : true

    // Save to user defaults, log to console, etc.

    return true
}

func reachabilityChanged(note: NSNotification) {
    var hasInternet = Bool()
    if let reachability = note.object as? Reachability {
        hasInternet = internetReachability.currentReachabilityStatus().value == 0 ? false : true
    }

    // Update user defaults, log to console, show error message, etc.
}

答案 2 :(得分:0)

对于iOS 3+,请使用以下代码:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    NotificationCenter.default.addObserver(self, selector: #selector(checkForReachability(notification:)), name: NSNotification.Name.reachabilityChanged, object: nil)
    reachability = Reachability.forInternetConnection()
    reachability.startNotifier()

    return true
}

 // MARK:
// MARK: Check Internet Status
func checkForReachability(notification:NSNotification)
{
    let networkReachability = notification.object as! Reachability;
    let remoteHostStatus = networkReachability.currentReachabilityStatus()

    if (remoteHostStatus == NotReachable)
    {
        print("Not Reachable")
    }
    else
    {
        print("Reachable")
    }
}