我需要检测设备是否连接到WIFI:
reach = [Reachability reachabilityForLocalWiFi];
status = [reach currentReachabilityStatus];
但当我在iPad上连接到WIFI时状态为WWAN,并且没有插入SIM卡,我即将用SIM测试状态。
有谁知道原因?
编辑:
刚刚尝试使用SIM卡,它仍被识别为ReachableViaWWAN,但实际连接是通过3G(服务器只允许3G连接,WIFI会失败),所以我猜可达性可能随时间而变化?
答案 0 :(得分:2)
最后我想通了,我使用的Reachability类是ASIHTTPRequest的一部分,ASIHTTPRequest是一个修改版本,它的修改方式是重写方法,但旧方法(具有相同的签名)不是删除,这不会造成麻烦(即调用新方法),直到我为项目中使用的3RD方库创建静态库,该方法声明将方法声明与不适合可修改的可达性框架的旧方法相关联。 / p>
答案 1 :(得分:1)
可达性*可达性= [Reachability reachabilityForInternetConnection]; BOOL remoteHostStatus = [reachability currentReachabilityStatus];
if (remoteHostStatus == ReachableViaWiFi || remoteHostStatus == ReachableViaWWAN)
{
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:textFieldUserName.text,@"email",textFieldpass.text,@"password", nil];
objApp.responseData = [[NSMutableData data]init];
objApp.request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@login",mainUrl]]];
NSData * requestBodyData = [NSJSONSerialization dataWithJSONObject:dict options:0 error:NULL];
objApp.request.HTTPMethod= @"POST";
[objApp.request setHTTPBody:requestBodyData];
[objApp.request setTimeoutInterval:8.0f];
// Create url connection and fire request
objApp.connection = [[NSURLConnection alloc] initWithRequest:objApp.request delegate:self];
}
else
{
[objApp alertViewFromApp:@"check internet connection of device"];
}
答案 2 :(得分:0)
第1步: -
导入SystemConfiguration
第2步: -
//MARK:- Internet available or Not
public static func isConnectedToNetwork() -> Bool {
var zeroAddress = sockaddr_in()
zeroAddress.sin_len = UInt8(sizeofValue(zeroAddress))
zeroAddress.sin_family = sa_family_t(AF_INET)
let defaultRouteReachability = withUnsafePointer(&zeroAddress) {
SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer($0))
}
var flags = SCNetworkReachabilityFlags()
if !SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) {
return false
}
let isReachable = (flags.rawValue & UInt32(kSCNetworkFlagsReachable)) != 0
let needsConnection = (flags.rawValue & UInt32(kSCNetworkFlagsConnectionRequired)) != 0
return (isReachable && !needsConnection)
}
第3步: -
if Global.isConnectedToNetwork() {
}