Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/94.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 互联网连接如何等待结果?_Ios_Networking - Fatal编程技术网

Ios 互联网连接如何等待结果?

Ios 互联网连接如何等待结果?,ios,networking,Ios,Networking,我有一个简单的应用程序,在没有互联网的情况下从RSS加载文本,它会显示一个空的tableView。我想让它这样,当没有互联网时,它会给出一些文本,说没有互联网可用,并有一个按钮尝试重新连接 为了实现这一点,我使用了Tony Million的可达性类,如中所示。 我在如下函数中将布尔值设置为YES和NO: - (void)testInternetConnection { internetReachableFoo = [Reachability reachabilityWithHostnam

我有一个简单的应用程序,在没有互联网的情况下从RSS加载文本,它会显示一个空的tableView。我想让它这样,当没有互联网时,它会给出一些文本,说没有互联网可用,并有一个按钮尝试重新连接

为了实现这一点,我使用了Tony Million的可达性类,如中所示。 我在如下函数中将布尔值设置为YES和NO:

- (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!");
            internetConnect = YES;
        });
    };

    // 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 :(");
            internetConnect = NO;
        });
    };

    [internetReachableFoo startNotifier];
}
现在,当我尝试检查viewDidLoad函数中的布尔值时,它总是在函数完成之前返回。这是因为可达性类位于后台线程中。我不知道如何让我的代码在继续之前等待结果

因此,我应该让代码等待结果,然后根据结果使tableView消失,并用按钮将其更改为文本

我想知道:

如何让我的代码等待后台线程的结果。 如何重新连接。用一个加载条或什么东西让用户 知道它正在搜索连接。
呃。。。为什么不把你想继续写的代码放到块里?你真的需要那玩意儿吗

无论如何,您也可以将网络请求包装到可访问性块中,在没有连接时返回错误,将特定错误连接到UI

有点假,但你会明白的。关键是,只有当您想要请求某些东西时,才需要连接,而不需要每次我都监视。通过使用,您可以按需获取可访问性状态,并且这些块只会被调用一次,而不是每次可访问性更改时

另外,你可能对你要联系的主机感兴趣,而不是谷歌。当google.com运行正常时,您的提要可能无法访问

-(void)fetchFeed
{
    [EPPZReachability reachHost:@"your.rss.host.com"
                     completion:^(EPPZReachability *reachability)
    {
        if (reachability.reachable)
        {
            [self hideRetryUI]; // UI

            [self fetchFeed:^(Feed *feed) // Networking
            { [self showFeed:feed]; }]; // UI (probably table view reload)
        }

        else
        {
            [self showRetryUI]; // UI
        }
    }];
}

-(IBAction)retryTouchedUp
{ [self fetchFeed]; }

您还可以根据需要检查可达性

NSInteger reachabilityStatus = 0;

reachabilityStatus = [self checkNetworkReachability];
if (reachabilityStatus) {
   //network is available so perform network oriented task;
} else {
   // show an alert saying network is unavailable;
}

- (NSInteger)checkNetworkReachability {

    networkReachability = [Reachability reachabilityForInternetConnection];
    NetworkStatus networkStatus = [networkReachability currentReachabilityStatus];

    if (networkStatus == NotReachable) {
        NSLog(@"There IS NO internet connection");
    } else {
        NSLog(@"There IS internet connection");
    }

    return (NSInteger)networkStatus;
}

不,我不需要BOOL,这只是我的想法,但日志显示它不起作用,你能给出一个代码示例吗?