iOS 플랫폼 네트워크 유형 검사
5889 단어 ios
#import <Foundation/Foundation.h>
/*
Framework
SystemConfiguration
CoreTelephony
*/
typedef enum _NetWorkType
{
kNetworkOff = 0,
kNetworkWifi,
kNetworkWLan,// , (iOS7 )
kNetworkWLan2G,
kNetworkWLan3G,
kNetworkGPRS,
kNetworkEdge,
kNetworkWCDMA,
kNetworkHSDPA,
kNetworkHSUPA,
kNetworkCDMA1x,
kNetworkCDMAEVDORev0,
kNetworkCDMAEVDORevA,
kNetworkCDMAEVDORevB,
kNetworkHRPD,
kNetworkLTE,
}NetworkType;
@interface HSNetworkDetect : NSObject
//
/*
0~2 NetworkType
*/
+(NetworkType)getNetworkType;
/*
iOS7 , , >2 ; , 2
*/
+(NetworkType)getCellularDataNetworkType;
/*
, Reachability
*/
+(BOOL)isReachable:(NSString*)address port:(NSInteger)port;
/*
WIFI SSID
*/
+(NSString*)getWifiSSID;
@end
#import "HSNetworkDetect.h"
#import "Reachability.h"
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <SystemConfiguration/CaptiveNetwork.h>
#define ISIOS7 !([[[UIDevice currentDevice] systemVersion] floatValue] <=6.9f)
#define SENTENCED_EMPTY(string) (string = ((string == nil) ? @"":string))
@implementation HSNetworkDetect
#pragma mark
+(NetworkType)getNetworkType
{
struct sockaddr_in zeroAddress;
bzero(&zeroAddress, sizeof(zeroAddress));
zeroAddress.sin_len = sizeof(zeroAddress);
zeroAddress.sin_family = AF_INET;
SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress); // :
SCNetworkReachabilityFlags flags;
SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
if ((flags & kSCNetworkReachabilityFlagsReachable) == 0)
{
return kNetworkOff;
}
NetworkType retVal = kNetworkOff;
if ((flags & kSCNetworkReachabilityFlagsConnectionRequired) == 0)
{
retVal = kNetworkWifi;
}
if ((((flags & kSCNetworkReachabilityFlagsConnectionOnDemand ) != 0) ||
(flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) != 0))
{
if ((flags & kSCNetworkReachabilityFlagsInterventionRequired) == 0)
{
retVal = kNetworkWifi;
}
}
if ((flags & kSCNetworkReachabilityFlagsIsWWAN) == kSCNetworkReachabilityFlagsIsWWAN)
{
if((flags & kSCNetworkReachabilityFlagsReachable) == kSCNetworkReachabilityFlagsReachable) {
if ((flags & kSCNetworkReachabilityFlagsTransientConnection) == kSCNetworkReachabilityFlagsTransientConnection)
{
retVal = kNetworkWLan3G;
if((flags & kSCNetworkReachabilityFlagsConnectionRequired) == kSCNetworkReachabilityFlagsConnectionRequired)
{
retVal = kNetworkWLan2G;
}
}
}
}
return retVal;
}
+(NetworkType)getCellularDataNetworkType
{
if (ISIOS7)
{
CTTelephonyNetworkInfo *telephonyInfo = [CTTelephonyNetworkInfo new];
NSString* wlanNetwork = telephonyInfo.currentRadioAccessTechnology;
if (wlanNetwork == nil)
return kNetworkOff;
if([wlanNetwork isEqualToString:CTRadioAccessTechnologyGPRS ])
{
return kNetworkGPRS;
}
else if([wlanNetwork isEqualToString:CTRadioAccessTechnologyEdge ])
{
return kNetworkEdge;
}
else if([wlanNetwork isEqualToString:CTRadioAccessTechnologyWCDMA ])
{
return kNetworkWCDMA;
}
else if([wlanNetwork isEqualToString:CTRadioAccessTechnologyHSDPA ])
{
return kNetworkHSDPA;
}
else if([wlanNetwork isEqualToString:CTRadioAccessTechnologyHSUPA ])
{
return kNetworkHSUPA;
}
else if([wlanNetwork isEqualToString:CTRadioAccessTechnologyCDMA1x ])
{
return kNetworkCDMA1x;
}
else if([wlanNetwork isEqualToString:CTRadioAccessTechnologyCDMAEVDORev0])
{
return kNetworkCDMAEVDORev0;
}
else if([wlanNetwork isEqualToString:CTRadioAccessTechnologyCDMAEVDORevA])
{
return kNetworkCDMAEVDORevA;
}
else if([wlanNetwork isEqualToString:CTRadioAccessTechnologyCDMAEVDORevB])
{
return kNetworkCDMAEVDORevB;
}
else if([wlanNetwork isEqualToString:CTRadioAccessTechnologyeHRPD ])
{
return kNetworkHRPD;
}
else if([wlanNetwork isEqualToString:CTRadioAccessTechnologyLTE ])
{
return kNetworkLTE;
}
}
return kNetworkWLan;
}
+(NSString*)getWifiSSID
{
NSArray *ifs = (__bridge id)CNCopySupportedInterfaces();
id info = nil;
for (NSString *ifnam in ifs)
{
info = (__bridge id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);
if (info && [info count])
{
NSString *ssid=[info objectForKey:(__bridge NSString *)kCNNetworkInfoKeySSID];
//NSString *bssid=[info objectForKey:(__bridge NSString *)kCNNetworkInfoKeyBSSID];
//NSLog(@"interfaceName:%@ ssid:%@ bssid:%@",ifnam,ssid,bssid);
return ssid;
}
}
return nil;
}
+(BOOL)isReachable:(NSString*)address port:(NSInteger)port
{
Reachability* poReach = [Reachability reachabilityWithHostname:[NSString stringWithFormat:@"%@:%d",address,port]];
BOOL bRet = [poReach isReachable];
return bRet;
}
@end
PS---> , .
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Swift의 패스트 패스Objective-C를 대체하기 위해 만들어졌지만 Xcode는 Objective-C 런타임 라이브러리를 사용하기 때문에 Swift와 함께 C, C++ 및 Objective-C를 컴파일할 수 있습니다. Xcode는 S...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.