因此,我使警报视图检测到是否存在 Activity 的互联网连接。
,这是代码:
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reachabilityChanged:)
name:kReachabilityChangedNotification
object:nil];
Reachability * reach = [Reachability reachabilityWithHostname:@"www.google.com"];
reach.reachableBlock = ^(Reachability * reachability)
{
dispatch_async(dispatch_get_main_queue(), ^{
blockLabel.text = @"";
});
};
reach.unreachableBlock = ^(Reachability * reachability)
{
dispatch_async(dispatch_get_main_queue(), ^{
blockLabel.text = @"You are not connected to the Internet";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Please connect to Internet"
message:nil
delegate:self
cancelButtonTitle:nil
otherButtonTitles:nil];
UIActivityIndicatorView *progress= [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(125, 50, 30, 30)];
progress.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
[alert addSubview:progress];
[progress startAnimating];
[alert show];
});
};
[reach startNotifier];
// Do any additional setup after loading the view, typically from a nib.
}
因此,我的应用程序检测是否有Internet连接。但是问题是,如果我在iPhone上打开互联网并打开该应用程序,它仍然说没有互联网连接。我该怎么办…
最佳答案
好吧,我通过使用通知来解决此问题。参考代码
-(void)reachabilityChanged:(NSNotification*)note
{
Reachability * reach = [note object];
if([reach isReachable])
{
notificationLabel.text = @"Notification Says Reachable";
NSLog(@"Internet is Up");
}
else
{
notificationLabel.text = @"Notification Says Unreachable";
NSLog(@"Internet is Down");
}
}
关于ios - iOS编程中的可达性AlertView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13519157/