IP可达性等级
我修改了苹果的可达性类以与我的服务器 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 reachabilityWithHostName: 选择器时,您会在初始化 Reachability 后不久收到通知,因为在解析该名称之前,主机名的可达性是未知的。对于 reachabilityWithAddress:,可达性状态已知,因此无需报告任何更改。
我通过将以下三行添加到 reachabilityWithAddress: 函数中最内部的“if”块来解决这个问题。
这将调用与可达性实际发生更改时所调用的回调函数相同的回调函数。因此,如果您像我一样完全依赖于通知,这将导致通知在初始化可达性后发布。
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.
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.