This repository has been archived by the owner on May 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConnectionChecking.m
101 lines (82 loc) · 2.87 KB
/
ConnectionChecking.m
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//
// ConnectionChecking.m
//
// Created by Németh Balázs on 6/26/12.
//
#import "ConnectionChecking.h"
#import "Reachability.h"
@interface ConnectionChecking()
@property(strong,nonatomic) NSTimer* connectionCheckTimer;
-(void) checkConnection;
@end
@implementation ConnectionChecking
@synthesize connectionCheckTimer;
static ConnectionChecking *sharedConnectionChecking = nil;
+ (ConnectionChecking *)sharedConnectionChecking
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedConnectionChecking = [[ConnectionChecking alloc] init];
// Do any other initialisation stuff here
});
return sharedConnectionChecking;
}
+ (id)allocWithZone:(NSZone *)zone {
@synchronized(self) {
if (sharedConnectionChecking == nil) {
sharedConnectionChecking = [super allocWithZone:zone];
return sharedConnectionChecking; // assignment and return on first allocation
}
}
return nil; // on subsequent allocation attempts return nil
}
+ (id)alloc {
@synchronized(self) {
if (sharedConnectionChecking == nil) {
sharedConnectionChecking = [super alloc];
return sharedConnectionChecking; // assignment and return on first allocation
}
}
return sharedConnectionChecking; // on subsequent allocation attempts return nil
}
//The real class function!
- (void) startCheckingNetwork{
[self stopCheckingNetwork];
connectionCheckTimer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(checkConnection) userInfo:nil repeats:true];
[[NSRunLoop currentRunLoop] addTimer:connectionCheckTimer forMode:NSDefaultRunLoopMode];
}
- (void) stopCheckingNetwork{
if(connectionCheckTimer != nil){
[connectionCheckTimer invalidate];
connectionCheckTimer = nil;
}
}
-(void) checkConnection{
Reachability* reachability = [Reachability reachabilityForInternetConnection];
//[reachability setHostName:serverURL]; // set your host name here
NetworkStatus internetStatus = [reachability currentReachabilityStatus];
BOOL internetActive = FALSE;
switch (internetStatus)
{
case NotReachable:
{
NSLog(@"The internet is down.");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Az adatbázis nem elérhető" message: @"Kérjük ellenőrizze a hálózatot" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alert show];
break;
}
case ReachableViaWiFi:
{
NSLog(@"The internet is working via WIFI.");
internetActive = YES;
break;
}
case ReachableViaWWAN:
{
NSLog(@"The internet is working via WWAN.");
internetActive = YES;
break;
}
}
}
@end