Skip to content

Instantly share code, notes, and snippets.

@rossmartin
Last active December 16, 2015 14:28
Show Gist options
  • Save rossmartin/5448596 to your computer and use it in GitHub Desktop.
Save rossmartin/5448596 to your computer and use it in GitHub Desktop.
Return file contents to JavaScript from Objective-C
- (void) restClient:(DBRestClient*)client loadedFile:(NSString*)localPath {
NSLog(@"File loaded into path: %@", localPath);
NSData *fileData;
fileData = [NSData dataWithContentsOfFile:localPath];
if (!fileData){
NSLog(@"Error - File not found");
}
NSString* fileString = [[NSString alloc] initWithData:fileData encoding:NSUTF8StringEncoding];
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSString *jsCallback = [[NSString alloc] initWithFormat:@"showFileContents('%@')", fileString];
[appDelegate.viewController.webView stringByEvaluatingJavaScriptFromString:jsCallback]; // this calls showFileContents function in JS and sends it the fileString as a parameter
[self writeJavascript:restoreJavaScript]; // return code to execute JavaScript function after file is downloaded
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment