IP可达性等级

发布于 2024-12-20 05:22:24 字数 1702 浏览 3 评论 0原文

我修改了苹果的可达性类以与我的服务器 IP 一起使用。但是,当我使用 reachabilityWithAddress 时,应用程序启动时不会调用 reachabilityChanged 。这称为仅互联网连接状态发生变化。 (就像关闭、打开 Wi-Fi 一样)但是,如果我使用 reachabilityWithHostName,则在应用程序启动时调用 reachabilityChanged 函数。

我缺少什么?

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    ///////////////////////////////////////////////////////////////////////////////////
    // Reachability Local Notifications 
    ///////////////////////////////////////////////////////////////////////////////////
    hasInternetConnection = NO;
    struct sockaddr_in address;
    address.sin_len = sizeof(address);
    address.sin_family = AF_INET;
    address.sin_port = htons(80);
    address.sin_addr.s_addr = inet_addr("X.X.X.X");
    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name:kReachabilityChangedNotification object: nil];
    hostReach = [Reachability reachabilityWithAddress:&address];
    [hostReach startNotifier];

    ...

}

然后在方法中:

-(void)reachabilityChanged:(NSNotification*)note
   {
    Reachability* curReach = [note object];
    NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
    if ( curReach == hostReach ) {
        NetworkStatus netStatus = [curReach currentReachabilityStatus]; 
        if ( netStatus != ReachableViaWiFi && netStatus != ReachableViaWWAN ) {
            hasInternetConnection = NO;
        }
        else {
            hasInternetConnection = YES;
        }
    }
    else {
        DLog(@"Something go wrong!");
    }
}

I modified apple's reachability class to use with my server ip. But when I use reachabilityWithAddress it's not called reachabilityChanged while app is launching. It is called only internet connection status changed. ( like turning wi-fi off, on ) But, if I use reachabilityWithHostName, reachabilityChanged function called when app is launching.

What am I missing?

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    ///////////////////////////////////////////////////////////////////////////////////
    // Reachability Local Notifications 
    ///////////////////////////////////////////////////////////////////////////////////
    hasInternetConnection = NO;
    struct sockaddr_in address;
    address.sin_len = sizeof(address);
    address.sin_family = AF_INET;
    address.sin_port = htons(80);
    address.sin_addr.s_addr = inet_addr("X.X.X.X");
    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name:kReachabilityChangedNotification object: nil];
    hostReach = [Reachability reachabilityWithAddress:&address];
    [hostReach startNotifier];

    ...

}

Then in method:

-(void)reachabilityChanged:(NSNotification*)note
   {
    Reachability* curReach = [note object];
    NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
    if ( curReach == hostReach ) {
        NetworkStatus netStatus = [curReach currentReachabilityStatus]; 
        if ( netStatus != ReachableViaWiFi && netStatus != ReachableViaWWAN ) {
            hasInternetConnection = NO;
        }
        else {
            hasInternetConnection = YES;
        }
    }
    else {
        DLog(@"Something go wrong!");
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猥︴琐丶欲为 2024-12-27 05:22:24

使用 reachabilityWithHostName: 选择器时,您会在初始化 Reachability 后不久收到通知,因为在解析该名称之前,主机名的可达性是未知的。对于 reachabilityWithAddress:,可达性状态已知,因此无需报告任何更改。

我通过将以下三行添加到 reachabilityWithAddress: 函数中最内部的“if”块来解决这个问题。

SCNetworkReachabilityFlags flags;
SCNetworkReachabilityGetFlags(reachability, &flags);
ReachabilityCallback(reachability, flags, retVal);

这将调用与可达性实际发生更改时所调用的回调函数相同的回调函数。因此,如果您像我一样完全依赖于通知,这将导致通知在初始化可达性后发布。

When using the reachabilityWithHostName: selector you get a notification soon after initializing Reachability b/c the reachability of the host name is not known until that name is resolved. For reachabilityWithAddress:, the reachability status is already known so there's no change to report.

I got around the problem by adding the following three lines to the inner most "if" block in the reachabilityWithAddress: function.

SCNetworkReachabilityFlags flags;
SCNetworkReachabilityGetFlags(reachability, &flags);
ReachabilityCallback(reachability, flags, retVal);

This calls the same callback function that would be called if reachability had actually changed. So if you're completely depending on the notification, like I am, this will cause the notification to post after initializing reachability.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文