Reachability.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. Copyright (c) 2011, Tony Million.
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. 1. Redistributions of source code must retain the above copyright notice, this
  7. list of conditions and the following disclaimer.
  8. 2. Redistributions in binary form must reproduce the above copyright notice,
  9. this list of conditions and the following disclaimer in the documentation
  10. and/or other materials provided with the distribution.
  11. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  12. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  13. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  14. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  15. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  16. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  17. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  18. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  19. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  20. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  21. POSSIBILITY OF SUCH DAMAGE.
  22. */
  23. #import <Foundation/Foundation.h>
  24. #import <SystemConfiguration/SystemConfiguration.h>
  25. /**
  26. * Create NS_ENUM macro if it does not exist on the targeted version of iOS or OS X.
  27. *
  28. * @see http://nshipster.com/ns_enum-ns_options/
  29. **/
  30. #ifndef NS_ENUM
  31. #define NS_ENUM(_type, _name) enum _name : _type _name; enum _name : _type
  32. #endif
  33. extern NSString *const kReachabilityChangedNotification;
  34. typedef NS_ENUM(NSInteger, NetworkStatus) {
  35. // Apple NetworkStatus Compatible Names.
  36. NotReachable = 0,
  37. ReachableViaWiFi = 2,
  38. ReachableViaWWAN = 1
  39. };
  40. @class Reachability;
  41. typedef void (^NetworkReachable)(Reachability * reachability);
  42. typedef void (^NetworkUnreachable)(Reachability * reachability);
  43. @interface Reachability : NSObject
  44. @property (nonatomic, copy) NetworkReachable reachableBlock;
  45. @property (nonatomic, copy) NetworkUnreachable unreachableBlock;
  46. @property (nonatomic, assign) BOOL reachableOnWWAN;
  47. +(Reachability*)reachabilityWithHostname:(NSString*)hostname;
  48. // This is identical to the function above, but is here to maintain
  49. //compatibility with Apples original code. (see .m)
  50. +(Reachability*)reachabilityWithHostName:(NSString*)hostname;
  51. +(Reachability*)reachabilityForInternetConnection;
  52. +(Reachability*)reachabilityWithAddress:(void *)hostAddress;
  53. +(Reachability*)reachabilityForLocalWiFi;
  54. -(Reachability *)initWithReachabilityRef:(SCNetworkReachabilityRef)ref;
  55. -(BOOL)startNotifier;
  56. -(void)stopNotifier;
  57. -(BOOL)isReachable;
  58. -(BOOL)isReachableViaWWAN;
  59. -(BOOL)isReachableViaWiFi;
  60. // WWAN may be available, but not active until a connection has been established.
  61. // WiFi may require a connection for VPN on Demand.
  62. -(BOOL)isConnectionRequired; // Identical DDG variant.
  63. -(BOOL)connectionRequired; // Apple's routine.
  64. // Dynamic, on demand connection?
  65. -(BOOL)isConnectionOnDemand;
  66. // Is user intervention required?
  67. -(BOOL)isInterventionRequired;
  68. -(NetworkStatus)currentReachabilityStatus;
  69. -(SCNetworkReachabilityFlags)reachabilityFlags;
  70. -(NSString*)currentReachabilityString;
  71. -(NSString*)currentReachabilityFlags;
  72. @end