YYReachability.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. //
  2. // YYReachability.h
  3. // YYKit <https://github.com/ibireme/YYKit>
  4. //
  5. // Created by ibireme on 15/2/6.
  6. // Copyright (c) 2015 ibireme.
  7. //
  8. // This source code is licensed under the MIT-style license found in the
  9. // LICENSE file in the root directory of this source tree.
  10. //
  11. #import <Foundation/Foundation.h>
  12. #import <SystemConfiguration/SystemConfiguration.h>
  13. #import <netinet/in.h>
  14. NS_ASSUME_NONNULL_BEGIN
  15. typedef NS_ENUM(NSUInteger, YYReachabilityStatus) {
  16. YYReachabilityStatusNone = 0, ///< Not Reachable
  17. YYReachabilityStatusWWAN = 1, ///< Reachable via WWAN (2G/3G/4G)
  18. YYReachabilityStatusWiFi = 2, ///< Reachable via WiFi
  19. };
  20. typedef NS_ENUM(NSUInteger, YYReachabilityWWANStatus) {
  21. YYReachabilityWWANStatusNone = 0, ///< Not Reachable vis WWAN
  22. YYReachabilityWWANStatus2G = 2, ///< Reachable via 2G (GPRS/EDGE) 10~100Kbps
  23. YYReachabilityWWANStatus3G = 3, ///< Reachable via 3G (WCDMA/HSDPA/...) 1~10Mbps
  24. YYReachabilityWWANStatus4G = 4, ///< Reachable via 4G (eHRPD/LTE) 100Mbps
  25. };
  26. /**
  27. `YYReachability` can used to monitor the network status of an iOS device.
  28. */
  29. @interface YYReachability : NSObject
  30. @property (nonatomic, readonly) SCNetworkReachabilityFlags flags; ///< Current flags.
  31. @property (nonatomic, readonly) YYReachabilityStatus status; ///< Current status.
  32. @property (nonatomic, readonly) YYReachabilityWWANStatus wwanStatus NS_AVAILABLE_IOS(7_0); ///< Current WWAN status.
  33. @property (nonatomic, readonly, getter=isReachable) BOOL reachable; ///< Current reachable status.
  34. /// Notify block which will be called on main thread when network changed.
  35. @property (nullable, nonatomic, copy) void (^notifyBlock)(YYReachability *reachability);
  36. /// Create an object to check the reachability of the default route.
  37. + (instancetype)reachability;
  38. /// Create an object to check the reachability of the local WI-FI.
  39. + (instancetype)reachabilityForLocalWifi DEPRECATED_MSG_ATTRIBUTE("unnecessary and potentially harmful");
  40. /// Create an object to check the reachability of a given host name.
  41. + (nullable instancetype)reachabilityWithHostname:(NSString *)hostname;
  42. /// Create an object to check the reachability of a given IP address
  43. /// @param hostAddress You may pass `struct sockaddr_in` for IPv4 address or `struct sockaddr_in6` for IPv6 address.
  44. + (nullable instancetype)reachabilityWithAddress:(const struct sockaddr *)hostAddress;
  45. @end
  46. NS_ASSUME_NONNULL_END