I want to check if a web host is available so i used the code down below, it works, but sometimes if the site is slow in response it keep waiting, how do i implant a timeout into it? if no response from the host withen 2-3 second, return false also. Please help me out
- (BOOL)isDataSourceAvailable {
static BOOL checkNetwork = YES;
static BOOL _isDataSourceAvailable = NO;
if (checkNetwork) { // Since checking the reachability of a host can be expensive, cache the result and perform the reachability check once.
checkNetwork = NO;
Boolean success;
const char *host_name = "google.com"; //HOST
SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL, host_name);
SCNetworkReachabilityFlags flags;
success = SCNetworkReachabilityGetFlags(reachability, &flags);
_isDataSourceAvailable = success && (flags & kSCNetworkFlagsReachable) && !(flags & kSCNetworkFlagsConnectionRequired);
CFRelease(reachability);
}
return _isDataSourceAvailable;
}
Its quite simple
Just implement
NSTimerExample
if targetMethod: is called, then its timeout.
Here don’t forget to call
if you receive response from server.
Hope this helps you.