I need to download pdf file which I get in api response as a base64string.
How should I save it locally based on cell tag and preview it using documentcontroller?
Eg-api response is an array of dictionaries with different pdf files where we have a key called "pdf":"zcvffaewww.."
I need to download these pdf files on tap of uibutton on uitableviewcell and preview it in documentcontroller.
Please help.
Thanks.
you are getting a base64 string in JSON, it means you are getting the file name and file type also right (if not getting then it is fine)
Step 1: Instead of download, you can view that PDF in webView. Here is the way to do this,
// convert your base64 string into NSData
NSData* myData = [NSData dataFromBase64String: yourBase64String];
then write below code to show pdf in webView,
[_webView loadData:myData
MIMEType:#"application/pdf"
textEncodingName:#"NSUTF8StringEncoding"
baseURL:[NSURL URLWithString:#"https://www.google.co.in/"]];
// zooming pdf view
_webView.scalesPageToFit = YES;
_webView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
It will show pdf in webView
step 2: for downloading pdf, write code
NSURL *URL = [NSURL URLWithString:
[NSString stringWithFormat:#"data:application/pdf;base64,%#",
numStr]];
NSData *urlData = [NSData dataWithContentsOfURL:url];
if ( urlData )
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/%#", documentsDirectory,#"filename.pdf"];
[urlData writeToFile:filePath atomically:YES];
}
Related
Hi in my application i have option of downloading a pdf file i have searched for that i got the code i have used its working fine in my mac i can see the downloaded pdf file. But the problem is if i download the same pdf in device where i can find the downloaded pdf file in device.
This is my code which i have used to download the pdf file.
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:#"http://jesusredeems.com/mag/pdf/JRE-2014-03.pdf"]]; //Store the Data locally as PDF File
NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:#"Documents"]];
NSString *filePath = [resourceDocPath stringByAppendingPathComponent:#"11011.pdf"]; [pdfData writeToFile:filePath atomically:YES]; NSLog(#"%#",filePath);
If i print the filepath I'm getting like this.
/var/mobile/Applications/353C27AD-1E64-41CA-A429-16C2A20C8606/Documents/11011.pdf
In my mac i can see the downloaded file in that path but in my Ipad where i can see the downloaded pdf file please anyone tell where i have to find it.
Thanks.
Most likely your saving part is correct but try this anyways.
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:#"http://jesusredeems.com/mag/pdf/JRE-2014-03.pdf"]];
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask ,YES);
NSString* documentsPath = [paths objectAtIndex:0];
NSString *destPath = [documentsPath stringByAppendingPathComponent:#"11011.pdf"];
[pdfData writeToFile:destPath atomically:YES];
And do this to retrieve it.
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask ,YES);
NSString* documentsPath = [paths objectAtIndex:0];
NSString* dataFile = [documentsPath stringByAppendingPathComponent:#"11011.pdf"];
NSData *pdfData3 = [NSData dataWithContentsOfFile:dataFile];
I am trying to save some NSData in pdf format. The NSData is from a downloaded pdf. This is how I tried to do it:
NSData* data = [DKStoreManager loadFileWithName:_FileName forFolderNumber:[_FolderNumber intValue] forUser:userID andType:_FolderType];
NSDictionary *myDictionary = (NSDictionary*) [NSKeyedUnarchiver unarchiveObjectWithData:data];
NSData* dataFile;
NSMutableDictionary* dict = [myDictionary objectForKey:#"MultiFileOtherFile2"];
dataFile = [dict objectForKey:#"FileData"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:#"mLocalFileName.pdf"];
[dataFile writeToFile:pdfPath atomically:YES];
NSString *phrase = nil; // Document password (for unlocking most encrypted PDF files)
ReaderDocument *document = [ReaderDocument withDocumentFilePath:pdfPath password:phrase];
This doesn't work. The only thing that works is:
NSArray *pdfs = [[NSBundle mainBundle] pathsForResourcesOfType:#"pdf" inDirectory:nil];
NSString *filePath = [pdfs lastObject]; assert(filePath != nil); // Path to last PDF file
ReaderDocument *document = [ReaderDocument withDocumentFilePath:filePath password:phrase];
...with a pdf added manually to Xcode!! I even tried all of the conversion solutions at:
How to convert a NSData to pdf in iPhone sdk?
...and none work!
Thanks in advance for any help.
Your issue is not in these lines of code. Your issue is that either:
the downloaded file is corrupt (so your information in the NSData is corrupt)
ReaderDocument, the class you use to open the file, has an bug and downloads the file incorrectly
Maybe the PDF file uses some PDF features which are not supported by your ReaderDocument class? PDF is a very broad format.
To serialize the pdf I believe you could use NSData dataWithURL: and store in Core Data. I'm unsure as to how you'd deserialize back to a pdf however and view it with UIWebView.
You can use CGPDFDocumentCreateWithProvider:
If your NSData reference is pdfData then:
CFDataRef myPDFData = (CFDataRef)pdfData;
CGDataProviderRef provider = CGDataProviderCreateWithCFData(myPDFData);
CGPDFDocumentRef pdf = CGPDFDocumentCreateWithProvider(provider);
But I guess you just want to display it in a web view and the above is not what you need.
However if you already have the url wouldn't it be easier to store the URL rather than the pdf as such.
Also if you have the data you can write it into a file inside your documents directory temporarily and then use web view to load this url.
i.e;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:#"New1.pdf"];
NSURL *fileURL = [NSURLRequest requestWithURL:appFile];
[data writeToURL:fileURL atomically:YES];
and then :
NSURLRequest *request = [NSURLRequest requestWithURL:fileURL];
[webView loadRequest:request];
And if you want to remove the file from the documents directory you could use:
[[NSFileManager defaultManager] removeItemAtURL:fileUrl error:nil];
to remove the temporary pdf you created.
Imagine that you are reading json data from the server continuously. let's say, you are getting weather data. I want to show only data 2 hours back from now. Whenever user clicks on the play button, it would show weather data on the map.
How I could save json data first then show it on the map. Because that would enhance my application instead of connecting server , getting data and showing it at the same time. please just give me advice. Do I need to save it first as a plist or an array? Where should I keep this data before I simulate?
Storing it in the plist in the documents directory is a good way of saving data for later reference.If you want to save the JSON string you can also opt for NSUSerDefaults..(preferably if the string is not too large)
Saving to plist..
NSURL *url = [NSURL URLWithString:serverPath];
NSData *urlData = [NSData dataWithContentsOfURL:url];
if ( urlData )
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/myplist.plist", documentsDirectory];
[urlData writeToFile:filePath atomically:YES];
}
and retrieving it..
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithFormat: #"%#/myplist.plist",documentsDirectory ] ];
NSData *retrievedData = [NSData dataWithContentsOfFile:filePath];
I am receiving a JSON response and am currenlty able to make use of the data within my app.
I would like to save this response to a file so I could reference within an JS file located inside my project. I have already requested this data once when the application is launched so why not save it to a file and reference that throughout so only one call is needed for the data.
The HTML files for my UIWebView are imported into my Xcode project using the "create folder reference" option and the path to my JS file is html->js->app.js
I want to save the response as data.json somewhere on the device and then reference inside my js file like this request.open('GET', 'file-path-to-saved-json.data-file', false);
How can I achieve this?
After working with the idea some more here is what I came up with.
When the application is installed there is a default data file in the package that I copy to the Documents folder. When the application didFinishLaunchingWithOptions runs I call the following method:
- (void)writeJsonToFile
{
//applications Documents dirctory path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//live json data url
NSString *stringURL = #"http://path-to-live-file.json";
NSURL *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
//attempt to download live data
if (urlData)
{
NSString *filePath = [NSString stringWithFormat:#"%#/%#", documentsDirectory,#"data.json"];
[urlData writeToFile:filePath atomically:YES];
}
//copy data from initial package into the applications Documents folder
else
{
//file to write to
NSString *filePath = [NSString stringWithFormat:#"%#/%#", documentsDirectory,#"data.json"];
//file to copy from
NSString *json = [ [NSBundle mainBundle] pathForResource:#"data" ofType:#"json" inDirectory:#"html/data" ];
NSData *jsonData = [NSData dataWithContentsOfFile:json options:kNilOptions error:nil];
//write file to device
[jsonData writeToFile:filePath atomically:YES];
}
}
Then throughout the application when I need to reference the data, I use the saved file.
//application Documents dirctory path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSError *jsonError = nil;
NSString *jsonFilePath = [NSString stringWithFormat:#"%#/%#", documentsDirectory,#"data.json"];
NSData *jsonData = [NSData dataWithContentsOfFile:jsonFilePath options:kNilOptions error:&jsonError ];
To reference the json file in my JS code, I added a URL parameter for "src" and passed the file path to the Applications Documents folder.
request.open('GET', src, false);