【发布时间】:2015-08-19 08:01:54
【问题描述】:
为了检查我的 iOS 应用中的互联网连接,我做了以下操作:
导入 SystemConfiguration.framework
在我的项目中添加来自 https://developer.apple.com/library/ios/samplecode/Reachability/Introduction/Intro.html 的 Reachability.h 和 Reachability.m
然后我在 ViewController.m 中添加了这个:
#import <SystemConfiguration/SystemConfiguration.h>
#import "Reachability.h"
和
- (BOOL)connected
{
Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus networkStatus = [reachability currentReachabilityStatus];
return !(networkStatus == NotReachable);
}
- (void)viewDidLoad {
[super viewDidLoad];
if (![self connected])
{
// not connected
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No internet connection!" message:@"Check internet connection!" delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil];
[alert show];
} else
{
}
}
我在应用中打开的每个视图(控制器)都会出现警报。那个神经。它应该只出现在一个视图(需要互联网连接)或只出现一次(启动应用程序或互联网连接中断时)。 (或任何其他想法?)
有没有办法做到这一点?
【问题讨论】:
-
为什么?您可以随时断开连接。检查您何时真正需要使用连接(和/或处理连接错误)。
-
我不太明白你的意思。 “检查您何时真正需要使用连接”:我该怎么做?
-
您可能正在继承您的 ViewController 类?然后摆脱警报,只将其添加到您想要的视图控制器中。
-
在
[alert show];添加断点,看看是谁调用的。
标签: ios connection