ios - iOS 编程中的可达性 AlertView

标签 ios xcode ipad

所以我让警报 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/

相关文章:

iphone - RestKit集成问题 "No Such File Or directory"

html - iPad 在右侧留有填充空间

ios - Xcode 7.0.1 - 无效的 Swift 支持 - 缺少 SwiftSupport 文件夹。

android - 是否有可能获得嵌入在移动应用程序中的 key

xcode - NSButton "text only"点击时突出显示的变体?

xcode - 将plist读入nsmutabledictionary,更新字典然后再次保存

iOS 应用商店评论崩溃

ios - 通过示例了解何时使用 dispatch_get_main_queue 和 requiresMainQueueSetup

ios - 如何使用 URL Scheme 修复 iOS 13 中的 'sharing to instagram stories'

objective-c - 十进制到十六进制 - Cocoa/Objective-C