BUReachability.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. File: BUReachability.h
  3. Abstract: Basic demonstration of how to use the SystemConfiguration Reachablity APIs.
  4. Version: 3.5
  5. Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
  6. Inc. ("Apple") in consideration of your agreement to the following
  7. terms, and your use, installation, modification or redistribution of
  8. this Apple software constitutes acceptance of these terms. If you do
  9. not agree with these terms, please do not use, install, modify or
  10. redistribute this Apple software.
  11. In consideration of your agreement to abide by the following terms, and
  12. subject to these terms, Apple grants you a personal, non-exclusive
  13. license, under Apple's copyrights in this original Apple software (the
  14. "Apple Software"), to use, reproduce, modify and redistribute the Apple
  15. Software, with or without modifications, in source and/or binary forms;
  16. provided that if you redistribute the Apple Software in its entirety and
  17. without modifications, you must retain this notice and the following
  18. text and disclaimers in all such redistributions of the Apple Software.
  19. Neither the name, trademarks, service marks or logos of Apple Inc. may
  20. be used to endorse or promote products derived from the Apple Software
  21. without specific prior written permission from Apple. Except as
  22. expressly stated in this notice, no other rights or licenses, express or
  23. implied, are granted by Apple herein, including but not limited to any
  24. patent rights that may be infringed by your derivative works or by other
  25. works in which the Apple Software may be incorporated.
  26. The Apple Software is provided by Apple on an "AS IS" basis. APPLE
  27. MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
  28. THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
  29. FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
  30. OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
  31. IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
  32. OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  33. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  34. INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
  35. MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
  36. AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
  37. STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
  38. POSSIBILITY OF SUCH DAMAGE.
  39. Copyright (C) 2014 Apple Inc. All Rights Reserved.
  40. */
  41. #import <Foundation/Foundation.h>
  42. #import <SystemConfiguration/SystemConfiguration.h>
  43. #import <netinet/in.h>
  44. typedef NS_ENUM(NSInteger, BUNetWorkTypeCode) {
  45. // 初始状态,未连接
  46. BUNetWorkTypeCode_None = -1,
  47. // 未知
  48. BUNetWorkTypeCode_Unknown = 0,
  49. BUNetWorkTypeCode_Mobile = 1,
  50. BUNetWorkTypeCode_2G = 2,
  51. BUNetWorkTypeCode_3G = 3,
  52. BUNetWorkTypeCode_Wifi = 4,
  53. BUNetWorkTypeCode_4G = 5,
  54. BUNetWorkTypeCode_5G = 6,
  55. };
  56. typedef NS_ENUM(NSInteger) {
  57. // 程序无法判断出 App 的网络权限设置
  58. kBUNetworkAuthorizationStatusCantDetermined = 0,
  59. // App 未开启蜂窝数据网络权限
  60. kBUNetworkAuthorizationStatusCellNotPermitted,
  61. // App 未开启无线局域网与蜂窝移动网络权限,此项仅可能在国行 iPhone 手机上出现
  62. kBUNetworkAuthorizationStatusWLANAndCellNotPermitted,
  63. } kBUNetworkAuthorizationStatus;
  64. extern NSString *kBUReachabilityChangedNotification;
  65. // 是否开启 “网络状态检测” 的优化,需要外部调用方以 [[NSUserDefaults standardUserDefaults] setBool:forKey:] 方式写入
  66. extern NSString * const BUReachabilityDetectOptimizeKey;
  67. @interface BUReachability : NSObject
  68. /**
  69. 判断指定域名的连通性
  70. @param hostName 域名
  71. @return BUReachability对象
  72. */
  73. + (instancetype)reachabilityWithHostName:(NSString *)hostName;
  74. /**
  75. 判断指定IP地址的连通性
  76. @param hostAddress IP地址
  77. @return BUReachability对象
  78. */
  79. + (instancetype)reachabilityWithAddress:(const struct sockaddr_in *)hostAddress;
  80. /**
  81. 判断网络默认连接的连通性(应用连接到指定host主机时除外)
  82. @return BUReachability对象
  83. */
  84. + (instancetype)reachabilityForInternetConnection;
  85. /**
  86. 开始在当前runloop监听连通性通知
  87. @return 是否成功开始监听
  88. */
  89. - (BOOL)startNotifier;
  90. /**
  91. 结束监听连通性通知
  92. */
  93. - (void)stopNotifier;
  94. /**
  95. 当前连通性状态
  96. @return NetworkStatus枚举值
  97. */
  98. - (BUNetWorkTypeCode)currentReachabilityStatus;
  99. /**
  100. 是否需要连接。如WWAN需要首先建立一个可用连接才能被激活。WiFi可能需要一个VPN连接等
  101. @return 是否需要连接
  102. */
  103. - (BOOL)connectionRequired;
  104. /**
  105. 获取 App 当前的网络权限设置状态
  106. 因苹果尚未提供网络权限判断的 API,此方法目前只是在网络 NotReachable 时检查系统连接状态,
  107. 利用排除法推断出 App 当前没有 蜂窝数据网络权限 或者 WIFI及蜂窝数据网络权限,排除原理参见
  108. https://wiki.bytedance.net/pages/viewpage.action?pageId=107808003
  109. 在其它情况下(例如网络 Reachable 或者飞行模式)均返回 CantDetermined,上层业务调用方需注意
  110. @return 参见 kBUNetworkAuthorizationStatus 定义
  111. */
  112. - (kBUNetworkAuthorizationStatus)currentNetworkAuthorizationStatus;
  113. @end
  114. @interface BUReachability (Cellular)
  115. /**
  116. 2G网络是否连通
  117. @return 是否连通
  118. */
  119. + (BOOL)is2GConnected;
  120. /**
  121. 3G网络是否连通
  122. @return 是否连通
  123. */
  124. + (BOOL)is3GConnected;
  125. /**
  126. 4G网络是否连通
  127. @return 是否连通
  128. */
  129. + (BOOL)is4GConnected;
  130. /**
  131. 2017.5.22添加
  132. 借鉴微信方式判断当前网络是否连通
  133. @return 网络是否可以联调
  134. */
  135. + (BOOL)isNetworkConnected;
  136. @end