Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/120.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.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 可重复连接到internet_Ios_Xcode - Fatal编程技术网

Ios 可重复连接到internet

Ios 可重复连接到internet,ios,xcode,Ios,Xcode,我只想执行一些代码,并且仅当我连接到internet时: //Reachability [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotifica

我只想执行一些代码,并且仅当我连接到internet时:

//Reachability

[[NSNotificationCenter defaultCenter] addObserver:self
                                      selector:@selector(reachabilityChanged:)
                                      name:kReachabilityChangedNotification
                                      object:nil];

Reachability * reach = [Reachability reachabilityWithHostname:@"www.dropbox.com"];

reach.reachableBlock = ^(Reachability * reachability)
{
    dispatch_async(dispatch_get_main_queue(), ^{

        NSLog(@"Block Says Reachable");

        connect = @"yes";

    });
};

reach.unreachableBlock = ^(Reachability * reachability)
{
    dispatch_async(dispatch_get_main_queue(), ^{

        connect = @"no";


    });

};

[reach startNotifier];

//Reachability

if (connect == @"no") {

    UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"" message:@"There is no internet connection. Please connect to the internet. If you are already connected, there might be a problem with our server. Try again in a moment." delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles: nil];
    [alert1 show];

} else if (titleSet == NULL){

    UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"" message:@"Please select a group or create a new one" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles: nil];
    [alert1 show];

}else if (NavBar.topItem.title.length < 1){

    UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"" message:@"Please select a group or create a new one" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles: nil];
    [alert1 show];

} else if (newmessagename.text.length < 4){

    UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"" message:@"Please give a name to your event that is at least 4 characters long" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles: nil];
    [alert1 show];

}
//可达性
[[NSNotificationCenter defaultCenter]添加观察者:self
选择器:@selector(可达性更改:)
名称:kReachabilityChangedNotification
对象:无];
可达性*reach=[可达性可达性WithHostName:@“www.dropbox.com”];
reach.reachableBlock=^(可达性*可达性)
{
dispatch\u async(dispatch\u get\u main\u queue()^{
NSLog(@“Block表示可以访问”);
connect=@“是”;
});
};
reach.unreachableBlock=^(可达性*可达性)
{
dispatch\u async(dispatch\u get\u main\u queue()^{
connect=@“否”;
});
};
[到达startNotifier];
//可达性
如果(连接==@“否”){
UIAlertView*alert1=[[UIAlertView alloc]initWithTitle:@”“消息:@”没有internet连接。请连接到internet。如果您已经连接,我们的服务器可能有问题。请稍后重试。“代理:自取消按钮:@“解除”其他按钮:nil];
[警报1显示];
}else if(titleSet==NULL){
UIAlertView*alert1=[[UIAlertView alloc]initWithTitle:@”“消息:@“请选择一个组或创建一个新组”委托:自取消按钮:@“解除”其他按钮:无];
[警报1显示];
}否则如果(NavBar.topItem.title.length<1){
UIAlertView*alert1=[[UIAlertView alloc]initWithTitle:@”“消息:@“请选择一个组或创建一个新组”委托:自取消按钮:@“解除”其他按钮:无];
[警报1显示];
}else if(newmessagename.text.length<4){
UIAlertView*alert1=[[UIAlertView alloc]initWithTitle:@“消息:@“请为您的事件指定一个至少4个字符长的名称”委托:自取消按钮:@“解除”其他按钮:nil];
[警报1显示];
}
代码似乎没有按顺序执行。我认为检查互联网连接比执行代码花费的时间要多。我怎样才能解决这个问题?
请不要告诉我将代码直接放在括号中,
connect=@“no”位于

块不是按顺序执行的,而是异步执行的

这意味着您无法判断何时调用块中的代码。使用该块的代码可能在方法的其余部分之前完成并执行(但是这不太可能,尤其是在Internet连接的情况下)

您应该将
ifs
放入在有效时间调用的方法中。这一次可能是当您收到来自块的响应时,或者,如果我的内存为true,
[reach startNotifier]可以在可达性状态发生更改时通知您,这似乎是您的
可达性更改:
方法:

-(void) reachabilityChanged:(id) parameter
{
   //Query reachability and notify / cache as required.
} 

当然,它不是按顺序执行的,这些方法的全部目的是在获得可访问性响应时停止ui冻结。基本上,您设置了可达性响应,并在尚未检查任何内容时立即询问结果。你真正需要做的是把它移到括号里

您还可以使用这些结果生成一个函数,并在两个块中调用此函数

如果您希望在viewcontroller上加载此控件或在显示任何其他内容之前加载此控件,则必须在显示此控制器之前检查可访问性,或者添加“加载”屏幕


编辑:我不明白的是,那些可达性方法在得到结果时似乎会触发一个块,但是你也在注册一个通知。我没有看到你为此发布通知。您在这里使用的是两种异步方法(块和通知)

connect=@“no”
无论如何都不起作用。。使用
isEqualToString:
比较字符串。我尝试将方法放在括号内,但代码没有被调用!如果您只是将nslogs添加到这两个博客中,没有人会打印任何内容?然后,将所有ifs复制到一个新函数中,例如-(void)responseAction;然后加上[自我反应];连接后=否,连接后=是。