ZFReachabilityManager.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //
  2. // ZFReachabilityManager.h
  3. // ZFPlayer
  4. //
  5. // Copyright (c) 2016年 任子丰 ( http://github.com/renzifeng )
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. #import <Foundation/Foundation.h>
  25. #if !TARGET_OS_WATCH
  26. #import <SystemConfiguration/SystemConfiguration.h>
  27. typedef NS_ENUM(NSInteger, ZFReachabilityStatus) {
  28. ZFReachabilityStatusUnknown = -1,
  29. ZFReachabilityStatusNotReachable = 0,
  30. ZFReachabilityStatusReachableViaWiFi = 1,
  31. ZFReachabilityStatusReachableVia2G = 2,
  32. ZFReachabilityStatusReachableVia3G = 3,
  33. ZFReachabilityStatusReachableVia4G = 4,
  34. };
  35. NS_ASSUME_NONNULL_BEGIN
  36. /**
  37. `ZFReachabilityManager` monitors the reachability of domains, and addresses for both WWAN and WiFi network interfaces.
  38. Reachability can be used to determine background information about why a network operation failed, or to trigger a network operation retrying when a connection is established. It should not be used to prevent a user from initiating a network request, as it's possible that an initial request may be required to establish reachability.
  39. See Apple's Reachability Sample Code ( https://developer.apple.com/library/ios/samplecode/reachability/ )
  40. @warning Instances of `AFNetworkReachabilityManager` must be started with `-startMonitoring` before reachability status can be determined.
  41. */
  42. @interface ZFReachabilityManager : NSObject
  43. /**
  44. The current network reachability status.
  45. */
  46. @property (readonly, nonatomic, assign) ZFReachabilityStatus networkReachabilityStatus;
  47. /**
  48. Whether or not the network is currently reachable.
  49. */
  50. @property (readonly, nonatomic, assign, getter = isReachable) BOOL reachable;
  51. /**
  52. Whether or not the network is currently reachable via WWAN.
  53. */
  54. @property (readonly, nonatomic, assign, getter = isReachableViaWWAN) BOOL reachableViaWWAN;
  55. /**
  56. Whether or not the network is currently reachable via WiFi.
  57. */
  58. @property (readonly, nonatomic, assign, getter = isReachableViaWiFi) BOOL reachableViaWiFi;
  59. ///---------------------
  60. /// @name Initialization
  61. ///---------------------
  62. /**
  63. Returns the shared network reachability manager.
  64. */
  65. + (instancetype)sharedManager;
  66. /**
  67. Creates and returns a network reachability manager with the default socket address.
  68. @return An initialized network reachability manager, actively monitoring the default socket address.
  69. */
  70. + (instancetype)manager;
  71. /**
  72. Creates and returns a network reachability manager for the specified domain.
  73. @param domain The domain used to evaluate network reachability.
  74. @return An initialized network reachability manager, actively monitoring the specified domain.
  75. */
  76. + (instancetype)managerForDomain:(NSString *)domain;
  77. /**
  78. Creates and returns a network reachability manager for the socket address.
  79. @param address The socket address (`sockaddr_in6`) used to evaluate network reachability.
  80. @return An initialized network reachability manager, actively monitoring the specified socket address.
  81. */
  82. + (instancetype)managerForAddress:(const void *)address;
  83. /**
  84. Initializes an instance of a network reachability manager from the specified reachability object.
  85. @param reachability The reachability object to monitor.
  86. @return An initialized network reachability manager, actively monitoring the specified reachability.
  87. */
  88. - (instancetype)initWithReachability:(SCNetworkReachabilityRef)reachability NS_DESIGNATED_INITIALIZER;
  89. ///--------------------------------------------------
  90. /// @name Starting & Stopping Reachability Monitoring
  91. ///--------------------------------------------------
  92. /**
  93. Starts monitoring for changes in network reachability status.
  94. */
  95. - (void)startMonitoring;
  96. /**
  97. Stops monitoring for changes in network reachability status.
  98. */
  99. - (void)stopMonitoring;
  100. ///-------------------------------------------------
  101. /// @name Getting Localized Reachability Description
  102. ///-------------------------------------------------
  103. /**
  104. Returns a localized string representation of the current network reachability status.
  105. */
  106. - (NSString *)localizedNetworkReachabilityStatusString;
  107. ///---------------------------------------------------
  108. /// @name Setting Network Reachability Change Callback
  109. ///---------------------------------------------------
  110. /**
  111. Sets a callback to be executed when the network availability of the `baseURL` host changes.
  112. @param block A block object to be executed when the network availability of the `baseURL` host changes.. This block has no return value and takes a single argument which represents the various reachability states from the device to the `baseURL`.
  113. */
  114. - (void)setReachabilityStatusChangeBlock:(nullable void (^)(ZFReachabilityStatus status))block;
  115. @end
  116. ///--------------------
  117. /// @name Notifications
  118. ///--------------------
  119. FOUNDATION_EXPORT NSString * const ZFReachabilityDidChangeNotification;
  120. FOUNDATION_EXPORT NSString * const ZFReachabilityNotificationStatusItem;
  121. ///--------------------
  122. /// @name Functions
  123. ///--------------------
  124. /**
  125. Returns a localized string representation of an `ZFReachabilityStatus` value.
  126. */
  127. FOUNDATION_EXPORT NSString * ZFStringFromNetworkReachabilityStatus(ZFReachabilityStatus status);
  128. NS_ASSUME_NONNULL_END
  129. #endif