Skip to content

Instantly share code, notes, and snippets.

@rlustemberg
Created March 2, 2017 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rlustemberg/8dbf4e36a95fcb92dd0a500f508c2fa6 to your computer and use it in GitHub Desktop.
Save rlustemberg/8dbf4e36a95fcb92dd0a500f508c2fa6 to your computer and use it in GitHub Desktop.
// at the top of the file
@interface TiApp()
- (void)checkBackgroundServices;
- (void)appBoot;
@property (atomic) NSMutableDictionary *responsesData; //here we are holding the responsesData, using their taskIdentifiers as keys
@end
// Here I initialise the property
- (void)appBoot
{
kjsBridge = [[KrollBridge alloc] initWithHost:self];
[kjsBridge boot:self url:nil preload:nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
self.responsesData = [[NSMutableDictionary alloc] init]; //initialising the dictionary holding the responses
}
//delegate method from NSURLSessionDataDelegate , not implemented yet in TiApp.m
-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
{
//Not very nice but I defined and initialised self.responsesData which is an NSMutableDictionary holding responses data
// defined as @property (atomic) NSMutableDictionary *responsesData
//initialised in appBoot
NSMutableData *responseData = self.responsesData[@(dataTask.taskIdentifier)];
if (!responseData) {
responseData = [NSMutableData dataWithData:data];
self.responsesData[@(dataTask.taskIdentifier)] = responseData;
} else {
[responseData appendData:data];
}
}
-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
//FunctionName();
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithUnsignedInteger:task.taskIdentifier], @"taskIdentifier",
nil];
if (session.configuration.identifier) {
[dict setObject:session.configuration.identifier forKey:@"sessionIdentifier"];
}
if (error) {
NSDictionary * errorinfo = [NSDictionary dictionaryWithObjectsAndKeys:NUMBOOL(NO), @"success",
NUMINTEGER([error code]), @"errorCode",
[error localizedDescription], @"message",
nil];
[dict addEntriesFromDictionary:errorinfo];
} else {
NSMutableData *responseData = self.responsesData[@(task.taskIdentifier)];
NSString *responseText = nil;
if (responseData) {
//we only send responseText as this is the responsesData dictionary only gets filled with data from uploads
responseText = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
[self.responsesData removeObjectForKey:@(task.taskIdentifier)];
}
NSDictionary * success = [NSMutableDictionary dictionaryWithObjectsAndKeys:NUMBOOL(YES), @"success",
NUMINT(0), @"errorCode",
@"", @"message",
responseText,@"responseText",
nil];
[dict addEntriesFromDictionary:success];
}
[[NSNotificationCenter defaultCenter] postNotificationName:kTiURLSessionCompleted object:self userInfo:dict];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment