gpt4 book ai didi

ios - iOS 编程中的可达性 AlertView

转载 作者:行者123 更新时间:2023-11-29 13:24:21 25 4
gpt4 key购买 nike

所以我让警报 View 检测是否存在事件的互联网连接。

这是代码:

- (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.
}

所以我的应用程序会检测是否有互联网连接。但问题是,如果我在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com