我有一个从服务器接收数据的TableView,一切正常。
- (void)viewDidLoad
{
[super viewDidLoad];
// retrieveData is a method that Loads the Content
[self retrieveData];
}
但如果用户没有互联网,则应用程序崩溃。 我如何检查此连接,如果是,我加载数据..如果IS不正常,我将NSAlert发送给用户?
答案 0 :(得分:1)
您可以检查下面给出的互联网连接,并希望您可以在您的网站上实施UIAlertview。
// Checks if we have an internet connection or not
- (void)testInternetConnection
{
internetReachableFoo = [Reachability reachabilityWithHostname:@"www.google.com"];
// Internet is reachable
internetReachableFoo.reachableBlock = ^(Reachability*reach)
{
// Update the UI on the main thread
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"Yayyy, we have the interwebs!");
});
};
// Internet is not reachable
internetReachableFoo.unreachableBlock = ^(Reachability*reach)
{
// Update the UI on the main thread
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"Someone broke the internet :(");
});
};
[internetReachableFoo startNotifier];
}
iWasRobbed在这里很好地解释了这一点。
答案 1 :(得分:0)
答案 2 :(得分:0)
获得downloaded并导入Reachbility.m
和Reachbility.h
个文件
创建辅助函数:
-(BOOL)IsConnected{
Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus networkStatus = [reachability currentReachabilityStatus];
return !(networkStatus == NotReachable);
}
然后使用它
if([self IsConnected]){
[self retrieveData];
}
else{
//not connected to internet!
}