我只想执行一些代码,并且只有当我连接到互联网时:
//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];
}
似乎代码没有按顺序执行。我认为检查 Internet 连接所花费的时间比执行代码所花费的时间要多。我怎样才能解决这个问题?请不要告诉我将代码直接放在括号中connect = @"no";
的位置。