123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- //
- // Util.m
- // Miaxis
- //
- // Created by tongjun on 14-3-5.
- // Copyright (c) 2014年 tongjun. All rights reserved.
- //
- #import "Util.h"
- //#import <SystemConfiguration/SystemConfiguration.h>
- //#import <netdb.h>
- #import <RealReachability/RealReachability.h>
- @implementation Util
- //判断网络是否连接(弃用2017/2/12)
- //- (BOOL) connectedToNetWork
- //{
- // @try {
- // //创建零地址,0.0.0.0的地址表示查询本机的网络连接状态
- // struct sockaddr_in zeroAddress;
- // bzero(&zeroAddress, sizeof(zeroAddress));
- // zeroAddress.sin_len = sizeof(zeroAddress);
- // zeroAddress.sin_family = AF_INET;
- //
- // /**
- // * SCNetworkReachabilityRef: 用来保存创建测试连接返回的引用
- // *
- // * SCNetworkReachabilityCreateWithAddress: 根据传入的地址测试连接.
- // * 第一个参数可以为NULL或kCFAllocatorDefault
- // * 第二个参数为需要测试连接的IP地址,当为0.0.0.0时则可以查询本机的网络连接状态
- // * 同时返回一个引用必须在用完后释放
- // * PS: SCNetworkReachabilityCreateWithName: 这是个根据传入的网络测试连接,
- // * 第二个参数比如为“www.apple.com”,其他和上一个一样
- // *
- // * SCNetworkReachabilityGetFlags:这个函数用来获得测试连接的状态,
- // * 第一个参数为之前建立的测试连接的引用,
- // * 第二个参数用来保存获得的状态,
- // * 如果能获得状态则返回TRUE,否则返回FALSE。
- // *
- // */
- //
- // SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
- // SCNetworkReachabilityFlags flags;
- //
- // BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
- // CFRelease(defaultRouteReachability);
- //
- // if (!didRetrieveFlags) {
- // printf("Error. Could not recover network reachability flags");
- // return NO;
- // }
- //
- // /**
- // * kSCNetworkReachabilityFlagsReachable: 能够连接网络
- // * kSCNetworkReachabilityFlagsConnectionRequired: 能够连接网络,但是首先得建立连接过程
- // * kSCNetworkReachabilityFlagsIsWWAN: 判断是否通过蜂窝网覆盖的连接,
- // * 比如EDGE,GPRS或者目前的3G.主要是区别通过WiFi的连接
- // */
- // BOOL isReachable = ((flags & kSCNetworkReachabilityFlagsReachable) != 0);
- // BOOL needsConnection = ((flags & kSCNetworkReachabilityFlagsConnectionRequired) != 0);
- //
- // return (isReachable && !needsConnection) ? YES : NO;
- // }
- // @catch (NSException *exception) {
- // return NO;
- // }
- //}
- //判断网络是否连接
- + (BOOL) connectedToNetWork{
- //需要确保该host地址可以被ping探测到。此操作为可选,默认的目标host地址为www.baidu.com
- //GLobalRealReachability.hostForPing = @"www.baidu.com";
- //手动设置自动探测间隔(autoCheckInterval,单位为分钟).此处根据应用的实时性要求来设置自动探测的间隔,实时性要求较高时可适当调低此参数,但不建议设置为1.0以下
- //GLobalRealReachability.autoCheckInterval = 2.0f;
-
- //----------------------------这一块先埋伏在这里吧-----------------------@lee
-
- // [GLobalRealReachability reachabilityWithBlock:^(ReachabilityStatus status)
- // {
-
- // switch (status)
- // {
- // case RealStatusUnknown:
- // {
- // //NSLog(@"Unknown");
- // break;
- // }
- //
- // case RealStatusNotReachable:
- // {
- // //NSLog(@"NotReachable");
- // break;
- // }
- //
- // case RealStatusViaWiFi:
- // {
- // //NSLog(@"wifi");
- // break;
- // }
- //
- // case RealStatusViaWWAN:
- // {
- // //NSLog(@"ViaWWAN");
- // WWANAccessType accessType = [GLobalRealReachability currentWWANtype];
- // if (accessType == WWANType2G)
- // {
- // //NSLog(@"RealReachabilityStatus2G");
- // }
- // else if (accessType == WWANType3G)
- // {
- // //NSLog(@"RealReachabilityStatus3G");
- // }
- // else if (accessType == WWANType4G)
- // {
- // //NSLog(@"RealReachabilityStatus4G");
- // }
- // else
- // {
- // //NSLog(@"Unknown RealReachability WWAN Status, might be iOS6");
- // }
- //
- //
- // break;
- // }
- // default:
- // break;
- // }
- // }];
- [GLobalRealReachability startNotifier];
- ReachabilityStatus status = [GLobalRealReachability currentReachabilityStatus];
- [GLobalRealReachability stopNotifier];
- return status ? YES : NO;
- }
- +(NSString *)getAnswerByTen:(NSInteger)value
- {
- NSMutableString *string = [NSMutableString string];
- while (value)
- {
- [string insertString:(value & 1)? @"1": @"0" atIndex:0];
- value /= 2;
- }
- if (string.length==5) {
- string=[NSMutableString stringWithFormat:@"000%@",string];
- }else if(string.length==6){
- string=[NSMutableString stringWithFormat:@"00%@",string];
- }else if(string.length==7){
- string=[NSMutableString stringWithFormat:@"0%@",string];
- }else{
- string=string;
- }
- NSMutableString *answer=[[NSMutableString alloc]init];
- if ([[string substringWithRange:NSMakeRange(3, 1)]intValue]==1) {
- [answer appendString:@"A"];
- }
- if ([[string substringWithRange:NSMakeRange(2, 1)]intValue]==1) {
- [answer appendString:@"B"];
- }
- if ([[string substringWithRange:NSMakeRange(1, 1)]intValue]==1) {
- [answer appendString:@"C"];
- }
- if ([[string substringWithRange:NSMakeRange(0, 1)]intValue]==1) {
- [answer appendString:@"D"];
- }
- return [answer copy];
- }
- @end
|