Util.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. //
  2. // Util.m
  3. // Miaxis
  4. //
  5. // Created by tongjun on 14-3-5.
  6. // Copyright (c) 2014年 tongjun. All rights reserved.
  7. //
  8. #import "Util.h"
  9. //#import <SystemConfiguration/SystemConfiguration.h>
  10. //#import <netdb.h>
  11. #import <RealReachability/RealReachability.h>
  12. @implementation Util
  13. //判断网络是否连接(弃用2017/2/12)
  14. //- (BOOL) connectedToNetWork
  15. //{
  16. // @try {
  17. // //创建零地址,0.0.0.0的地址表示查询本机的网络连接状态
  18. // struct sockaddr_in zeroAddress;
  19. // bzero(&zeroAddress, sizeof(zeroAddress));
  20. // zeroAddress.sin_len = sizeof(zeroAddress);
  21. // zeroAddress.sin_family = AF_INET;
  22. //
  23. // /**
  24. // * SCNetworkReachabilityRef: 用来保存创建测试连接返回的引用
  25. // *
  26. // * SCNetworkReachabilityCreateWithAddress: 根据传入的地址测试连接.
  27. // * 第一个参数可以为NULL或kCFAllocatorDefault
  28. // * 第二个参数为需要测试连接的IP地址,当为0.0.0.0时则可以查询本机的网络连接状态
  29. // * 同时返回一个引用必须在用完后释放
  30. // * PS: SCNetworkReachabilityCreateWithName: 这是个根据传入的网络测试连接,
  31. // * 第二个参数比如为“www.apple.com”,其他和上一个一样
  32. // *
  33. // * SCNetworkReachabilityGetFlags:这个函数用来获得测试连接的状态,
  34. // * 第一个参数为之前建立的测试连接的引用,
  35. // * 第二个参数用来保存获得的状态,
  36. // * 如果能获得状态则返回TRUE,否则返回FALSE。
  37. // *
  38. // */
  39. //
  40. // SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
  41. // SCNetworkReachabilityFlags flags;
  42. //
  43. // BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
  44. // CFRelease(defaultRouteReachability);
  45. //
  46. // if (!didRetrieveFlags) {
  47. // printf("Error. Could not recover network reachability flags");
  48. // return NO;
  49. // }
  50. //
  51. // /**
  52. // * kSCNetworkReachabilityFlagsReachable: 能够连接网络
  53. // * kSCNetworkReachabilityFlagsConnectionRequired: 能够连接网络,但是首先得建立连接过程
  54. // * kSCNetworkReachabilityFlagsIsWWAN: 判断是否通过蜂窝网覆盖的连接,
  55. // * 比如EDGE,GPRS或者目前的3G.主要是区别通过WiFi的连接
  56. // */
  57. // BOOL isReachable = ((flags & kSCNetworkReachabilityFlagsReachable) != 0);
  58. // BOOL needsConnection = ((flags & kSCNetworkReachabilityFlagsConnectionRequired) != 0);
  59. //
  60. // return (isReachable && !needsConnection) ? YES : NO;
  61. // }
  62. // @catch (NSException *exception) {
  63. // return NO;
  64. // }
  65. //}
  66. //判断网络是否连接
  67. + (BOOL) connectedToNetWork{
  68. //需要确保该host地址可以被ping探测到。此操作为可选,默认的目标host地址为www.baidu.com
  69. //GLobalRealReachability.hostForPing = @"www.baidu.com";
  70. //手动设置自动探测间隔(autoCheckInterval,单位为分钟).此处根据应用的实时性要求来设置自动探测的间隔,实时性要求较高时可适当调低此参数,但不建议设置为1.0以下
  71. //GLobalRealReachability.autoCheckInterval = 2.0f;
  72. //----------------------------这一块先埋伏在这里吧-----------------------@lee
  73. // [GLobalRealReachability reachabilityWithBlock:^(ReachabilityStatus status)
  74. // {
  75. // switch (status)
  76. // {
  77. // case RealStatusUnknown:
  78. // {
  79. // //NSLog(@"Unknown");
  80. // break;
  81. // }
  82. //
  83. // case RealStatusNotReachable:
  84. // {
  85. // //NSLog(@"NotReachable");
  86. // break;
  87. // }
  88. //
  89. // case RealStatusViaWiFi:
  90. // {
  91. // //NSLog(@"wifi");
  92. // break;
  93. // }
  94. //
  95. // case RealStatusViaWWAN:
  96. // {
  97. // //NSLog(@"ViaWWAN");
  98. // WWANAccessType accessType = [GLobalRealReachability currentWWANtype];
  99. // if (accessType == WWANType2G)
  100. // {
  101. // //NSLog(@"RealReachabilityStatus2G");
  102. // }
  103. // else if (accessType == WWANType3G)
  104. // {
  105. // //NSLog(@"RealReachabilityStatus3G");
  106. // }
  107. // else if (accessType == WWANType4G)
  108. // {
  109. // //NSLog(@"RealReachabilityStatus4G");
  110. // }
  111. // else
  112. // {
  113. // //NSLog(@"Unknown RealReachability WWAN Status, might be iOS6");
  114. // }
  115. //
  116. //
  117. // break;
  118. // }
  119. // default:
  120. // break;
  121. // }
  122. // }];
  123. [GLobalRealReachability startNotifier];
  124. ReachabilityStatus status = [GLobalRealReachability currentReachabilityStatus];
  125. [GLobalRealReachability stopNotifier];
  126. return status ? YES : NO;
  127. }
  128. +(NSString *)getAnswerByTen:(NSInteger)value
  129. {
  130. NSMutableString *string = [NSMutableString string];
  131. while (value)
  132. {
  133. [string insertString:(value & 1)? @"1": @"0" atIndex:0];
  134. value /= 2;
  135. }
  136. if (string.length==5) {
  137. string=[NSMutableString stringWithFormat:@"000%@",string];
  138. }else if(string.length==6){
  139. string=[NSMutableString stringWithFormat:@"00%@",string];
  140. }else if(string.length==7){
  141. string=[NSMutableString stringWithFormat:@"0%@",string];
  142. }else{
  143. string=string;
  144. }
  145. NSMutableString *answer=[[NSMutableString alloc]init];
  146. if ([[string substringWithRange:NSMakeRange(3, 1)]intValue]==1) {
  147. [answer appendString:@"A"];
  148. }
  149. if ([[string substringWithRange:NSMakeRange(2, 1)]intValue]==1) {
  150. [answer appendString:@"B"];
  151. }
  152. if ([[string substringWithRange:NSMakeRange(1, 1)]intValue]==1) {
  153. [answer appendString:@"C"];
  154. }
  155. if ([[string substringWithRange:NSMakeRange(0, 1)]intValue]==1) {
  156. [answer appendString:@"D"];
  157. }
  158. return [answer copy];
  159. }
  160. @end