我想判断一下,在使用Cocoa Touch个库的iOS或使用Cocoa个库的MacOS上,我是否连接了互联网.

我想出了一个用NSURL来做这件事的方法.我这样做的方式似乎有点不可靠(因为即使谷歌有一天也可能会崩溃,依赖第三方看起来很糟糕),虽然如果谷歌没有回应,我可以查看其他一些网站的react ,但它确实看起来很浪费,给我的应用程序带来了不必要的开销.

- (BOOL) connectedToInternet
{
    NSString *URLString = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.google.com"]];
    return ( URLString != NULL ) ? YES : NO;
}

是不是我做得不好(更不用说stringWithContentsOfURL在iOS 3.0和macOS 10.4中被弃用了),如果是这样,有什么更好的方法来实现这一点?

推荐答案

Important:此判断应异步执行always.下面的大多数答案都是同步的,所以要小心,否则会冻结您的应用程序.


Swift

  1. 通过CocoaPods或Carthage安装:https://github.com/ashleymills/Reachability.swift

  2. 通过闭包测试可达性

    let reachability = Reachability()!
    
    reachability.whenReachable = { reachability in
        if reachability.connection == .wifi {
            print("Reachable via WiFi")
        } else {
            print("Reachable via Cellular")
        }
    }
    
    reachability.whenUnreachable = { _ in
        print("Not reachable")
    }
    
    do {
        try reachability.startNotifier()
    } catch {
        print("Unable to start notifier")
    }
    

Objective-C

  1. SystemConfiguration个框架添加到项目中,但不要担心在任何地方都要包含它

  2. 将Tony Million版本的Reachability.hReachability.m添加到该项目中(可在此处找到:https://github.com/tonymillion/Reachability)

  3. 更新接口节

    #import "Reachability.h"
    
    // Add this to the interface in the .m file of your view controller
    @interface MyViewController ()
    {
        Reachability *internetReachableFoo;
    }
    @end
    
  4. 然后在视图控制器的.m文件中实现此方法,您可以调用

    // Checks if we have an internet connection or not
    - (void)testInternetConnection
    {
        internetReachableFoo = [Reachability reachabilityWithHostname:@"www.google.com"];
    
        // Internet is reachable
        internetReachableFoo.reachableBlock = ^(Reachability*reach)
        {
            // Update the UI on the main thread
            dispatch_async(dispatch_get_main_queue(), ^{
                NSLog(@"Yayyy, we have the interwebs!");
            });
        };
    
        // Internet is not reachable
        internetReachableFoo.unreachableBlock = ^(Reachability*reach)
        {
            // Update the UI on the main thread
            dispatch_async(dispatch_get_main_queue(), ^{
                NSLog(@"Someone broke the internet :(");
            });
        };
    
        [internetReachableFoo startNotifier];
    }
    

Important Note: Reachability类是项目中最常用的类之一,因此您可能会遇到与其他项目的命名冲突.如果发生这种情况,您将不得不将Reachability.hReachability.m文件对中的一个文件重命名为其他名称以解决该问题.

Note:你使用的域名并不重要.它只是在测试通向任何域的网关.

Ios相关问答推荐

Swift addtarget方法的默认参数不生效

SWIFT根据地球坐标修改 node 旋转

如何在Android或iPhone上的原生react 中处理其他应用程序通知?

无法关闭包含字体 Select 器的工作表

保存和读取登录到 keys 串不工作 IOS swift

在自定义 ButtonStyle 中修改形状 colored颜色

拔下设备后,DriverKit USB 驱动程序 (dext) 进程不会终止

UIView 倒置旋转时的zoom 问题

包装在 AnyView 中时 UIViewControllerRepresentable 无法正常工作

缺少推送通知权利

Swift 错误:在其自己的初始值中使用的变量

在主线程上发布 NSNotification

界面生成器中 UIView 的边框 colored颜色 不起作用?

在 Swift 中上传带参数的图像

在 iPhone 中将 UIViewController 显示为弹出窗口

如何缩小 UIImage 并使其同时变得清晰/锐利而不是模糊?

@IBDesignable 崩溃代理

如何立即移动 CALayer(无动画)

我的应用程序因使用广告支持框架而被拒绝.哪个图书馆负责?

iPhone UITableView.如何开启音乐 App 之类的单字母字母列表?