我想检查一下我是否可以在iOS上使用Cocoa Touch库或在macOS上使用Cocoa库连接互联网。

我想出了一个方法来做这个使用NSURL。我的方法似乎有点不可靠(因为即使谷歌也可能有一天宕机,依赖第三方似乎很糟糕),虽然如果谷歌没有响应,我可以检查其他一些网站的响应,但这似乎是浪费和不必要的应用程序开销。

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

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


当前回答

以下是我在我的应用程序中是如何做到的:虽然200状态响应代码不能保证任何东西,但对我来说已经足够稳定了。这并不需要像NSData回答一样多的加载,因为我的只是检查HEAD响应。

银行的国际代码

func checkInternet(flag:Bool, completionHandler:(internet:Bool) -> Void)
{
    UIApplication.sharedApplication().networkActivityIndicatorVisible = true

    let url = NSURL(string: "http://www.google.com/")
    let request = NSMutableURLRequest(URL: url!)

    request.HTTPMethod = "HEAD"
    request.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData
    request.timeoutInterval = 10.0

    NSURLConnection.sendAsynchronousRequest(request, queue:NSOperationQueue.mainQueue(), completionHandler:
    {(response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in

        UIApplication.sharedApplication().networkActivityIndicatorVisible = false

        let rsp = response as! NSHTTPURLResponse?

        completionHandler(internet:rsp?.statusCode == 200)
    })
}

func yourMethod()
{
    self.checkInternet(false, completionHandler:
    {(internet:Bool) -> Void in

        if (internet)
        {
            // "Internet" aka Google URL reachable
        }
        else
        {
            // No "Internet" aka Google URL un-reachable
        }
    })
}

objective - c代码

typedef void(^connection)(BOOL);

- (void)checkInternet:(connection)block
{
    NSURL *url = [NSURL URLWithString:@"http://www.google.com/"];
    NSMutableURLRequest *headRequest = [NSMutableURLRequest requestWithURL:url];
    headRequest.HTTPMethod = @"HEAD";

    NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration ephemeralSessionConfiguration];
    defaultConfigObject.timeoutIntervalForResource = 10.0;
    defaultConfigObject.requestCachePolicy = NSURLRequestReloadIgnoringLocalAndRemoteCacheData;

    NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration:defaultConfigObject delegate:self delegateQueue: [NSOperationQueue mainQueue]];

    NSURLSessionDataTask *dataTask = [defaultSession dataTaskWithRequest:headRequest
        completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
    {
        if (!error && response)
        {
            block([(NSHTTPURLResponse *)response statusCode] == 200);
        }
    }];
    [dataTask resume];
}

- (void)yourMethod
{
    [self checkInternet:^(BOOL internet)
    {
         if (internet)
         {
             // "Internet" aka Google URL reachable
         }
         else
         {
             // No "Internet" aka Google URL un-reachable
         }
    }];
}

其他回答

-(void)newtworkType {

 NSArray *subviews = [[[[UIApplication sharedApplication] valueForKey:@"statusBar"] valueForKey:@"foregroundView"]subviews];
NSNumber *dataNetworkItemView = nil;

for (id subview in subviews) {
    if([subview isKindOfClass:[NSClassFromString(@"UIStatusBarDataNetworkItemView") class]]) {
        dataNetworkItemView = subview;
        break;
    }
}


switch ([[dataNetworkItemView valueForKey:@"dataNetworkType"]integerValue]) {
    case 0:
        NSLog(@"No wifi or cellular");
        break;

    case 1:
        NSLog(@"2G");
        break;

    case 2:
        NSLog(@"3G");
        break;

    case 3:
        NSLog(@"4G");
        break;

    case 4:
        NSLog(@"LTE");
        break;

    case 5:
        NSLog(@"Wifi");
        break;


    default:
        break;
}
}

重要:该检查应该始终异步执行。下面的大多数答案是同步的,所以要小心,否则你会冻结你的应用程序。


斯威夫特

通过CocoaPods或Carthage安装:https://github.com/ashleymills/Reachability.swift 通过闭包测试可达性 let reachability =可达性()! 可达性。whenReachable ={可达性 如果可达性。连接== .wifi { 打印(“可通过WiFi访问”) }其他{ 打印(“可通过蜂窝连接”) } } 可达性。whenUnreachable = {_ in 打印(“不可以”) } {做 尝试reachability.startNotifier () } catch { 打印("无法启动通知程序") }


