Crash when trying to open downloaded pdf in new view - path

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!

Related

iOS: Download and save PDF file in Documents app

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);

Download XLS sheet from server to IPhone

How to download a .xls sheet from server and save it in iPhone memory, if spreadsheet is bigger in size then process took place in background.
Try this in any method.
NSURL *url=[NSURL URLWithString:#"http://en.wikipedia.org/wiki"]; //Your URL here
NSData *dbFile = [[NSData alloc] initWithContentsOfURL:url];
NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent]stringByAppendingPathComponent:#"Documents"]];
NSString *filePath = [resourceDocPath stringByAppendingPathComponent:#"Text_file.xls"];
[dbFile writeToFile:filePath atomically:YES];
I assume that you server doesn't require authentication

Show downloaded PDF file with webView ios

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
}

CSS and JS Files are not loading in uiwebview

In my app, a website included in a folder. I tried to load it in webview using the following code:
self.wView.dataDetectorTypes = UIDataDetectorTypeLink;
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html" inDirectory:NO];
NSLog(#"%#",htmlFile);
//NSData *pathData = [NSData dataWithContentsOfFile:htmlFile];
//NSString *htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
NSURL *url = [NSURL URLWithString:[htmlFile lastPathComponent] relativeToURL:[NSURL fileURLWithPath:[htmlFile stringByDeletingLastPathComponent] isDirectory:YES]];
//(void)[NSURLConnection connectionWithRequest:req delegate:self];
[self.wView loadRequest:[NSURLRequest requestWithURL:url]] ;
I get the file path in nslog.
But in my webview, css and js files are not loading.
Please help me.
Simple Solution for this problem
Just select the option Create Folder references for any added folders while you are copying the Website folder into XCode

Getting bundle file references / paths at app launch

Suppose I have an arbitrary set of files included in the Main App Bundle. I would like to fetch the file URLs for those at launch and store them somewhere. Is this possible using NSFileManager? The documentation is unclear in that regard.
Note: I only need the file URLs, I do not need to access the actual files.
You can get the URL of a file in the main bundle using
NSString *path = [[NSBundle mainBundle] pathForResource:#"SomeFile" ofType:#"jpeg"];
NSURL *url = [NSURL fileURLWithPath:path];
You can write this URL to, for example, a property list file in the Documents directory:
NSString *docsDir = [NSSearchForDirectoriesInDomains(NSDocumentsDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *plistPath = [docsDir stringByAppendingPathComponent:#"Files.plist"];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[url absoluteString] forKey:#"SomeFile.jpeg"];
[dict writeToFile:plistPath atomically:YES];
If you don't know the names of the files and you just want to list all the files in the bundle, use
NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[[NSBundle mainBundle] bundlePath] error:NULL];
for (NSString *fileName in files) {
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:nil];
NSURL *url = [NSURL fileURLWithPath:path];
// do something with `url`
}
Or in Swift 4:
let url = Bundle.main.url(forResource: "FileName", withExtension: ".xyz")
Yes, you would get their path:
NSString *path = [NSBundle mainBundle] pathForResource:#"file1" ofType:#"png"];
NSURL *fileURL = [NSURL fileURLWithPath:path]
// save it as any other object or in a dictionary:
[myMutableDictionary setObject:fileURL forKey:#"file1.png"];
EDIT: to get the complete list of files, use the NSFileManager, get the path to the bundle itself, then walk each directory getting the files, making URLs, and saving them somewhere. There is oodles of code on SO how to walk a directory to do this. [You should update your question to be more specific on what you want, this was not made clear at all originally]
There's an API on NSBundle (documentation) available on 10.6 or iOS 4 to get an NSURL directly, rather than passing a path to the NSURL constructor:
- (NSURL *)URLForResource:(NSString *)name withExtension:(NSString *)ext;
It also has variants that take a subdirectory and localizationName, the same as its -pathForResource: equivalents.

Resources