reachabilityWithAddress 在 Objective C 编程中不起作用
Posted
技术标签:
【中文标题】reachabilityWithAddress 在 Objective C 编程中不起作用【英文标题】:reachabilityWithAddress is not working in Objective C programming 【发布时间】:2016-06-10 07:28:11 【问题描述】:我想通过 ip 检查服务器是否处于活动状态,例如 74.125.71.104(Google 的 ip)
// 分配可达性对象
`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("74.125.71.104");`
Reachability *reach = [Reachability reachabilityWithAddress:&address];
但那些不工作。
当我更改为 reachabilityWithHostname
时,它正在工作。
【问题讨论】:
【参考方案1】:请导入#include <arpa/inet.h>
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
struct sockaddr_in address;
address.sin_len = sizeof(address);
address.sin_family = AF_INET;
address.sin_port = htons(8080);
address.sin_addr.s_addr = inet_addr("216.58.199.174"); //google ip
self.internetReachability = [Reachability reachabilityWithAddress:&address];
[self.internetReachability startNotifier];
[self updateInterfaceWithReachability:self.internetReachability];
编辑
根据您的 cmets,您的可达性块不会被调用。我总是使用不太了解可达性块的通知。所以我更喜欢使用如下通知。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
struct sockaddr_in address;
address.sin_len = sizeof(address);
address.sin_family = AF_INET;
address.sin_port = htons(8080);
address.sin_addr.s_addr = inet_addr("216.58.199.174");
self.internetReachability = [Reachability reachabilityWithAddress:&address];
[self.internetReachability startNotifier];
[self updateInterfaceWithReachability:self.internetReachability];
现在,当您的互联网状态发生变化时,reachabilityChanged 方法将由可达性实例触发:)
- (void) reachabilityChanged:(NSNotification *)note
Reachability* curReach = [note object];
[self updateInterfaceWithReachability:curReach];
最后实现 updateInterfaceWithReachability 为
- (void)updateInterfaceWithReachability:(Reachability *)reachability
NetworkStatus netStatus = [reachability currentReachabilityStatus];
switch (netStatus)
case NotReachable:
//not reachable
break;
case ReachableViaWWAN:
case ReachableViaWiFi:
//reachable via either 3g or wifi
break;
希望这会有所帮助。
【讨论】:
请注意,一些移动提供商已经取消了 iPv4 并通过 IPv6 对所有内容进行 nat,然后静态 IP 地址可能无法正常工作。 @rckoenes : Bingo :) 可达性 internetReachableFoo= [可达性可达性WithHostName:@"74.125.71.104"]; internetReachableFoo.reachableBlock = ^(Reachabilityreach) dispatch_async(dispatch_get_main_queue(), ^ NSLog(@"Yayyy, we have the interwebs!"); ); ; internetReachableFoo.unreachableBlock = ^(Reachability*reach) dispatch_async(dispatch_get_main_queue(), ^ NSLog(@"有人破坏了互联网 :("); ); ; [internetReachableFoo startNotifier]; 我的块永远不会被调用 正如你所说你使用委托模式,我应该遵守任何协议 @sowjanya-lankisetty:对不起,我用错了委托这个词:)它不是委托模式,它正在使用它的通知模式:)如果你看到我已经向 kReachabilityChangedNotification 添加了一个通知。每当网络状态发生变化时,您都会收到通知:)以上是关于reachabilityWithAddress 在 Objective C 编程中不起作用的主要内容,如果未能解决你的问题,请参考以下文章
NOIP 2015 & SDOI 2016 Round1 & CTSC 2016 & SDOI2016 Round2游记