WFCCNetworkStatus.m 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //
  2. // WFCCNetworkStatus.m
  3. // WFChatClient
  4. //
  5. // Created by heavyrain on 2017/11/5.
  6. // Copyright © 2017年 WildFireChat. All rights reserved.
  7. //
  8. #import "WFCCNetworkStatus.h"
  9. #import <Foundation/Foundation.h>
  10. #import <SystemConfiguration/CaptiveNetwork.h>
  11. #import <SystemConfiguration/SCNetworkReachability.h>
  12. #import <netinet/in.h>
  13. SCNetworkReachabilityRef g_Reach = nil;
  14. static void ReachCallback(SCNetworkReachabilityRef target, SCNetworkConnectionFlags flags, void* info)
  15. {
  16. @autoreleasepool {
  17. [(__bridge id)info performSelector:@selector(ChangeReach)];
  18. }
  19. }
  20. @implementation WFCCNetworkStatus
  21. static WFCCNetworkStatus * sharedSingleton = nil;
  22. + (WFCCNetworkStatus*)sharedInstance {
  23. @synchronized (self) {
  24. if (sharedSingleton == nil) {
  25. sharedSingleton = [[WFCCNetworkStatus alloc] init];
  26. }
  27. }
  28. return sharedSingleton;
  29. }
  30. -(void) Start:(__unsafe_unretained id<WFCCNetworkStatusDelegate>)delWFCNetworkStatus {
  31. m_delWFCNetworkStatus = delWFCNetworkStatus;
  32. if (g_Reach == nil) {
  33. struct sockaddr_in zeroAddress;
  34. bzero(&zeroAddress, sizeof(zeroAddress));
  35. zeroAddress.sin_len = sizeof(zeroAddress);
  36. zeroAddress.sin_family = AF_INET;
  37. g_Reach = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (struct sockaddr *)&zeroAddress);
  38. }
  39. SCNetworkReachabilityContext context = {0, (__bridge void *)self, NULL, NULL, NULL};
  40. if(SCNetworkReachabilitySetCallback(g_Reach, ReachCallback, &context)) {
  41. if(!SCNetworkReachabilityScheduleWithRunLoop(g_Reach, CFRunLoopGetCurrent(), kCFRunLoopCommonModes)) {
  42. SCNetworkReachabilitySetCallback(g_Reach, NULL, NULL);
  43. return;
  44. }
  45. }
  46. }
  47. -(void) Stop {
  48. if(g_Reach != nil) {
  49. SCNetworkReachabilitySetCallback(g_Reach, NULL, NULL);
  50. SCNetworkReachabilityUnscheduleFromRunLoop(g_Reach, CFRunLoopGetCurrent(), kCFRunLoopCommonModes);
  51. CFRelease(g_Reach);
  52. g_Reach = nil;
  53. }
  54. m_delWFCNetworkStatus = nil;
  55. }
  56. - (bool)isConnectionAvaible {
  57. SCNetworkConnectionFlags connFlags = [self connFlags];
  58. return (connFlags & kSCNetworkFlagsReachable) || (connFlags & kSCNetworkFlagsConnectionRequired);
  59. }
  60. - (SCNetworkConnectionFlags)connFlags {
  61. SCNetworkConnectionFlags connFlags;
  62. if(g_Reach == nil || !SCNetworkReachabilityGetFlags(g_Reach, &connFlags)) {
  63. return 0;
  64. }
  65. return connFlags;
  66. }
  67. -(void) ChangeReach {
  68. SCNetworkConnectionFlags connFlags = [self connFlags];
  69. if(m_delWFCNetworkStatus != nil) {
  70. [m_delWFCNetworkStatus ReachabilityChange:connFlags];
  71. }
  72. }
  73. @end