-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOffLineMode.m
More file actions
183 lines (134 loc) · 4.47 KB
/
OffLineMode.m
File metadata and controls
183 lines (134 loc) · 4.47 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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//
// OffLineMode.m
// WaveNews
//
// Created by GUYU XUE on 29/12/11.
// Copyright (c) 2011 Nanyang Technological University. All rights reserved.
//
#import "OffLineMode.h"
#import "Reachability.h"
@implementation OffLineMode
{
BOOL alerted;
}
@synthesize internetReachable;
@synthesize hostReachable;
-(void) dealloc
{
NSLog(@"end");
[internetReachable release];
[hostReachable release];
[super dealloc];
}
+(BOOL) isConnected
{
Reachability *tempTest=[[Reachability reachabilityForInternetConnection] retain];
[tempTest startNotifier];
NetworkStatus internetStatus = [tempTest currentReachabilityStatus];
switch (internetStatus)
{
case NotReachable:
{
return FALSE;
break;
}
case ReachableViaWiFi:
{
NSLog(@"Connected!");
return TRUE;
break;
}
case ReachableViaWWAN:
{
return TRUE;
break;
}
default:
{
return FALSE;
}
}
[tempTest release];
}
#pragma mark - notify network change
-(void) startTestNetwork
{
NSLog(@"++++++++++start");
alerted = NO;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:)name:kReachabilityChangedNotification object:nil];
internetReachable = [[Reachability reachabilityForInternetConnection] retain];
[internetReachable startNotifier];
// check if a pathway to a random host exists
hostReachable = [[Reachability reachabilityWithHostName:@"www.google.com"] retain];
[hostReachable startNotifier];
}
- (void) checkNetworkStatus:(NSNotification *)notice
{
// called after network status changes
NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
switch (internetStatus)
{
case NotReachable:
{
if(alerted==NO)
{
NSLog(@"The internet is down.");
NSString *alertTitle = @"Please check if you have internet access.";
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Network disconnected..."
message:alertTitle delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[message show];
alerted = YES;
}
break;
}
case ReachableViaWiFi:
{
NSLog(@"The internet is working via WIFI.");
break;
}
case ReachableViaWWAN:
{
NSLog(@"The internet is working via WWAN.");
break;
}
}
NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
switch (hostStatus)
{
case NotReachable:
{
NSLog(@"A gateway to the host server is down.");
break;
}
case ReachableViaWiFi:
{
NSLog(@"A gateway to the host server is working via WIFI.");
break;
}
case ReachableViaWWAN:
{
NSLog(@"A gateway to the host server is working via WWAN.");
break;
}
}
}
#pragma mark - get saved news
+(NSArray *) getStoredNews:(NSString *) newspaper withSection:(NSString *) section
{
NSArray *myPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *myDocPath = [myPaths objectAtIndex:0];
newspaper=[newspaper stringByReplacingOccurrencesOfString:@" " withString:@"_"];
section=[section stringByReplacingOccurrencesOfString:@" " withString:@"_"];
NSString *fileName=[NSString stringWithFormat:@"%@_%@.plist",newspaper,section];
NSString *filePath = [myDocPath stringByAppendingPathComponent:fileName];
BOOL fileExists=[[NSFileManager defaultManager] fileExistsAtPath:filePath];
if(!fileExists)
{
return nil;
}
NSArray *resultToReturn=[NSArray arrayWithContentsOfFile:filePath];
return resultToReturn;
}
@end