Reputation: 5823
I need to detect if the device is connected to WIFI:
reach = [Reachability reachabilityForLocalWiFi];
status = [reach currentReachabilityStatus];
but the status is WWAN when I connect to WIFI on iPad, and there is no SIM inserted, I am about to test the status WITH a SIM.
Does anyone know the reason?
edit:
Just tried with a SIM card, it is still recognised as ReachableViaWWAN, but the actual connection is via 3G(the server only allows 3G connection, WIFI will fail), so I guess the Reachability may vary over time?
Upvotes: 0
Views: 1923
Reputation: 1125
Step 1:-
import SystemConfiguration
Step 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)
}
Step 3:-
if Global.isConnectedToNetwork() {
}
Upvotes: 0
Reputation: 1125
Reachability *reachability = [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"];
}
Upvotes: 1
Reputation: 5823
Finally I figured it out, the Reachability class I used is part of ASIHTTPRequest, which is a modified version, and it is modified in a way that a method is re-written but old method(with the same signature) is not removed, this does not make trouble(ie new method is called) until I created a static library for 3RD party libraries used in the project, which links the method declaration with the old method that does not fit into the modified framework of Reachability.
Upvotes: 2