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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM