-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServerInteract.m
executable file
·52 lines (44 loc) · 1.67 KB
/
ServerInteract.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
//
// ServerInteract.m
// FloridaTripTracker
//
// Created by Pinchao Liu on 3/13/17.
//
//
#import "ServerInteract.h"
@implementation ServerInteract
+ (NSData*) sendRequest:(id)inPostVars toURLAddress:(NSString*) URLString{
if (![self connected]) {
NSLog(@"%@",@"no internet connection");
return nil;
}
else {
NSData *postData;
if ([inPostVars isKindOfClass:[NSDictionary class]] || [inPostVars isKindOfClass:[NSArray class]] ){
postData= [NSJSONSerialization dataWithJSONObject:inPostVars options:nil error:nil];
}
else if([inPostVars isKindOfClass:[NSString class]]){
postData = [inPostVars dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
}
NSString *postLength = [NSString stringWithFormat:@"%ld", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:URLString]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLResponse *response;
NSError *err;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSString *readableJsonText = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];
NSLog(@"Received Readable Json%@ from URLString: %@", readableJsonText, URLString);
return responseData;
}
}
+ (BOOL)connected
{
Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus networkStatus = [reachability currentReachabilityStatus];
return networkStatus != NotReachable;
}
@end