仅在 iOS 应用程序中检查一次互联网连接
Check internet connection in iOS app only once
为了在我的 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 创建一个自己的 HomeViewController.h/HomeViewController.m 文件解决了这个问题。我在那里添加了 alert-if-no-internet-connection 代码。
而不是 UIAlertView
,你应该输入一个布尔值,如果你有互联网连接则为真,如果你没有互联网连接则为假,当你想发出请求时,检查布尔值并显示警报或做你想做的。
如果您只想在 ViewController
第一次打开时执行一次,您可以这样做
- (BOOL)connected
{
Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus networkStatus = [reachability currentReachabilityStatus];
return !(networkStatus == NotReachable);
}
- (void)viewDidLoad {
[super viewDidLoad];
if (![self connected])
{
if(hasPresentedAlert == false){
// not connected
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No internet connection!" message:@"Check internet connection!" delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil];
[alert show];
hasPresentedAlert = true;
}
} else
{
}
}
并在接口中声明布尔值
@interface ViewController (){
BOOL hasPresentedAlert;
}
在应用程序委托中 class 你应该编写 (BOOL) 连接函数,在视图控制器中 class 你只需从应用程序委托中访问这个函数。
-(无效)viewDidLoad:(BOOL)动画{
[超级viewDidLoad:animated];
AppDelegate *
应用程序=(AppDelegate
*)[[UIApplication sharedApplication]delegate];
if([应用已连接]){
//未连接
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No internet connection!" message:@"Check internet connection!" delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil];
[警报显示];
}
}
为了在我的 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 创建一个自己的 HomeViewController.h/HomeViewController.m 文件解决了这个问题。我在那里添加了 alert-if-no-internet-connection 代码。
而不是 UIAlertView
,你应该输入一个布尔值,如果你有互联网连接则为真,如果你没有互联网连接则为假,当你想发出请求时,检查布尔值并显示警报或做你想做的。
如果您只想在 ViewController
第一次打开时执行一次,您可以这样做
- (BOOL)connected
{
Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus networkStatus = [reachability currentReachabilityStatus];
return !(networkStatus == NotReachable);
}
- (void)viewDidLoad {
[super viewDidLoad];
if (![self connected])
{
if(hasPresentedAlert == false){
// not connected
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No internet connection!" message:@"Check internet connection!" delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil];
[alert show];
hasPresentedAlert = true;
}
} else
{
}
}
并在接口中声明布尔值
@interface ViewController (){
BOOL hasPresentedAlert;
}
在应用程序委托中 class 你应该编写 (BOOL) 连接函数,在视图控制器中 class 你只需从应用程序委托中访问这个函数。
-(无效)viewDidLoad:(BOOL)动画{
[超级viewDidLoad:animated];
AppDelegate * 应用程序=(AppDelegate *)[[UIApplication sharedApplication]delegate];
if([应用已连接]){
//未连接
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No internet connection!" message:@"Check internet connection!" delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil];
[警报显示];
}
}