Reachability.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. #import <sys/socket.h>
  26. #import <netinet/in.h>
  27. #import <netinet6/in6.h>
  28. #import <arpa/inet.h>
  29. #import <ifaddrs.h>
  30. #import <netdb.h>
  31. /**
  32. * Does ARC support support GCD objects?
  33. * It does if the minimum deployment target is iOS 6+ or Mac OS X 8+
  34. **/
  35. #if TARGET_OS_IPHONE
  36. // Compiling for iOS
  37. #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 60000 // iOS 6.0 or later
  38. #define NEEDS_DISPATCH_RETAIN_RELEASE 0
  39. #else // iOS 5.X or earlier
  40. #define NEEDS_DISPATCH_RETAIN_RELEASE 1
  41. #endif
  42. #else
  43. // Compiling for Mac OS X
  44. #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1080 // Mac OS X 10.8 or later
  45. #define NEEDS_DISPATCH_RETAIN_RELEASE 0
  46. #else
  47. #define NEEDS_DISPATCH_RETAIN_RELEASE 1 // Mac OS X 10.7 or earlier
  48. #endif
  49. #endif
  50. extern NSString *const kReachabilityChangedNotification;
  51. typedef enum
  52. {
  53. // Apple NetworkStatus Compatible Names.
  54. NotReachable = 0,
  55. ReachableViaWiFi = 2,
  56. ReachableViaWWAN = 1
  57. } NetworkStatus;
  58. @class Reachability;
  59. typedef void (^NetworkReachable)(Reachability * reachability);
  60. typedef void (^NetworkUnreachable)(Reachability * reachability);
  61. @interface Reachability : NSObject
  62. @property (nonatomic, copy) NetworkReachable reachableBlock;
  63. @property (nonatomic, copy) NetworkUnreachable unreachableBlock;
  64. @property (nonatomic, assign) BOOL reachableOnWWAN;
  65. +(Reachability*)reachabilityWithHostname:(NSString*)hostname;
  66. +(Reachability*)reachabilityForInternetConnection;
  67. +(Reachability*)reachabilityWithAddress:(const struct sockaddr_in*)hostAddress;
  68. +(Reachability*)reachabilityForLocalWiFi;
  69. -(Reachability *)initWithReachabilityRef:(SCNetworkReachabilityRef)ref;
  70. -(BOOL)startNotifier;
  71. -(void)stopNotifier;
  72. -(BOOL)isReachable;
  73. -(BOOL)isReachableViaWWAN;
  74. -(BOOL)isReachableViaWiFi;
  75. // WWAN may be available, but not active until a connection has been established.
  76. // WiFi may require a connection for VPN on Demand.
  77. -(BOOL)isConnectionRequired; // Identical DDG variant.
  78. -(BOOL)connectionRequired; // Apple's routine.
  79. // Dynamic, on demand connection?
  80. -(BOOL)isConnectionOnDemand;
  81. // Is user intervention required?
  82. -(BOOL)isInterventionRequired;
  83. -(NetworkStatus)currentReachabilityStatus;
  84. -(SCNetworkReachabilityFlags)reachabilityFlags;
  85. -(NSString*)currentReachabilityString;
  86. -(NSString*)currentReachabilityFlags;
  87. @end