ios - 没有互联网的 NSNotification

标签 ios uitableview connection

我有一个 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 对此进行了很好的解释。

礼貌:- https://stackoverflow.com/a/3597085/1865424

关于ios - 没有互联网的 NSNotification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23066528/

相关文章:

IOS如何快速删除核心数据中的对象

ios - 如何使用 swift 在多单元格 uitableview 中实现导航

ios - 为什么 Xcode 10 在完成剩余 2 分钟时停止向前更新?

swift - 如何为 UITableView 中的每个部分启用水平分页

java - 连接数据库时出现问题

ios - iOS 中如何设置图像的长宽比

ios - 如何禁用UITableView的节索引的用户交互?

uitableview - layoutIfNeeded 影响表格 View 单元格

mysql - Tomcat7 MySQL 连接错误

Android 设备需要连接到服务器 - C2DM、轮询或第三方?