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];
Related
I am fairly new to iOS development, and am unable to store an SVG image in the documents directory of my app. I have access to the raw data and would like to store this in SVG format
The raw data is in NSData format.
NSData *rawData = [[NSData alloc] initWithBytes:ptr length:size];
rawData has the dowloaded file, please help me save this as an SVG. Any help appreciated, also I have been doing this for less than a week, sorry for any mistakes.
yeah, do this:
- (NSString *)writeDataToDocuments:(NSData *)data withFilename:(NSString *)filename {
NSString *docsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *filePath = [[NSString alloc] initWithString: [docsPath stringByAppendingPathComponent:filename]];
[data writeToFile:filePath atomically:YES];
return filePath;
}
the file path returned is where the SVG is now stored
so in your case it's
NSData *rawData = [[NSData alloc] initWithBytes:ptr length:size];
NSString *fileLocationString = [self writeDataToDocuments:rawData withFilename:#"example.svg"];
and now it's saved and you have the file path
I am using the below code to download a ebook from url and store into my device. While this works in emulator, in my ipad I am getting error saying file is not available. I am not able to read the file in the respective path of the device. please help..
NSString *urls = [NSString stringWithFormat: #"http://hungerin.com/?download_file=%#&order=%#&email=%#&key=%#", BookID, orderKey, email, downloadId];
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: urls]];
NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:#""]];
NSString *filePathAppend = [NSString stringWithFormat: #"/testPubb.app/Documents/%#.epub",BookID];
NSString *filePath = [resourceDocPath stringByAppendingPathComponent:filePathAppend];
[pdfData writeToFile:filePath atomically:YES];
The way you are getting the app document directory path is not correct. Try this:
NSString *urls = [NSString stringWithFormat: #"http://hungerin.com/?download_file=%#&order=%#&email=%#&key=%#", BookID, orderKey, email, downloadId];
NSData *pdfData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urls]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.epub",BookID];
[pdfData writeToFile:filePath atomically:YES];
Let me know if you have questions.
I am trying to retrieve PDF file using NSSearchPathForDirectoriesInDomains.i am getting path for PDF file but how to convert that pdf file into nsdata or nsmutabledata(bytes).Here is my code to get path of PDF.
NSArray *arrayPaths1 =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *path1 = [arrayPaths1 objectAtIndex:0];
path1 = [path1 stringByAppendingPathComponent:#"mypdffile.pdf"];
in path1 i am getting the path and to get byte data I am trying
[decodedData writeToFile:path1 atomically:YES];
decodedData is NSMutableData.decodedData is nil.Please help me.How can I solve this problem?
Try below code..
NSData *dataPdf = [NSData dataWithContentsOfURL:pdfOnline.url];
//Get path directory
NSArray *arrayPaths1 =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
//Create PDF_Documents directory
NSString * documentsDirectory = [arrayPaths1 objectAtIndex:0];
documentsDirectory = [documentsDirectory stringByAppendingPathComponent:#"mypdffile.pdf"];
[[NSFileManager defaultManager] documentsDirectory withIntermediateDirectories:YES attributes:nil error:nil];
NSString *filePath = [NSString stringWithFormat:#"%#/%#", documentsDirectory, #"PDFRefYouPreferred"];
[dataPdf writeToFile:filePath atomically:YES];
Hope it will fix the issue..!
You can use [NSData dataWithContentsOfFile:<#path of your file#>] for that purpose.
In your case it should be:
decodedData = [NSData dataWithContentsOfFile: path1];
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.
So I am using this library: https://github.com/flyingdolphinstudio/Objective-Zip
I implemented it and am trying to take a UIImage and NSString and make it a .png and .txt in the .zip file, respectively.
Now these are my 2 concerns, I am trying to save the *zipFile below to the documents directory.
Now with the dropbox API, how come I can't just provide the file itself and skip the path. It seems like I HAVE to save the .zip to the documents directory first and then get the path so I can then upload it to dropbox. Do I have to do that?
In the ...writeToFile line, I am getting a warning that ZipFile may not respond to writeToFile so how would I properly save it to the documents directory?
Anyway this is the code I have so far:
NSString *filename = [NSString stringWithFormat:#"%#.zip", textField.text];
ZipFile *zipFile= [[ZipFile alloc] initWithFileName:filename mode:ZipFileModeCreate];
//Image
NSString *nameImage = #"Image.png";
NSMutableDictionary *theDictionary = [Singleton sharedSingleton].dictionary;
NSData *data = [theDictionary objectForKey:#"image"];
ZipWriteStream *writeImage = [zipFile writeFileInZipWithName:nameImage compressionLevel:ZipCompressionLevelBest];
[writeImage writeData:data];
[writeImage finishedWriting];
//Text
NSString *nameText = #"Text.txt";
NSData *dataText = [textView.text dataUsingEncoding:NSUTF8StringEncoding];
ZipWriteStream *writeText = [zipFile writeFileInZipWithName:nameText compressionLevel:ZipCompressionLevelBest];
[writeText writeData:dataText];
[writeText finishedWriting];
//Now we HAVE to save it to the documents directory to get it to work with dropbox
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
NSString *filePath = [documentsPath stringByAppendingPathComponent:filename]; //Add the file name
[zipFile writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:NULL];
//Save to Dropbox
NSString *zipPath = [[NSBundle mainBundle] pathForResource:textField.text ofType:#"zip"];
[[self restClient] uploadFile:filename toPath:#"/" withParentRev:nil fromPath:zipPath];
So what am I doing wrong here?
Thanks!
It looks to me like ZipFile already writes to a file, so there's no need for something like writeToFile. Just initialize zipFile with the path you want, be sure to close the file at the end ([zipFile close]), and then upload to Dropbox as you would any other file.