- (void)viewDidLoad { [super viewDidLoad]; NSString *results = [NSString stringWithFormat:@"네트워크 사용 %@가능 %@%@", ([UIDevice networkAvailable] ? @"" : @"불"), ([UIDevice activeWLAN] ? @"[WiFi]" : @""), ([UIDevice activeWWAN] ? @"[Cell]" : @"")]; self.lbl_txt.text = results; } + (NSString *) localWiFiIPAddress { BOOL success; struct ifaddrs * addrs; const struct ifaddrs * cursor; success = getifaddrs(&addrs) == 0; if (success) { cursor = addrs; while (cursor != NULL) { // the second test keeps from picking up the loopback address if (cursor->ifa_addr->sa_family == AF_INET && (cursor->ifa_flags & IFF_LOOPBACK) == 0) { NSString *name = [NSString stringWithUTF8String:cursor->ifa_name]; if ([name isEqualToString:@"en0"]) // Wi-Fi adapter return [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)cursor->ifa_addr)->sin_addr)]; } cursor = cursor->ifa_next; } freeifaddrs(addrs); } return nil; } #pragma mark Checking Connections + (void) pingReachabilityInternal { BOOL ignoresAdHocWiFi = NO; struct sockaddr_in ipAddress; bzero(&ipAddress, sizeof(ipAddress)); ipAddress.sin_len = sizeof(ipAddress); ipAddress.sin_family = AF_INET; ipAddress.sin_addr.s_addr = htonl(ignoresAdHocWiFi ? INADDR_ANY : IN_LINKLOCALNETNUM); // Recover reachability flags SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (struct sockaddr *)&ipAddress); /* SCNetworkReachabilityGetFlags : if the given target is reachable using the current network configuration. */ BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &connectionFlags); CFRelease(defaultRouteReachability); if (!didRetrieveFlags) printf("Error. Could not recover network reachability flags\n"); } + (BOOL) networkAvailable { [self pingReachabilityInternal]; BOOL isReachable = ((connectionFlags & kSCNetworkFlagsReachable) != 0); BOOL needsConnection = ((connectionFlags & kSCNetworkFlagsConnectionRequired) != 0); return (isReachable && !needsConnection) ? YES : NO; } + (BOOL) activeWWAN { if (![self networkAvailable]) return NO; return ((connectionFlags & kSCNetworkReachabilityFlagsIsWWAN) != 0); } + (BOOL) activeWLAN { return ([UIDevice localWiFiIPAddress] != nil); }
'Developer > iOS' 카테고리의 다른 글
[iOS] 복사하기 (0) | 2013.05.03 |
---|---|
[iOS]Text의 길이에 따라 UILabel 크기 변경하기 (0) | 2013.02.18 |
[iOS] 네트워크 연결 테스트 (0) | 2013.02.13 |
[iOS] UITableView refresh, 당겨서 새로고침 하기 (0) | 2011.12.19 |
[iOS] 쿠키 삭제하기 (0) | 2011.12.19 |