我有一个 TableView,它从服务器接收数据并且一切正常。
- (void)viewDidLoad
{
[super viewDidLoad];
// retrieveData is a method that Loads the Content
[self retrieveData];
}
但是如果用户没有互联网,应用程序就会崩溃。 我如何检查此连接,如果正常,我加载数据。如果不正常,我向用户发送 NSAlert?
最佳答案
您可以按照下面的方式检查互联网连接,并希望您可以在您的网络上实现 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 对此进行了很好的解释。
关于ios - 没有互联网的 NSNotification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23066528/