暫無描述

AWSKSReachability.h 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //
  2. // KSReachability.h
  3. //
  4. // Created by Karl Stenerud on 5/5/12.
  5. //
  6. // Copyright (c) 2012 Karl Stenerud. All rights reserved.
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights
  11. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. // copies of the Software, and to permit persons to whom the Software is
  13. // furnished to do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall remain in place
  16. // in this source code.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. // THE SOFTWARE.
  25. //
  26. #import <Foundation/Foundation.h>
  27. #import <SystemConfiguration/SystemConfiguration.h>
  28. /** This is the notification name used in the Apple reachability example.
  29. * It is not used internally, and is merely a suggested notification name.
  30. */
  31. #define kAWSDefaultNetworkReachabilityChangedNotification @"kAWSNetworkReachabilityChangedNotification"
  32. @class AWSKSReachability;
  33. typedef void(^AWSKSReachabilityCallback)(AWSKSReachability* reachability);
  34. /** Monitors network connectivity.
  35. *
  36. * You can elect to be notified via blocks (onReachabilityChanged),
  37. * notifications (notificationName), or KVO (flags, reachable, and WWANOnly).
  38. *
  39. * All notification methods are disabled by default.
  40. *
  41. * Note: Upon construction, this object will fetch its initial reachability
  42. * state in the background. This means that the reachability status will ALWAYS
  43. * be "unreachable" until some time after object construction (possibly up to 10
  44. * seconds, depending on how long the DNS lookup takes). Use the "initialized"
  45. * property to monitor initialization, or set the callback "onInitializationComplete".
  46. */
  47. @interface AWSKSReachability : NSObject
  48. #pragma mark Constructors
  49. /** Reachability to a specific host. Returns nil if an initialization error occurs.
  50. *
  51. * @param hostname The name or IP address of the host to monitor. If nil or
  52. * empty string, check reachability to the internet in general.
  53. */
  54. + (AWSKSReachability*) reachabilityToHost:(NSString*) hostname;
  55. /** Reachability to the local (wired or wifi) network. Returns nil if an initialization error occurs.
  56. */
  57. + (AWSKSReachability*) reachabilityToLocalNetwork;
  58. /** Reachability to the internet. Returns nil if an initialization error occurs.
  59. */
  60. + (AWSKSReachability*) reachabilityToInternet;
  61. #pragma mark General Information
  62. /** The host we are monitoring reachability to, if any. */
  63. @property(nonatomic,readonly,retain) NSString* hostname;
  64. #pragma mark Notifications and Callbacks
  65. /** If non-nil, called when the KSReachability object has finished initializing.
  66. * If initialization has already completed, calls on the next main thread run loop.
  67. * This block will only be called once, and then discarded (released).
  68. * Block will be invoked on the main thread.
  69. */
  70. @property(atomic,readwrite,copy) AWSKSReachabilityCallback onInitializationComplete;
  71. /** If non-nil, called whenever reachability flags change.
  72. * Block will be invoked on the main thread.
  73. */
  74. @property(atomic,readwrite,copy) AWSKSReachabilityCallback onReachabilityChanged;
  75. /** The notification to send when reachability changes (nil = don't send).
  76. * Default = nil
  77. */
  78. @property(nonatomic,readwrite,retain) NSString* notificationName;
  79. #pragma mark KVO Compliant Status Properties
  80. /** The current reachability flags.
  81. * This property will always report 0 while "initialized" property = NO.
  82. */
  83. @property(nonatomic,readonly,assign) SCNetworkReachabilityFlags flags;
  84. /** Whether the host is reachable or not.
  85. * This property will always report NO while "initialized" property = NO.
  86. */
  87. @property(nonatomic,readonly,assign) BOOL reachable;
  88. /* If YES, the host is only reachable by WWAN (iOS only).
  89. * This property will always report NO while "initialized" property = NO.
  90. */
  91. @property(nonatomic,readonly,assign) BOOL WWANOnly;
  92. /** If YES, this object's status properties are valid. */
  93. @property(atomic,readonly,assign) BOOL initialized;
  94. @end
  95. /** A one-time operation to perform as soon as a host is deemed reachable.
  96. * The operation will only be performed once, regardless of how many times a
  97. * host becomes reachable.
  98. */
  99. @interface AWSKSReachableOperation: NSObject
  100. /** Constructor. Returns nil if an initialization error occurs.
  101. *
  102. * @param hostname The name or IP address of the host to monitor. If nil or
  103. * empty string, check reachability to the internet in general.
  104. * If hostname is a URL string, it will use the host portion.
  105. *
  106. * @param allowWWAN If NO, a WWAN-only connection is not enough to trigger
  107. * this operation.
  108. *
  109. * @param onReachabilityAchieved Invoke when the host becomes reachable.
  110. * This will be invoked ONE TIME ONLY, no matter
  111. * how many times reachability changes.
  112. * Block will be invoked on the main thread.
  113. */
  114. + (AWSKSReachableOperation*) operationWithHost:(NSString*) hostname
  115. allowWWAN:(BOOL) allowWWAN
  116. onReachabilityAchieved:(dispatch_block_t) onReachabilityAchieved;
  117. /** Constructor. Returns nil if an initialization error occurs.
  118. *
  119. * @param reachability A reachability instance. Note: This object will overwrite
  120. * the onReachabilityChanged property.
  121. *
  122. * @param allowWWAN If NO, a WWAN-only connection is not enough to trigger
  123. * this operation.
  124. *
  125. * @param onReachabilityAchieved Invoke when the host becomes reachable.
  126. * This will be invoked ONE TIME ONLY, no matter
  127. * how many times reachability changes.
  128. * Block will be invoked on the main thread.
  129. */
  130. + (AWSKSReachableOperation*) operationWithReachability:(AWSKSReachability*) reachability
  131. allowWWAN:(BOOL) allowWWAN
  132. onReachabilityAchieved:(dispatch_block_t) onReachabilityAchieved;
  133. /** Initializer. Returns nil if an initialization error occurs.
  134. *
  135. * @param hostname The name or IP address of the host to monitor. If nil or
  136. * empty string, check reachability to the internet in general.
  137. * If hostname is a URL string, it will use the host portion.
  138. *
  139. * @param allowWWAN If NO, a WWAN-only connection is not enough to trigger
  140. * this operation.
  141. *
  142. * @param onReachabilityAchieved Invoke when the host becomes reachable.
  143. * This will be invoked ONE TIME ONLY, no matter
  144. * how many times reachability changes.
  145. * Block will be invoked on the main thread.
  146. */
  147. - (id) initWithHost:(NSString*) hostname
  148. allowWWAN:(BOOL) allowWWAN
  149. onReachabilityAchieved:(dispatch_block_t) onReachabilityAchieved;
  150. /** Initializer. Returns nil if an initialization error occurs.
  151. *
  152. * @param reachability A reachability instance. Note: This object will overwrite
  153. * the onReachabilityChanged property.
  154. *
  155. * @param allowWWAN If NO, a WWAN-only connection is not enough to trigger
  156. * this operation.
  157. *
  158. * @param onReachabilityAchieved Invoke when the host becomes reachable.
  159. * This will be invoked ONE TIME ONLY, no matter
  160. * how many times reachability changes.
  161. * Block will be invoked on the main thread.
  162. */
  163. - (id) initWithReachability:(AWSKSReachability*) reachability
  164. allowWWAN:(BOOL) allowWWAN
  165. onReachabilityAchieved:(dispatch_block_t) onReachabilityAchieved;
  166. /** Access to internal reachability instance. Use this to monitor for errors. */
  167. @property(nonatomic,readonly,retain) AWSKSReachability* reachability;
  168. @end