Split View App, display PDF's in the Detail View, PDF's are local storage - ios

I have a Split View App that I am building, using a plist for my Array, and using a UIWebView to display content in the DetailViewController. Using the below code to make calls to the URL address in my plist, and the URL Address is displaying a PDF in the DetailViewController through the UIWebView.
DetailViewController.m
NSString *mutareURL = [_detailItem description];
NSURL *url = [NSURL URLWithString:mutareURL];
[_catalog loadRequest:[NSURLRequest requestWithURL:url]];
What I need to do is have all of the PDF's in my resource folder in my project. I can use an NSArray or plist for the RootViewController (TableView), but I need to have the ability to display the PDF's in the DetailViewController through the UIWebView when a Row is selected in the RootViewController (TableView). I tried this Code in my DetailViewController.m file, but it is not working.
NSString *path = [[NSBundle mainBundle] pathForResource:#"%#" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[_Pdf loadRequest:request];
I have an NSLog in my RootViewController.m file with this code, and i do see that the names of the pdf files are showing up in my debugger. I just do not know how to get them from the RootViewController and pass them into the pathforResourse"#"%#" ofType:#"pdf"]; area. brochures is an NSArray that has all of the addObject:#"nameofpdfs" that is passing.
RootViewController.m
detailViewController.detailItem = [NSString stringWithFormat:#"%#", [brochures objectAtIndex:indexPath.row]];
NSLog(#"Cell created for row: %d", [indexPath row]);
NSLog(#"brochures = %#", detailViewController.detailItem);
Thank you

The answer is probably in the last snippet. The first line essentially passes the selected filename from the RootViewController to the DetailViewController by setting the latter's detailItem property.
If you replace
NSString *path = [[NSBundle mainBundle] pathForResource:#"%#" ofType:#"pdf"];
by
NSString *path = [[NSBundle mainBundle] pathForResource: self.detailItem ofType:#"pdf"];
it should work.
Depending on whether the filenames in the brochures array already contain the #".pdf" suffix, you might have to leave it out of the message send:
NSString *path = [[NSBundle mainBundle] pathForResource: self.detailItem ofType: nil];

Related

viewcontroller using a NULL baseURL argument with the loadHTMLString baseURL method : data theorem

I am facing the problem "MyViewcontroller using a NULL baseURL argument with the loadHTMLString baseURL method : data theorem"- i have successfully completed my task and all are working fine.
The Issue was in the OSWAP security scan for vulnerability it shows the above error.
My code snippet:-
NSString *aHtmlString = kEmptyString;
// Getting the bool from configuration plist
NSString *thePlistPath = [[NSBundle mainBundle] pathForResource:#"config" ofType:#"plist"];
NSDictionary *theURLdata = [[NSDictionary alloc] initWithContentsOfFile:thePlistPath];
is
ServerFAQAvailable = [[theURLdata valueForKey:kIsServerFAQAvailableKey] boolValue];
if (one || two || three) {
aHtmlString = [self loadFAQFor];
} else {
aHtmlString = [self loadFAQForwithout];
}
NSURL *baseURL = [NSURL fileURLWithPath:thePlistPath];
[self.faqWebView loadHTMLString:aHtmlString baseURL:baseURL];
Update:
if (one || two || three) {
aHtmlString = [self loadFAQFor];
} else {
aHtmlString = [self loadFAQForwithout];
}
NSURL *baseURL = [NSURL fileURLWithPath:#"about:blank"];
[self.faqWebView loadHTMLString:aHtmlString baseURL:baseURL];
Still shows me scan issue
The issue is the baseURL: parameter. BaseURL isn't needed for an html string, it is usually used for relative links. If all you're trying to do is show some html, are you sure you need it?
The security issue flagged makes sense (my understanding, roughly): If a webview's baseURL is set to the local file system, then a page loaded (eventually) through that webview could access local resources.
Try passing nil for baseURL: should silence this warning.
baseURL should be [[NSBunld mainBunld] bunldPath], you can try like this..
NSString *path = [[NSBundle mainBundle]bundlePath];
NSString *htmlPath = [[NSBundle mainBundle] pathForResource:self.htmlName ofType:#"html"];
NSString *htmlContent = [NSString stringWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error:nil];
[self.webView loadHTMLString:htmlContent baseURL:[NSURL fileURLWithPath:path]];

Xcode app run on simulator but not on device

i've saw a lot of topic but no one answers to my questions.
This Program run on simulator but when i executed it , it crash .
This is my simple code. Thanks you!
NSURL *url=[NSURL fileURLWithPath:#"/Users/marco/Desktop/Letters/1.txt"];
NSString *fileContent = [NSString stringWithContentsOfURL:url];
NSLog(#"fileContent = %#", fileContent);
NSArray * a = [NSArray arrayWithObjects:fileContent, nil];
Output.text=[a objectAtIndex:0];
NSLog (#"The 4th integer is: %#", a);
Move your 1.txt file to your device, for example your documents folder.
Then change the first line of your app like so:
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *filePath = [NSString stringWithFormat:#"%#/1.txt", documentsPath];
NSURL *url = [NSURL fileURLWithPath:filePath];
Since you say this works on the simulator, I'll have to assume you added this file to your project properly. If not, add the file to your project and target. You can get the URL to any resource you included at build time through NSBundle:
NSBundle *bundle = [NSBundle bundleForClass:[self class]]; // often you will see [NSBunble mainBundle, but both work
NSURL *url = [bundle URLForResource:#"1" withExtension:#"txt"];

how to pass local html files using array in xcode

here i am trying for a application,
when i click on the table view it will redirect to another page called details. there I have used a webview to dispaly the selected url value.
here is my code
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:YES];
self.navigationItem.title=#"sample";
servicesArray=[[NSMutableArray alloc]init];
[servicesArray addObject:#"1"];
[servicesArray addObject:#"2"];
[servicesArray addObject:#"3"];
urlsArray=[[NSMutableArray alloc]init];
[urlsArray addObject:#"http://www.google.com"];
[urlsArray addObject:#"http://www.yahoo.com"];
[urlsArray addObject:#"http://www.facebook.com"];
}
here instead of urls link i want to pass my local html files. can you please help me out
Thanks
chintu.
try like this,take all the html file names in Array yourArray.
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:[yourArray objectAtIndex:selectedIndex] ofType:#"html" inDirectory:nil];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[WebView loadHTMLString:htmlString baseURL:nil];

Load local html files using array [duplicate]

here i am trying for a application,
when i click on the table view it will redirect to another page called details. there I have used a webview to dispaly the selected url value.
here is my code
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:YES];
self.navigationItem.title=#"sample";
servicesArray=[[NSMutableArray alloc]init];
[servicesArray addObject:#"1"];
[servicesArray addObject:#"2"];
[servicesArray addObject:#"3"];
urlsArray=[[NSMutableArray alloc]init];
[urlsArray addObject:#"http://www.google.com"];
[urlsArray addObject:#"http://www.yahoo.com"];
[urlsArray addObject:#"http://www.facebook.com"];
}
here instead of urls link i want to pass my local html files. can you please help me out
Thanks
chintu.
try like this,take all the html file names in Array yourArray.
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:[yourArray objectAtIndex:selectedIndex] ofType:#"html" inDirectory:nil];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[WebView loadHTMLString:htmlString baseURL:nil];

UIWebview - Loading PNG image - got error

I am trying to load the image(png) in UIWebview. But I got the following error
""ImageIO: PNGinvalid literal/lengths set"
I will get the "png" from the web service. I will store it in the following path. "Users/XXX/Library/Application Support/iPhone Simulator/4.3.2/Applications/E4DE3097-EA84-492F-A2A7-5882202F3B00/Documents/attachement.png "
When I reading the file from the path, I got the error. I am using the following code
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
NSString *commentHtml = [NSString stringWithFormat:#"<img src=\"%#\"/>", documentEntity.path];
NSString *html3 = [NSString stringWithFormat:#"<html><head/><body><div><b>COMMENTS:</b></div>%#</body></html>", commentHtml];
[self.documentView loadHTMLString:html3 baseURL:baseURL];
Please help me.
THanks
Girija
If you have saved the Image in Documents Folder, why are you fetching from MainBundle.
Fetching Image from the Documents Directory would be :
NSString *aDocumentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *aFilePath = [NSString stringWithFormat:#"%#/attachement.png", aDocumentsDirectory];
UIImage *anImage = [UIImage imageWithContentsOfFile:aFilePath];

Resources