iOS 网络状态判断方案(支持iOS11和iPhoneX)

在之前的iPhone中、我们可以根据导航栏上方的网络状态view、来判断网络状态。(这种方案本来就不太好)

并且,这种方案在iPhone X 手机上、不可使用。 

那么,在iPhone X 或者之前的手机上面该怎么办呢?

我们可以通过 Reachability  来判断网络状态

Reachability github 地址:https://github.com/tonymillion/Reachability

使用方法超简单,Block方式和NSNotification方式 二选一即可。

1、添加SystemConfiguration.framework.

2、使用Block方式

  注意:当网络状态发生改变的时候、会在后台进程中触发Block、我们需要在主线程中进行UI更新操作。

Objective-C 代码如下:

// Allocate a reachability object
Reachability* reach = [Reachability reachabilityWithHostname:@"www.baidu.com"];

// Set the blocks
reach.reachableBlock = ^(Reachability*reach)
{
    // keep in mind this is called on a background thread
    // and if you are updating the UI it needs to happen
    // on the main thread, like this:

    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"REACHABLE!");
    });
};

reach.unreachableBlock = ^(Reachability*reach)
{
    NSLog(@"UNREACHABLE!");
};

// Start the notifier, which will cause the reachability object to retain itself!
[reach startNotifier];

Swift  代码如下:

import Reachability

var reach: Reachability?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Allocate a reachability object
        self.reach = Reachability.forInternetConnection()
        
        // Set the blocks
        self.reach!.reachableBlock = {
            (reach: Reachability?) -> Void in
            
            // keep in mind this is called on a background thread
            // and if you are updating the UI it needs to happen
            // on the main thread, like this:
            DispatchQueue.main.async {
                print("REACHABLE!")
            }
        }
        
        self.reach!.unreachableBlock = {
            (reach: Reachability?) -> Void in
            print("UNREACHABLE!")
        }
        
        self.reach!.startNotifier()
    
        return true
}

3、NSNotification 方式

  此方式界面发生了变化时、将会通知。通知将在主线程上传递,所以可以从函数中进行UI更新。此外,它要求监控对象考虑WWAN(3G / EDGE / CDMA)作为一个不可到达的连接(比如正在写一个视频流的应用程序,或者下载视频等等)。

Objective-C 代码如下:

// Allocate a reachability object
Reachability* reach = [Reachability reachabilityWithHostname:@"www.baidu.com"];

// Tell the reachability that we DON'T want to be reachable on 3G/EDGE/CDMA
reach.reachableOnWWAN = NO;

// Here we set up a NSNotification observer. The Reachability that caused the notification
// is passed in the object parameter
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(reachabilityChanged:)
                                             name:kReachabilityChangedNotification
                                           object:nil];

[reach startNotifier];

Swift  代码如下:

import Reachability

var reach: Reachability?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Allocate a reachability object
    self.reach = Reachability.forInternetConnection()
    
    // Tell the reachability that we DON'T want to be reachable on 3G/EDGE/CDMA
    self.reach!.reachableOnWWAN = false
    
    // Here we set up a NSNotification observer. The Reachability that caused the notification
    // is passed in the object parameter
    NotificationCenter.default.addObserver(
        self,
        selector: #selector(reachabilityChanged),
        name: NSNotification.Name.reachabilityChanged,
        object: nil
    )
    
    self.reach!.startNotifier()
    
    return true
}
        
func reachabilityChanged(notification: NSNotification) {
    if self.reach!.isReachableViaWiFi() || self.reach!.isReachableViaWWAN() {
        print("Service avalaible!!!")
    } else {
        print("No service avalaible!!!")
    }
}
原文地址:https://www.cnblogs.com/jukaiit/p/8145200.html