KSReachabilityKSCrash.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //
  2. // KSReachability.h
  3. //
  4. // Created by Karl Stenerud on 2012-05-05.
  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 "KSSystemCapabilities.h"
  28. #if KSCRASH_HAS_REACHABILITY
  29. #import <SystemConfiguration/SystemConfiguration.h>
  30. #endif
  31. /** This is the notification name used in the Apple reachability example. */
  32. #define kDefaultNetworkReachabilityChangedNotification @"kNetworkReachabilityChangedNotification"
  33. /** Monitors network connectivity.
  34. *
  35. * Note: Upon construction, this object will fetch its initial reachability
  36. * state in the background. This means that the reachability status will ALWAYS
  37. * be "unreachable" until some time after object construction. If you want
  38. * the true reachability state before the current code block ends, you can call
  39. * updateFlags. Note, however, that it will probably block.
  40. *
  41. * You can elect to be notified via blocks (onReachabilityChanged),
  42. * notifications (notificationName), or KVO (flags, reachable, and WWANOnly).
  43. *
  44. * All notification methods are disabled by default.
  45. */
  46. @interface KSReachabilityKSCrash : NSObject
  47. #pragma mark Constructors
  48. /** Reachability to a specific host.
  49. *
  50. * @param hostname The name or IP address of the host to monitor. If nil or
  51. * empty string, check reachability to the internet in general.
  52. */
  53. + (KSReachabilityKSCrash*) reachabilityToHost:(NSString*) hostname;
  54. /** Reachability to the local (wired or wifi) network.
  55. */
  56. + (KSReachabilityKSCrash*) reachabilityToLocalNetwork;
  57. #pragma mark General Information
  58. /** The host we are monitoring reachability to, if any. */
  59. @property(nonatomic,readonly,retain) NSString* hostname;
  60. #pragma mark Notifications and Callbacks
  61. /** If non-nil, called whenever reachability flags change.
  62. * Block will be invoked on the main thread.
  63. */
  64. @property(nonatomic,readwrite,copy) void(^onReachabilityChanged)(KSReachabilityKSCrash* reachability);
  65. /** The notification to send when reachability changes (nil = don't send).
  66. * Default = nil
  67. */
  68. @property(nonatomic,readwrite,retain) NSString* notificationName;
  69. #pragma mark KVO Compliant Status Properties
  70. /** The current reachability flags. */
  71. #if KSCRASH_HAS_REACHABILITY
  72. @property(nonatomic,readonly,assign) SCNetworkReachabilityFlags flags;
  73. #endif
  74. /** Whether the host is reachable or not. */
  75. @property(nonatomic,readonly,assign) BOOL reachable;
  76. /* If YES, the host is only reachable by WWAN (iOS only). */
  77. @property(nonatomic,readonly,assign) BOOL WWANOnly;
  78. #pragma mark Utility
  79. /** Force updating of the reachability flags.
  80. * This method will potentially block.
  81. *
  82. * @return YES if the flags were successfully updated.
  83. */
  84. - (BOOL) updateFlags;
  85. @end
  86. /** A one-time operation to perform as soon as a host is deemed reachable.
  87. * The operation will only be performed once, regardless of how many times a
  88. * host becomes reachable.
  89. */
  90. @interface KSReachableOperationKSCrash: NSObject
  91. /** Constructor.
  92. *
  93. * @param hostname The name or IP address of the host to monitor. If nil or
  94. * empty string, check reachability to the internet in general.
  95. * If hostname is a URL string, it will use the host portion.
  96. *
  97. * @param allowWWAN If NO, a WWAN-only connection is not enough to trigger
  98. * this operation.
  99. *
  100. * @param block The block to invoke when the host becomes reachable.
  101. * Block will be invoked on the main thread.
  102. */
  103. + (KSReachableOperationKSCrash*) operationWithHost:(NSString*) hostname
  104. allowWWAN:(BOOL) allowWWAN
  105. block:(void(^)(void)) block;
  106. /** Constructor.
  107. *
  108. * @param hostname The name or IP address of the host to monitor. If nil or
  109. * empty string, check reachability to the internet in general.
  110. * If hostname is a URL string, it will use the host portion.
  111. *
  112. * @param allowWWAN If NO, a WWAN-only connection is not enough to trigger
  113. * this operation.
  114. *
  115. * @param block The block to invoke when the host becomes reachable.
  116. * Block will be invoked on the main thread.
  117. */
  118. - (id) initWithHost:(NSString*) hostname
  119. allowWWAN:(BOOL) allowWWAN
  120. block:(void(^)(void)) block;
  121. @end