objective - c

将SystemConfiguration框架添加到项目中,但不要担心将它包含在任何地方 添加Tony Million版本的Reachability.h和Reachability。m到项目(在这里找到:https://github.com/tonymillion/Reachability) 更新接口部分 #进口“Reachability.h” //在视图控制器的.m文件中添加这个接口 @interface MyViewController () { 可达性* internetReachableFoo; } @end 然后在视图控制器的。m文件中实现这个方法,你可以调用它 //检查我们是否有网络连接 - (void) testInternetConnection { internetReachableFoo = [Reachability reachabilityWithHostname:@"www.google.com"]; //网络可达 internetReachableFoo。reachableBlock = ^(可达性*reach) { //更新主线程的UI 设置(dispatch_get_main_queue()、^ { NSLog(@“Yayyy,我们有了互联网!”); }); }; //网络不可达 internetReachableFoo。unreachableBlock = ^(可达性*reach) { //更新主线程的UI 设置(dispatch_get_main_queue()、^ { NSLog(@"有人破坏了互联网:("); }); }; [internetReachableFoo startNotifier]; }

重要提示:Reachability类是项目中最常用的类之一,因此您可能会遇到与其他项目的命名冲突。如果发生这种情况,您将不得不重命名Reachability.h和Reachability对中的一个。M文件到其他文件来解决这个问题。

注意:您使用的域名并不重要。这只是在测试通往任何领域的入口。

迅捷5号,我是阿拉莫火,主持人

// Session reference
var alamofireSessionManager: Session!

func checkHostReachable(completionHandler: @escaping (_ isReachable:Bool) -> Void) {
    let configuration = URLSessionConfiguration.default
    configuration.timeoutIntervalForRequest = 1
    configuration.timeoutIntervalForResource = 1
    configuration.requestCachePolicy = .reloadIgnoringLocalCacheData

    alamofireSessionManager = Session(configuration: configuration)

    alamofireSessionManager.request("https://google.com").response { response in
        completionHandler(response.response?.statusCode == 200)
    }
}

// Using
checkHostReachable() { (isReachable) in
    print("isReachable:\(isReachable)")
}

当使用iOS 12或macOS v10.14 (Mojave)或更新版本时,您可以使用NWPathMonitor而不是史前可达性类。作为奖励,你可以很容易地检测到当前的网络连接类型:

import Network // Put this on top of your class

let monitor = NWPathMonitor()

monitor.pathUpdateHandler = { path in
    if path.status != .satisfied {
        // Not connected
    }
    else if path.usesInterfaceType(.cellular) {
        // Cellular 3/4/5g connection
    }
    else if path.usesInterfaceType(.wifi) {
        // Wi-Fi connection
    }
    else if path.usesInterfaceType(.wiredEthernet) {
        // Ethernet connection
    }
}

monitor.start(queue: DispatchQueue.global(qos: .background))

更多信息请访问:https://developer.apple.com/documentation/network/nwpathmonitor

Alamofire

我知道这个问题是要求Coca Touch解决方案,但我想为搜索检查iOS上的互联网连接的人提供一个解决方案,在这里将有一个更多的选择。

如果您已经在使用Alamofire,以下是您可以从中受益的内容。

你可以将以下类添加到你的应用程序中,并调用MNNetworkUtils.main.isConnected()来获得一个关于它是否连接的布尔值。

#import Alamofire

class MNNetworkUtils {
  static let main = MNNetworkUtils()
  init() {
    manager = NetworkReachabilityManager(host: "google.com")
    listenForReachability()
  }

  private let manager: NetworkReachabilityManager?
  private var reachable: Bool = false
  private func listenForReachability() {
    self.manager?.listener = { [unowned self] status in
      switch status {
      case .notReachable:
        self.reachable = false
      case .reachable(_), .unknown:
        self.reachable = true
      }
    }
    self.manager?.startListening()
  }

  func isConnected() -> Bool {
    return reachable
  }
}

这是一个单例类。每次,当用户连接或断开网络时,它都会覆盖self。因为我们开始在单例初始化时监听NetworkReachabilityManager。

另外,为了监视可达性,您需要提供一个主机。目前,我正在使用google.com,但如果需要,请随意更改到任何其他主机或您的主机之一。将类名和文件名更改为与项目匹配的任何名称。