Mau*_*imo 10 iphone objective-c reachability nsnotificationcenter ios
目前我正在通过apple reachability.m/.h使用该类,它可以工作,除了它通知我任何更改,我想只通知用户网络是否无法访问.目前,如果我有互联网连接,然后松散网络它告诉我.但是当你重新连接到网络时,它也告诉我,我不想要.我希望它只告诉我什么时候有丢失/没有网络.
我认为这与电话有关:
- (void)viewWillAppear:(BOOL)animated
{
// check for internet connection
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(checkNetworkStatus:)
name:kReachabilityChangedNotification
object:nil];
internetReachable = [[Reachability
reachabilityForInternetConnection] retain];
[internetReachable startNotifier];
// check if a pathway to a random host exists
hostReachable = [[Reachability reachabilityWithHostName:
@"www.google.ca"] retain];
[hostReachable startNotifier];
// now patiently wait for the notification
}
Run Code Online (Sandbox Code Playgroud)
在调用时-[NSNotificationCenter addObserver:selector:name:object:],该名称是否具有任何其他功能,然后字面上是一个名称?这是我第一次使用NSNotificationCenter,所以我不太熟悉这个问题.
编辑:
这是我的checkNetworkStatus函数:(问题是我得到"NotReachable",因为网络连接回来了,NSAlert多次关闭)
- (void) checkNetworkStatus:(NSNotification *)notice
{
// called after network status changes
NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
switch (internetStatus)
{
case NotReachable:
{
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Network Failed" message:@"Please check your connection and try again." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil ];
[alert show];
NSLog(@"The internet is down.");
break;
}
case ReachableViaWiFi:
{
NSLog(@"The internet is working via WIFI.");
break;
}
case ReachableViaWWAN:
{
NSLog(@"The internet is working via WWAN.");
break;
}
}
NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
switch (hostStatus)
{
case NotReachable:
{
NSLog(@"A gateway to the host server is down.");
break;
}
case ReachableViaWiFi:
{
NSLog(@"A gateway to the host server is working via WIFI.");
break;
}
case ReachableViaWWAN:
{
NSLog(@"A gateway to the host server is working via WWAN.");
break;
}
}
Run Code Online (Sandbox Code Playgroud)
}
可更改性将在状态发生变化时发送通知,但您对该通知的处理完全取决于您.如果您不想告诉用户网络已恢复,则您不必这样做.
NSNotificationCenter方法中的"name"参数指示您要订阅的通知.当对象发布通知时,它会使用特定名称.
| 归档时间: |
|
| 查看次数: |
34969 次 |
| 最近记录: |