KSYReachability.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //
  2. // KSYReachability.h
  3. // KSYCommon
  4. //
  5. // Created by ksyun on 2017/1/4.
  6. // Copyright © 2017年 ksyun. All rights reserved.
  7. //
  8. #ifndef _KSYReachability_H_
  9. #define _KSYReachability_H_
  10. #import <Foundation/Foundation.h>
  11. #import <SystemConfiguration/SystemConfiguration.h>
  12. /**
  13. * Create NS_ENUM macro if it does not exist on the targeted version of iOS or OS X.
  14. *
  15. * @see http://nshipster.com/ns_enum-ns_options/
  16. **/
  17. #ifndef NS_ENUM
  18. #define NS_ENUM(_type, _name) enum _name : _type _name; enum _name : _type
  19. #endif
  20. extern NSString *const kKSYReachabilityChangedNotification;
  21. typedef NS_ENUM(NSInteger, KSYNetworkStatus) {
  22. // same as Apple NetworkStatus
  23. KSYNotReachable = 0,
  24. KSYReachableViaWiFi = 2,
  25. KSYReachableViaWWAN = 1
  26. };
  27. @class KSYReachability;
  28. typedef void (^KSYNetworkReachable)(KSYReachability * reachability);
  29. typedef void (^KSYNetworkUnreachable)(KSYReachability * reachability);
  30. typedef void (^KSYNetworkReachability)(KSYReachability * reachability, SCNetworkConnectionFlags flags);
  31. @interface KSYReachability : NSObject
  32. @property (nonatomic, copy) KSYNetworkReachable reachableBlock;
  33. @property (nonatomic, copy) KSYNetworkUnreachable unreachableBlock;
  34. @property (nonatomic, copy) KSYNetworkReachability reachabilityBlock;
  35. @property (nonatomic, assign) BOOL reachableOnWWAN;
  36. +(instancetype)reachabilityWithHostname:(NSString*)hostname;
  37. // This is identical to the function above, but is here to maintain
  38. //compatibility with Apples original code. (see .m)
  39. +(instancetype)reachabilityWithHostName:(NSString*)hostname;
  40. +(instancetype)reachabilityForInternetConnection;
  41. +(instancetype)reachabilityWithAddress:(void *)hostAddress;
  42. +(instancetype)reachabilityForLocalWiFi;
  43. -(instancetype)initWithReachabilityRef:(SCNetworkReachabilityRef)ref;
  44. -(BOOL)startNotifier;
  45. -(void)stopNotifier;
  46. -(BOOL)isReachable;
  47. -(BOOL)isReachableViaWWAN;
  48. -(BOOL)isReachableViaWiFi;
  49. // WWAN may be available, but not active until a connection has been established.
  50. // WiFi may require a connection for VPN on Demand.
  51. -(BOOL)isConnectionRequired; // Identical DDG variant.
  52. -(BOOL)connectionRequired; // Apple's routine.
  53. // Dynamic, on demand connection?
  54. -(BOOL)isConnectionOnDemand;
  55. // Is user intervention required?
  56. -(BOOL)isInterventionRequired;
  57. -(KSYNetworkStatus)currentReachabilityStatus;
  58. -(SCNetworkReachabilityFlags)reachabilityFlags;
  59. -(NSString*)currentReachabilityString;
  60. -(NSString*)currentReachabilityFlags;
  61. @end
  62. #endif // _KSYReachability_H_