1

我正在使用更新的可达性库来测试互联网连接是否可达。如果无法访问互联网,我会尝试记录一条消息,但日志没有调试:

//Test the internet connection
Reachability* reach = [Reachability reachabilityForInternetConnection];
reach.unreachableBlock = ^(Reachability*reach)
{
    NSLog(@"Internet connexion unreachable");//Although Internet cnx is off, this message is not displayed
    return;
};

// start the notifier which will cause the reachability object to retain itself!
[reach startNotifier];

我误解了可达性库吗?互联网关闭时如何执行给定任务?谢谢。

PS:我的iPad只有wifi,没有3G服务。

4

3 回答 3

2

在 Swift 4 中使用 Alamofire 检查内部人员

import Foundation
import Alamofire

class Connectivity {

   class func isConnectedToInternet() ->Bool {
        return NetworkReachabilityManager()!.isReachable
   }
}

然后调用这个函数

if Connectivity.isConnectedToInternet() {
    print("Yes! internet is available.")
    // do some tasks..
}
于 2018-03-11T18:41:33.547 回答
0

实际上,您应该注册一个通知以接收更改的可达性:

[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];

在此处查看 Apple 示例:http: //developer.apple.com/library/ios/#samplecode/Reachability/Listings/Classes_ReachabilityAppDelegate_m.html#//apple_ref/doc/uid/DTS40007324-Classes_ReachabilityAppDelegate_m-DontLinkElementID_4

于 2013-02-07T18:10:25.790 回答
0

好的,所以我得到的最好方法是遵循 Apple 示例代码:

   //Test the internet connection
   Reachability* reach = [Reachability reachabilityForInternetConnection];

   NetworkStatus netStatus = [reach currentReachabilityStatus];
   //See if the reachable object status is "ReachableViaWifi"
if (netStatus!=ReachableViaWiFi) {
    //If not
    NSLog(@"wifi unavailable");
    //Alert the user about the Internet cnx
    WBErrorNoticeView *notice = [WBErrorNoticeView errorNoticeInView:self.view title:@"Network Error" message:@"Check your internet connection."];
    notice.sticky = NO;
    [notice show];
    return;//Exit the method
}
于 2013-02-07T20:34:12.770 回答