In one of my activity, I displayed a PDF using WebView on screen and tried to save this PDF using this code:
_pageSize = CGSizeMake(width, height);
NSString *newPDFName = [NSString stringWithFormat:#"%#", name];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:newPDFName];
UIGraphicsBeginPDFContextToFile(pdfPath, CGRectZero, nil);
NSLog(#"path=%#",pdfPath);
When I run this code in iOS simulator (using Xcode) and show the path and I opened this PDF file successfully in documents folder, but when I run this code in an iPhone, I got this path:
/var/mobile/Applications/0AF98361-C8DF-4C35-9E9F-EE48555185BC/Library/354746396.pdf
So where are PDF files stored in iPhone?
"..InDomains(NSLibraryDirectory, NSUserDomainMask, YES);"
"../Library/354746396.pdf"
Your paths is looking in the NSLibraryDirectory, it should be looking in the NSDocumentDirectory like this:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
If still fail, try this code:
NSString *documents = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents"];
NSString *savePath = [documents stringByAppendingPathComponent:#"filename.pdf"];
NSLog(#"savePath: %#", savePath);
- (void)viewDidLoad
{
[super viewDidLoad];
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 367)];
webView.scalesPageToFit = YES;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"myPDF11.pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:filePath];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];
}
my coding is right but i will not find the pdf file.i will solve my problem.i will find my pdf file in device.
there are two ways to find the pdf file.
1)install the iexplorer filemanager software.than open document folder.
2)connect your device in to the mac.select window menu and click the device in xcode.than after select your device name.select your app.
select show container.than after you can see the document folder....
Related
I have an ipad app in which i show PDF documents in a UIWebView from my server.I want users to be able to download these documents so that they can read them offline. However i want them to read it only in the app.Basically my question is: Is it possible to have a folder that is only accessible from the app but not by users.
you can save it in the app bundle.
NSDate *date = [NSDate new];
NSString *docName = [NSString stringWithFormat:#"%lld.pdf", (long long)[date timeIntervalSince1970]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
dataPath = [documentsDirectory stringByAppendingPathComponent:docName];
EDIT: acording your comments question i think the solution would be something like this:
NSString *str = #"http://myserver.com/myfile.ext";
NSURL *url = [NSURL URLWithString:str];
NSData *content = [NSData dataWithContentsOfURL:url];
if (content) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [NSString stringWithFormat:#"%#/%#", documentsDirectory,#"myfile.ext"];
[content writeToFile:dataPath atomically:YES];
}
Steps:
Create password to PDF file and Open PDF programatically with password.
You can Make Own DRM for PDF files.
Refer Following Link:
iPhone Documents Folder Library/Caches Security issue
hi i am created pdf file using Quartz framework in IOS.i need to download that pdf file in local folder like saving images in simulator.any one tell me how to do this.
my code for creating pdf
-(NSString*)getPDFFileName
{
NSString* fileName = #"Invoice.PDF";
NSArray *arrayPaths =
NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *path = [arrayPaths objectAtIndex:0];
NSString* pdfFileName = [path stringByAppendingPathComponent:fileName];
return pdfFileName;
}
-(void)showPDFFile
{
NSString* fileName = #"Invoice.PDF";
NSArray *arrayPaths =
NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *path = [arrayPaths objectAtIndex:0];
NSString* pdfFileName = [path stringByAppendingPathComponent:fileName];
UIWebView* webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
NSURL *url = [NSURL fileURLWithPath:pdfFileName];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView setScalesPageToFit:YES];
[webView loadRequest:request];
[self.view addSubview:webView];
}
- (void)viewDidLoad
{
NSString* fileName = [self getPDFFileName];
[PDFRenderer drawPDF:fileName];
[self showPDFFile];
[super viewDidLoad];
}
thanks in advance..
This will do what you need.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
NSString *pdfDirectoryString = [NSString stringWithFormat:#"%#/YOUR_FILE_NAME", documentsDirectoryPath];
NSError *error = nil;
if (![YOUR_PDF_DATA writeToFile:pdfDirectoryString options:NSDataWritingAtomic error:&error])
NSLog(#"Error: %#", [error localizedDescription]);
Try this
For saving PDF in Document Directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSData *data = [PDFImageConverter convertImageToPDF:screenShot]; // convert PDF to data
NSString *path = [NSString stringWithFormat:#"%#/%#",[docArr objectAtIndex:0],[NSString stringWithFormat:#"userDetails.pdf"]];
[data writeToFile:path atomically:NO];
To retrieve and display in web view
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfPathString =[NSString stringWithFormat:#"%#",[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"userDetails.pdf"]]];
NSURL *targetURL = [NSURL fileURLWithPath:pdfPathString];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[contentWebview loadRequest:request];
I have text file text.txt in my xcode project and I need to write NSString init from my iOS app. I've tried this code
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex: 0];
NSString *docFile = [docDir stringByAppendingPathComponent: #"text.txt"];
NSString *content = #"Test";
[content writeToFile: docFile atomically: NO];
But it doesn't work for me... So can you help me with it?
this worked for me
//Method writes a string to a text file
-(void) writeToTextFile{
//get the documents directory:
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//make a file name to write the data to using the documents directory:
NSString *fileName = [NSString stringWithFormat:#"%#/text.txt",
documentsDirectory];
//create content - four lines of text
NSString *content = #"Test";
//save content to the documents directory
[content writeToFile:fileName
atomically:NO
encoding:NSStringEncodingConversionAllowLossy
error:nil];
}
I have files recorded by the user in the documents directory... displayed in a uitableview...
I placed this code in my didSelectRowAtIndexPath... Why doesn't this work?
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileName = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#",[directoryContents objectAtIndex:indexPath.row]]];
NSURL *audioURL = [NSURL URLWithString:fileName];
NSData *audioData = [NSData dataWithContentsOfURL:audioURL];
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer addAttachmentData:audioData mimeType:#"audio/caf" fileName:fileName];
You are creating the URL incorrectly. You need to use fileURLWithPath, not URLWithString.
Also, this:
NSString *fileName = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#",[directoryContents objectAtIndex:indexPath.row]]];
Should be:
NSString *fileName = [documentsDirectory stringByAppendingPathComponent:[directoryContents objectAtIndex:indexPath.row]];
There is no need for the string format.
Last thing. Th file name passed to the addAttachment method should be just a file name, not a full pathname.
i want to play video from html 5 file by zipping and unzip file programmatically of this link
http://enhancelearning.co.in/downloads/HTML5.zip
this file contain 4 html file
zip and unzip successfully created but video not playing in web view
here is th my code
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docspath = [paths objectAtIndex:0];
paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
// NSString *cachePath = [paths objectAtIndex:0];
NSString *zipFile = [docspath stringByAppendingPathComponent:#"001.html"];
NSURL *url = [NSURL fileURLWithPath:zipFile];
[webVew loadRequest:[NSURLRequest requestWithURL:url]];
Try first to put this html file somewhere in the net and see if the video playing when you go to this page from safari.
It's probably HTML-Safari-ios problem, and this way it easier to debug.