-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJWReachability.m
More file actions
57 lines (44 loc) · 1.68 KB
/
JWReachability.m
File metadata and controls
57 lines (44 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//
// JWReachability.m
//
// Created by Jack Wu on 2014-03-12.
//
#import "JWReachability.h"
@import SystemConfiguration;
@implementation JWReachability
+(BOOL)isGoogleAvailableWithTimeout:(CGFloat) seconds {
return [JWReachability isServerAvailable:@"www.google.com" withTimeOut:seconds];
}
+(BOOL)isFacebookAvailableWithTimeout:(CGFloat)seconds {
return [JWReachability isServerAvailable:@"www.facebook.com" withTimeOut:seconds];
}
+(BOOL)isServerAvailable:(NSString*)url withTimeOut:(CGFloat)seconds {
const char *host_name = [url UTF8String]; // your data source host name
printf("Checking server:%s...",host_name);
NSDate * date = [NSDate date];
dispatch_queue_t queue = dispatch_queue_create("reachabilityqueue", DISPATCH_QUEUE_SERIAL);
__block BOOL _isDataSourceAvailable;
__block BOOL returned = NO;
__block Boolean success;
__block SCNetworkReachabilityFlags flags;
dispatch_async(queue, ^{
SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL, host_name);
success = SCNetworkReachabilityGetFlags(reachability, &flags);
_isDataSourceAvailable = success && (flags & kSCNetworkFlagsReachable) && !(flags & kSCNetworkFlagsConnectionRequired);
CFRelease(reachability);
returned = YES;
});
// dispatch_release(queue);
while (!returned && (-[date timeIntervalSinceNow]) < seconds) {
printf(".");
usleep(50000);
}
printf("\n");
if (!returned) {
NSLog(@"Timed out after %.3fs",-[date timeIntervalSinceNow]);
return NO;
}
NSLog(@"Completed in %.3fs",-[date timeIntervalSinceNow]);
return _isDataSourceAvailable;
}
@end