查看: 2442|回复: 0

[IOS开发教程] iOS 网络状态判断方案(支持iOS11和iPhoneX)

发表于 2018-1-12 08:00:00

在之前的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 代码如下:
  1. // Allocate a reachability object
  2. Reachability* reach = [Reachability reachabilityWithHostname:@"www.baidu.com"];
  3. // Set the blocks
  4. reach.reachableBlock = ^(Reachability*reach)
  5. {
  6. // keep in mind this is called on a background thread
  7. // and if you are updating the UI it needs to happen
  8. // on the main thread, like this:
  9. dispatch_async(dispatch_get_main_queue(), ^{
  10. NSLog(@"REACHABLE!");
  11. });
  12. };
  13. reach.unreachableBlock = ^(Reachability*reach)
  14. {
  15. NSLog(@"UNREACHABLE!");
  16. };
  17. // Start the notifier, which will cause the reachability object to retain itself!
  18. [reach startNotifier];
复制代码

Swift 代码如下:
  1. import Reachability
  2. var reach: Reachability?
  3. func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
  4. // Allocate a reachability object
  5. self.reach = Reachability.forInternetConnection()
  6. // Set the blocks
  7. self.reach!.reachableBlock = {
  8. (reach: Reachability?) -> Void in
  9. // keep in mind this is called on a background thread
  10. // and if you are updating the UI it needs to happen
  11. // on the main thread, like this:
  12. DispatchQueue.main.async {
  13. print("REACHABLE!")
  14. }
  15. }
  16. self.reach!.unreachableBlock = {
  17. (reach: Reachability?) -> Void in
  18. print("UNREACHABLE!")
  19. }
  20. self.reach!.startNotifier()
  21. return true
  22. }
复制代码

3、NSNotification 方式

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

Objective-C 代码如下:
  1. // Allocate a reachability object
  2. Reachability* reach = [Reachability reachabilityWithHostname:@"www.baidu.com"];
  3. // Tell the reachability that we DON'T want to be reachable on 3G/EDGE/CDMA
  4. reach.reachableOnWWAN = NO;
  5. // Here we set up a NSNotification observer. The Reachability that caused the notification
  6. // is passed in the object parameter
  7. [[NSNotificationCenter defaultCenter] addObserver:self
  8. selector:@selector(reachabilityChanged:)
  9. name:kReachabilityChangedNotification
  10. object:nil];
  11. [reach startNotifier];
复制代码
Swift 代码如下:
  1. import Reachability
  2. var reach: Reachability?
  3. func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
  4. // Allocate a reachability object
  5. self.reach = Reachability.forInternetConnection()
  6. // Tell the reachability that we DON'T want to be reachable on 3G/EDGE/CDMA
  7. self.reach!.reachableOnWWAN = false
  8. // Here we set up a NSNotification observer. The Reachability that caused the notification
  9. // is passed in the object parameter
  10. NotificationCenter.default.addObserver(
  11. self,
  12. selector: #selector(reachabilityChanged),
  13. name: NSNotification.Name.reachabilityChanged,
  14. object: nil
  15. )
  16. self.reach!.startNotifier()
  17. return true
  18. }
  19. func reachabilityChanged(notification: NSNotification) {
  20. if self.reach!.isReachableViaWiFi() || self.reach!.isReachableViaWWAN() {
  21. print("Service avalaible!!!")
  22. } else {
  23. print("No service avalaible!!!")
  24. }
  25. }
复制代码



回复

使用道具 举报