This repository has been archived by the owner on May 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
PlayFabConnection.m
67 lines (46 loc) · 2.06 KB
/
PlayFabConnection.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
#import "PlayFabConnection.h"
#import "PlayFabVersion.h"
static NSMutableArray *sharedConnectionList = nil;
@implementation PlayFabConnection
@synthesize request,completionBlock,internalConnection;
-(void)postURL:(NSString*)url body:(NSString*)body authType:(NSString*)authType authKey:(NSString*)authKey
{
NSURL *sRequestURL = [NSURL URLWithString:url];
NSMutableURLRequest *myRequest = [NSMutableURLRequest requestWithURL:sRequestURL];
NSString *sMessageLength = [NSString stringWithFormat:@"%d", (int)[body length]];
[myRequest addValue: @"application/json" forHTTPHeaderField:@"Content-Type"];
[myRequest addValue: sMessageLength forHTTPHeaderField:@"Content-Length"];
[myRequest addValue: versionString forHTTPHeaderField:@"X-PlayFabSDK"];
if ([authType length] != 0)
{
[myRequest addValue: authKey forHTTPHeaderField:authType];
}
[myRequest setHTTPMethod:@"POST"];
[myRequest setHTTPBody: [body dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:myRequest delegate:self];
if( theConnection ) {
container = [NSMutableData data];
if(!sharedConnectionList)
sharedConnectionList = [[NSMutableArray alloc] init];
[sharedConnectionList addObject:self];
}else {
NSLog(@"Some error occurred in Connection");
}
}
#pragma mark NSURLConnectionDelegate methods
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[container appendData:data];
}
//If finish, return the data and the error nil
-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
if([self completionBlock])
[self completionBlock](container,nil);
[sharedConnectionList removeObject:self];
}
//If fail, return nil and an error
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
if([self completionBlock])
[self completionBlock](nil,error);
[sharedConnectionList removeObject:self];
}
@end