Reachability.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. //! Project version number for MacOSReachability.
  26. FOUNDATION_EXPORT double ReachabilityVersionNumber;
  27. //! Project version string for MacOSReachability.
  28. FOUNDATION_EXPORT const unsigned char ReachabilityVersionString[];
  29. /**
  30. * Create NS_ENUM macro if it does not exist on the targeted version of iOS or OS X.
  31. *
  32. * @see http://nshipster.com/ns_enum-ns_options/
  33. **/
  34. #ifndef NS_ENUM
  35. #define NS_ENUM(_type, _name) enum _name : _type _name; enum _name : _type
  36. #endif
  37. extern NSString *const kReachabilityChangedNotification;
  38. typedef NS_ENUM(NSInteger, NetworkStatus) {
  39. // Apple NetworkStatus Compatible Names.
  40. NotReachable = 0,
  41. ReachableViaWiFi = 2,
  42. ReachableViaWWAN = 1
  43. };
  44. @class Reachability;
  45. typedef void (^NetworkReachable)(Reachability * reachability);
  46. typedef void (^NetworkUnreachable)(Reachability * reachability);
  47. typedef void (^NetworkReachability)(Reachability * reachability, SCNetworkConnectionFlags flags);
  48. @interface Reachability : NSObject
  49. @property (nonatomic, copy) NetworkReachable reachableBlock;
  50. @property (nonatomic, copy) NetworkUnreachable unreachableBlock;
  51. @property (nonatomic, copy) NetworkReachability reachabilityBlock;
  52. @property (nonatomic, assign) BOOL reachableOnWWAN;
  53. +(instancetype)reachabilityWithHostname:(NSString*)hostname;
  54. // This is identical to the function above, but is here to maintain
  55. //compatibility with Apples original code. (see .m)
  56. +(instancetype)reachabilityWithHostName:(NSString*)hostname;
  57. +(instancetype)reachabilityForInternetConnection;
  58. +(instancetype)reachabilityWithAddress:(void *)hostAddress;
  59. +(instancetype)reachabilityForLocalWiFi;
  60. +(instancetype)reachabilityWithURL:(NSURL*)url;
  61. -(instancetype)initWithReachabilityRef:(SCNetworkReachabilityRef)ref;
  62. -(BOOL)startNotifier;
  63. -(void)stopNotifier;
  64. -(BOOL)isReachable;
  65. -(BOOL)isReachableViaWWAN;
  66. -(BOOL)isReachableViaWiFi;
  67. // WWAN may be available, but not active until a connection has been established.
  68. // WiFi may require a connection for VPN on Demand.
  69. -(BOOL)isConnectionRequired; // Identical DDG variant.
  70. -(BOOL)connectionRequired; // Apple's routine.
  71. // Dynamic, on demand connection?
  72. -(BOOL)isConnectionOnDemand;
  73. // Is user intervention required?
  74. -(BOOL)isInterventionRequired;
  75. -(NetworkStatus)currentReachabilityStatus;
  76. -(SCNetworkReachabilityFlags)reachabilityFlags;
  77. -(NSString*)currentReachabilityString;
  78. -(NSString*)currentReachabilityFlags;
  79. @end