My app has to be able to download a file and display it. The format could be any of the major format types. I have been doing this with a UIWebView but when iOS 8 rolled out it broke this feature. I am wondering if this an issue with iOS 8 or is there a work around that I can use?
Here is my code:
//To show how I create the file path
NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:#"Documents"]];
//Name is the file's name that I get from the web service I use.
NSString *filePath = [resourceDocPath stringByAppendingPathComponent:name];
- (void) showFile:(NSString *)path andFileName:(NSString *)name
{
dispatch_async(dispatch_get_main_queue(), ^{
self.webView = [[UIWebView alloc] initWithFrame:self.view.frame];
NSURL *targetURL = [NSURL fileURLWithPath:self.filePath];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[self.webView loadRequest:request];
self.webView.delegate = self;
[self.view addSubview:self.webView];
});
}
Thank you in advance.
I ran into the same problem rendering a pdf document on iOS 8.
So far, I've noticed this only for pdfs. So i check and see if the file is a pdf or not.
This is what i'm doing as a workaround.
NSData *pdfData = [[NSData alloc] initWithContentsOfFile:fileLocation];
[self.webView loadData:pdfData MIMEType:#"application/pdf" textEncodingName:#"utf-8" baseURL:nil];
I found out what the issue was in my case and so I wanted to post the answer.
I was using a direct file path:
NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:#"Documents"]];
When I should have been using a relative path like so:
NSString *resourceDocPath;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
resourceDocPath = [paths objectAtIndex:0];
They changes the file structure for iOS 8 which is why my code wasn't working since I was looking in the wrong place. I hope this helps someone else.
Related
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);
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 tried to show an html page that is inside my app, but it did not show on the view:
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask ,YES );
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"abt.html"];
NSURLRequest *documentsRequest = [NSURLRequest requestWithURL:
[NSURL fileURLWithPath:path]] ;
[_webview loadRequest:documentsRequest] ;
I followed this link: iOS - display content from an html resource file or remote webpage in a webview
by Matt Gibson
Please let me know how to show.
thanks
As you said your html page is inside the app so try this one, I have used it and it's work fine.
NSString *htmlFile=[[NSBundle mainBundle] pathForResource:#"abt" ofType:#"html"];
NSString *htmlString=[NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:Nil];
[_webview loadHTMLString:htmlString baseURL:nil];
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
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);