I want to download a PDF file then open it in a webView. I searched about that then I found this issu. The accepted answer describes how to download and display the file. When I test it, I can not find the file in the Documents because it's stored inside my app. Also I don't think that the file is saved because the pdf is never shown in the webView even if I pass the filePath to be loaded. This is the code of the answer:
// Get the PDF Data from the url in a NSData Object
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[
NSURL URLWithString:#"http://www.example.com/info.pdf"]];
// Store the Data locally as PDF File
NSString *resourceDocPath = [[NSString alloc] initWithString:[
[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent]
stringByAppendingPathComponent:#"Documents"
]];
NSString *filePath = [resourceDocPath
stringByAppendingPathComponent:#"myPDF.pdf"];
[pdfData writeToFile:filePath atomically:YES];
// Now create Request for the file that was saved in your documents folder
NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView setUserInteractionEnabled:YES];
[webView setDelegate:self];
[webView loadRequest:requestObj];
And this is what I see in the log:
libMobileGestalt MobileGestaltSupport.m:153: pid 2828 (my_app) does not have sandbox access for frZQaeyWLUvLjeuEK43hmg and IS NOT appropriately entitled
2017-08-01 10:29:06.882562+0100 my_app[2828:1334057] libMobileGestalt MobileGestalt.c:550: no access to InverseDeviceID (see <rdar://problem/11744455>)
What is the problem with what I did? And how can I put the downloaded file in the Documents app of my iPhone in order to be checked whenever the user want like any other PDF?
Sandbox path Error
NSString *resourceDocPath = [[NSString alloc] initWithString:[
[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent]
stringByAppendingPathComponent:#"Documents"
]];
Please use your sandbox path instead of bundle path.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
Related
Is there an ability to load files like documents, articles from server and store them in application local storage using Filemanager? And how then users can read these files? Do they have access to that folder?
Follow these steps for downloading and saving file in NSDocumentDirectory
// Determine local file path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [NSString stringWithFormat:#"%#/%#", [paths objectAtIndex:0],#"fileName.doc"]; //it could be any file type
// Download and write to file
NSURL *url = [NSURL URLWithString:#"http://www.filePathFromServerLocation.com"];
//and write to file
NSData *urlData = [NSData dataWithContentsOfURL:url];
[urlData writeToFile:filePath atomically:YES];
This way your file will be save at filePath and u can use it in future whatever the way u like.
Opening a file in iOS:
Using a webview
NSString *path = [[NSBundle mainBundle] pathForResource:#"document" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
using a UIDocumentInteractionController.
Have a look at this Apple sample code for ZoomingPDFViewer
I'm developing an app for iOS 8.1 using Xcode 6.1.1
And I want to download a PDF from internet, store in to my iPad and then load it into a webview; this is the PDF test:
this is the result I get on iPad after trying to display it on my WebView:
this is my code so far...
// Get the PDF Data from the url in a NSData Object
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:#"http://www.ferc.gov/docs-filing/elibrary/larg-file.pdf"]];
// Store the Data locally as PDF File
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentFolderPath = searchPaths[0];
NSString *archivePath = [documentFolderPath stringByAppendingPathComponent:#"larg-file.pdf"];
[pdfData writeToFile:archivePath atomically:YES];
// Now create Request for the file that was saved in your documents folder
NSURL *url = [NSURL fileURLWithPath:archivePath];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_webView setUserInteractionEnabled:YES];
[_webView setDelegate:self];
[_webView loadRequest:requestObj];
How do I make this work?
EDIT: this is the answer
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentFolderPath = searchPaths[0];
NSString *archivePath = [documentFolderPath stringByAppendingPathComponent:#"larg-file.pdf"];
if (![Functions isAlreadyStoredinDeviceFileWithName:#"larg-file.pdf"]) {
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:#"http://www.ferc.gov/docs-filing/elibrary/larg-file.pdf"]];
[pdfData writeToFile:archivePath atomically:YES];
}
// Now create Request for the file that was saved in your documents folder
NSURL *url = [NSURL fileURLWithPath:archivePath];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_webView setUserInteractionEnabled:YES];
[_webView setDelegate:self];
[_webView loadRequest:requestObj];
This code:
NSString *resourceDocPath = [[NSString alloc] initWithString:[
[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent]
stringByAppendingPathComponent:#"Documents"]];
Attempts to formulate a directory within the application bundle. You do not have write access to the application bundle. Your file therefore isn't saved.
Check out -[NSFileManager URLsForDirectory:inDomains:] or NSSearchPathForDirectoriesInDomains for how to get the path to the user's documents folder.
I'm making an iPhone app, that will download a PDF file and display it in a webView.
However my script will not show the downloaded PDF. It does download it and save it in Documents, but the webView will not show it.
Here's my script:
NSString *path = [[NSBundle mainBundle] pathForResource:#"3" ofType:#"pdf"];
NSURL *urlen = [NSURL fileURLWithPath:path];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:urlen];
[webView loadRequest:urlRequest];
[webView setScalesPageToFit:YES];
From the official documentation on NSURL official documentation on NSURL.
Sending nil as the argument to fileURLWithPath: produces an exception.
The problem then is actually with [[NSBundle mainBundle] pathForResource:ofType:]. This is returning nil, rather than an actual path to a file.
The problem here is actually that [NSBundle mainBundle] refers to files that are bundle with your app. You need to look in your app's document directory, which is where it stores files it has downloaded.
This method will give you the path to your app's document directory:
NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
Now, just append the file name to this path:
NSString *pdfPath = [documentsPath stringByAppendingPathComponent:#"3.pdf"];
And for good measure (because crashes are always bad), make sure the file exists as such:
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:pdfPath];
And finish as such:
if (fileExists) {
NSURL *urlen = [NSURL fileURLWithPath:pdfPath];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:urlen];
[webView loadRequest:urlRequest];
[webView setScalesPageToFit:YES];
} else {
// probably let the user know there's some sort of problem
}
My app downloads a pdf and then on a button press brings it up in a new view.
I get the error:
-[NSURL initFileURLWithPath:]: nil string parameter'
After some troubleshooting I pinned the problem to somewhere in this code snippet. The path that is being pointed to is in the /Documents folder where the downloaded pdf is placed. Thus the document is not in the main bundle.
NSString *path = [[NSBundle mainBundle] pathForResource:PDFpathwithextension ofType:#"pdf"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
Here's the download code:
//Start an NSURL connection to download from the remotepath
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:remotepathURL];
//Store the Data locally as PDF File
NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:#"Documents"]];
NSString *filePath = [resourceDocPath stringByAppendingPathComponent:[newdata.ThirdPickerName stringByAppendingFormat:#".pdf"]];
pdfData writeToFile:filePath atomically:YES];
As NSURL is telling you, you've handed it nil instead of a valid path.
nil here means no such resource could be found by that name. Indeed, your question suggests you're well aware of this.
Since you claim your app already downloads a PDF, does it actually write that out to disk? If so, you should know where the resulting file is from doing that. If not, you first need to write the actual download code!
I'm trying to download content with an URL to a local file system in my iPad (cachedDir ? )
with this function:
- (IBAction)download:(id)sender {
NSURL *url = [NSURL URLWithString:#"http:www.google.de"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request startAsynchronous];
[request setDownloadDestinationPath:#"/Users/ben/Desktop/my_file.pdf"]; // Which //directory??
}
Which directory do I have to choose for my path if I want to store the data as long as possible without getting rejected by Apple,
and how do I retrieve the data I've saved?
Can somebody help?
The best place to store documents that you want to keep around is your application's Documents directly. You can find the path to it like so:
// Returns the URL to the application's Documents directory.
- (NSURL *)applicationDocumentsDirectory
{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
Apple have a useful piece of documentation about the iOS file system and where to store particular types of files: http://developer.apple.com/library/mac/#documentation/FileManagement/Conceptual/FileSystemProgrammingGUide/FileSystemOverview/FileSystemOverview.html
Try out this my code for save pdf in document using below code
NSString *downloadUrl=[NSURL URLWithString:#"http:www.google.de"];
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:downloadUrl]];
//Store the Data locally as PDF File
NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:#"Documents"]];
NSString *pdf1 = #"title.pdf";
NSString *filePath = [resourceDocPath stringByAppendingPathComponent:pdf1];
[pdfData writeToFile:filePath atomically:YES];
and retrive your file using
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSURL *pdfURL = [[NSBundle bundleWithPath:[paths objectAtIndex:0]] URLForResource:[NSString stringWithFormat:#"title.pdf"] withExtension:nil];
NSLog(#"pDF URl %#", pdfURL);
pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);