iOS network status judgment scheme (supports iOS 11 and iPhone x)

Keywords: iOS network github Swift

In the previous iPhone, we can judge the network status according to the network status view above the navigation bar. (this kind of plan is not very good)

This solution is not available on iPhone X phones.

 

We can judge the network state through the Reachability

Reachability github address: https://github.com/tonymillion/Reachability

Super simple to use

1. Add SystemConfiguration.framework

2. Use Block mode

Note: when the network status changes, Block will be triggered in the background process. We need to update the UI in the main thread.

Objective-C code is as follows:

// 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 is as follows

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

When the interface of this mode changes, it will be notified. Notifications are passed on the main thread, so UI updates can be made from functions. In addition, it requires the monitoring object to consider WWAN (3G / EDGE / CDMA) as an unreachable connection (such as writing an application of video stream, downloading video, etc.).

 

Objective-C code is as follows:

// 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 code is as follows:

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!!!")
    }
}

Posted by prosolutions on Sun, 03 May 2020 16:58:58 -0700