Xcode 网络或Wi-Fi的Apple可达性通知
这是我在应用程序中使用的可访问性代码:Xcode 网络或Wi-Fi的Apple可达性通知,xcode,reachability,Xcode,Reachability,这是我在应用程序中使用的可访问性代码: - (void) handleNetworkChange:(NSNotification *)notice { NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus]; if(remoteHostStatus == NotReachable) { UIAlertView *alertnew = [[UIAlertView allo
- (void) handleNetworkChange:(NSNotification *)notice
{
NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];
if(remoteHostStatus == NotReachable) {
UIAlertView *alertnew = [[UIAlertView alloc] initWithTitle:@"No Internet Connection!" message:@"Your device lost internet connection!"
delegate:self
cancelButtonTitle:@"Close"
otherButtonTitles:@"Dismiss", nil];
[alertnew show];
} else if (remoteHostStatus == ReachableViaWWAN) {
//if connected to the Network
UIAlertView *alertre = [[UIAlertView alloc] initWithTitle:@"Internet Connection!" message:@"Your device re-connected to the internet!"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"Ok!", nil];
[alertre show];
} else if (remoteHostStatus == ReachableViaWiFi) {
//if connected to Wi-Fi
UIAlertView *alertre = [[UIAlertView alloc] initWithTitle:@"Internet Connection!" message:@"Your device re-connected to the internet!"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"Ok!", nil];
[alertre show];
}
}
我想提醒用户,当他们失去互联网,当他们重新获得它。但我遇到的问题是,当用户在iPhone上(保持网络连接)并连接到Wi-Fi时,他们会在已经连接的情况下收到警报。因此,我只想在他们没有互联网连接和Wi-Fi连接的情况下提醒他们。这段代码适用于没有数据计划的iPod和iPad,但问题出在iPhone上。我可以做些什么来编辑它,使它只显示一个或另一个警报
我在上一个警报中尝试了以下代码:
else if (remoteHostStatus == ReachableViaWiFi && !(remoteHostStatus == ReachableViaWWAN)) {
//if connected to Wi-Fi but without Network
UIAlertView *alertre = [[UIAlertView alloc] initWithTitle:@"Internet Connection!" message:@"Your device re-connected to the internet!"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"Ok!", nil];
[alertre show];
}
但这并没有解决问题…代码逻辑中的基本错误是将三个独立的状态视为两个状态<代码>网络状态在苹果的
可达性
示例中,由枚举定义,如下所示:
typedef enum {
NotReachable = 0,
ReachableViaWiFi,
ReachableViaWWAN
} NetworkStatus;
因为您基本上需要一个BOOL
(可访问或不可访问)。只需将当前网络状态与NotReachable
进行比较即可。因此,在可通过WiFi访问
或可通过WWAN访问
的情况下,BOOL
为YES
BOOL reachable = (_reachability.currentReachabilityStatus != NotReachable); // underbar added for good ivar style.
现在,你只需要处理你有WiFi和移动到蜂窝或反之亦然的情况。在这种情况下,您必须跟踪呼叫之间的状态。添加一个名为\u可访问的BOOL
ivar。然后比较观察者方法中的新值
-(void)reachabilityDidChange:(NSNotification *)note{
BOOL reachable = (_reachability.currentReachabilityStatus != NotReachable);
if (reachable != _reachable){
// State Changed. Inform the user if necessary.
if (reachable){
// Connected.
} else {
// Lost connection.
}
}
_reachable = reachable;
}
警告:下面是未经请求的建议
关于这些UIAlertView
s的概念,最后一点是一般的。警报本质上是在向用户尖叫。重新连接的警报实质上是大叫“嘿!!!现在一切都好了!”。我想推荐大家观看WWDC2012的第221次会议,他们在会上谈到了常见的设计陷阱。大约35分钟后,他们开始谈论警报。很明显,他们正试图引导开发人员尽可能多地使用警报 感谢您提供的所有信息,现在您谈到AlertView,我同意您的看法。再次感谢!谢谢你的代码,但是我可以解释一下把所有代码放在哪里(我也在尝试做@tdun正在做的事情)…@Programmer。。。在这里查看我的个人资料,通过我的网站向我发送电子邮件,我可以帮助你。我可以在Stackoverflow中写我的问题,还是希望我使用你的网站