RealReachability.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //
  2. // RealReachability.h
  3. // Version 1.3.0
  4. //
  5. // Created by Dustturtle on 16/1/9.
  6. // Copyright (c) 2016 Dustturtle. All rights reserved.
  7. //
  8. // This code is distributed under the terms and conditions of the MIT license.
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining a copy
  11. // of this software and associated documentation files (the "Software"), to deal
  12. // in the Software without restriction, including without limitation the rights
  13. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. // copies of the Software, and to permit persons to whom the Software is
  15. // furnished to do so, subject to the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be included in
  18. // all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  26. // THE SOFTWARE.
  27. //
  28. #import <Foundation/Foundation.h>
  29. #import "LocalConnection.h"
  30. #define GLobalRealReachability [RealReachability sharedInstance]
  31. ///This notification was called only when reachability really changed;
  32. ///We use FSM to promise this for you;
  33. ///We post self to this notification, then you can invoke currentReachabilityStatus method to fetch current status.
  34. extern NSString *const kRealReachabilityChangedNotification;
  35. extern NSString *const kRRVPNStatusChangedNotification;
  36. typedef NS_ENUM(NSInteger, ReachabilityStatus) {
  37. ///Direct match with Apple networkStatus, just a force type convert.
  38. RealStatusUnknown = -1,
  39. RealStatusNotReachable = 0,
  40. RealStatusViaWWAN = 1,
  41. RealStatusViaWiFi = 2
  42. };
  43. typedef NS_ENUM(NSInteger, WWANAccessType) {
  44. WWANTypeUnknown = -1, /// maybe iOS6
  45. WWANType5G = 2,
  46. WWANType4G = 0,
  47. WWANType3G = 1,
  48. WWANType2G = 3
  49. };
  50. @protocol RealReachabilityDelegate <NSObject>
  51. @optional
  52. /// TODO:通过挂载一个定制的代理请求来检查网络,需要用户自己实现,我们会给出一个示例。
  53. /// 可以通过这种方式规避解决http可用但icmp被阻止的场景下框架判断不正确的问题。
  54. /// (Update: 已经添加了判断VPN的相关逻辑,以解决这种场景下大概率误判的问题)
  55. /// 此方法阻塞?同步返回?还是异步?如果阻塞主线程超过n秒是不行的。
  56. /// 当CustomAgent的doubleCheck被启用时,ping的doubleCheck将不再工作。
  57. /// TODO: We introduce a custom agent to check the network by making http request, that need
  58. /// the user to achieve this.
  59. /// We want to solve the issue on special case(http available but icmp prevented).
  60. /// NOTE: When the double check of the custom agent was used, the double check by ping will work no longer.
  61. - (BOOL)doubleCheckByCustomAgent;
  62. @end
  63. @interface RealReachability : NSObject
  64. // local connection observer
  65. @property (nonatomic, strong) LocalConnection *localObserver;
  66. /// Please make sure this host is available for pinging! default host:www.apple.com
  67. @property (nonatomic, copy) NSString *hostForPing;
  68. @property (nonatomic, copy) NSString *hostForCheck;
  69. /// Interval in minutes; default is 2.0f, suggest value from 0.3f to 60.0f;
  70. /// If exceeded, the value will be reset to 0.3f or 60.0f (the closer one).
  71. @property (nonatomic, assign) float autoCheckInterval;
  72. // Timeout used for ping. Default is 2 seconds
  73. @property (nonatomic, assign) NSTimeInterval pingTimeout;
  74. // Latency from latest ping result
  75. @property (nonatomic, assign) NSTimeInterval latency;
  76. + (instancetype)sharedInstance;
  77. - (void)startNotifier;
  78. - (void)stopNotifier;
  79. /**
  80. * To get real reachability we need to do async request,
  81. * then we use the block blow for invoker to handle business request(need real reachability).
  82. * Now we have introduced a double check to make our result more reliable.
  83. *
  84. * @param asyncHandler async request handler, return in 5 seconds(max limit).
  85. * The limit time may be adjusted later for better experience.
  86. */
  87. - (void)reachabilityWithBlock:(void (^)(ReachabilityStatus status))asyncHandler;
  88. /**
  89. * Return current reachability immediately.
  90. *
  91. * @return see enum LocalConnectionStatus
  92. */
  93. - (ReachabilityStatus)currentReachabilityStatus;
  94. /**
  95. * Return previous reachability status.
  96. *
  97. * @return see enum LocalConnectionStatus
  98. */
  99. - (ReachabilityStatus)previousReachabilityStatus;
  100. /**
  101. * Return current WWAN type immediately.
  102. *
  103. * @return unknown/5g/4g/3g/2g.
  104. *
  105. * This method can be used to improve app's further network performance
  106. * (different strategies for different WWAN types).
  107. */
  108. - (WWANAccessType)currentWWANtype;
  109. /**
  110. * Sometimes people use VPN on the device.
  111. * In this situation we need to ignore the ping error.
  112. * (VPN usually do not support ICMP.)
  113. *
  114. * @return current VPN status: YES->ON, NO->OFF.
  115. *
  116. * This method can be used to improve app's further network performance
  117. * (different strategies for different WWAN types).
  118. */
  119. - (BOOL)isVPNOn;
  120. @end