I have the pdf file in local resource folder i want to open this file using UIWebView and user can read. But the PDF file is not displaying to read.
NSString *pathToBundle = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:pathToBundle];
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"Insupen_User_Guide" ofType:#"pdf"];
NSString *htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
//CGRect fullScreenRect=[[UIScreen mainScreen]applicationFrame];
catalogue=[[UIWebView alloc]initWithFrame:CGRectMake(50,400,600,600)];
[catalogue loadHTMLString:htmlString baseURL:baseURL];
catalogue.delegate=self;
[self.view addSubview:catalogue];
try this.
NSString *strPath = [[NSBundle mainBundle]pathForResource:#"Insupen_User_Guide" ofType:#"pdf"];
[catalogue loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[strPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]];
maybe this will help you.
NSString *path = [[NSBundle mainBundle]pathForResource:#"HR" ofType:#"pdf"];
NSURL *targeturl = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targeturl];
// _webview = [[UIWebView alloc]init];
[[_webview scrollView] setContentOffset:CGPointMake(0, 500) animated:YES];
[_webview stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:#"window.scrollTo(0.0, 50.0)"]];
[_webview loadRequest:request];
// worked for me perfectly you can try this
In the code you posted you are turning the contents of the PDF file into a UTF8 encoded string and displaying the string inside the web view, this is not what you want.
Instead, you need to make a NSURLRequest pointing to the local PDF and load the request in the web view like so
NSString *pdfPath = [[NSBundle mainBundle] pathForResource:#"Insupen_User_Guide" ofType:#"pdf"];
NSURL *pdfUrl = [NSURL fileURLWithPath:pdfPath];
NSURLRequest *pdfUrlRequest = [NSURLRequest requestWithURL:pdfUrl];
[catalogue loadRequest:pdfUrlRequest];
You can also display PDF in UIDocumentInteractionController using this.
UIDocumentInteractionController *docController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"PDF file name" ofType:#"pdf"]]];
docController.delegate = self;
//[DocView addSubview:docController];
[docController presentPreviewAnimated:YES];
And add these delegate methods in your class.
#pragma mark documentInteractionController methods
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller {
return self;
}
- (UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller {
return self.view;
}
- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController *)controller {
return self.view.frame;
}
- (BOOL)documentInteractionController:(UIDocumentInteractionController *)controller canPerformAction:(SEL)action{
return FALSE;
}
Related
I want to display MSOffice extensions in webview. Is this possible?
I've tried this:
NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:#"Documents"]];
NSString *filePath = [resourceDocPath
stringByAppendingPathComponent:#"myPDF.pdf"];
[pdfData writeToFile:filePath atomically:YES];
CGRect rect = [[UIScreen mainScreen] bounds];
CGSize screenSize = rect.size;
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,screenSize.width,screenSize.height)];
// Now create Request for the file that was saved in your documents folder
//UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 250, self.view.frame.size.width, 300)];
NSURL *targetURL = [NSURL URLWithString:fileurl];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];
But I get the message:
"An error occured while reading the document".
Try below if your ppt file is drag dropped in your project
[webView loadRequest:[NSURLRequest requestWithURL:
[NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"FileName" ofType:#"ppt"]isDirectory:NO]]];
If you are getting your file from server in form of NSData then use below
NSURL *url=[NSURL URLWithString:[#"yourURL" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData* responseData = [NSData dataWithContentsOfURL: url];
[webView loadData:responseData MIMEType:#"application/ppt"
textEncodingName:#"utf-8" baseURL:[NSURL URLWithString:#""]];
Attempting to load an html file into webview that has images that link to other pages in the supporting files folder. The main html file loads but the images and link do not work. Any suggestion on how to solve this problem? I'm referencing my img file as img src="$#%.png".
- (void)viewDidLoad {
[super viewDidLoad];
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"iphone"
ofType:#"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile
encoding:NSUTF8StringEncoding error:nil];
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[myWebView loadHTMLString:htmlString baseURL:baseURL];
[myWebView loadHTMLString:htmlString baseURL:[[NSBundle mainBundle]
bundleURL]];
[myWebView loadHTMLString:htmlString baseURL:nil];
}
Use this code to load html file in UIWebView :
[myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"iphone" ofType:#"html"] isDirectory:NO]]];
I have a view controller that has a UIWebView. I want to load a html file in the UIWebView.
I have kept my html file under the Supporting files(Generated by Xcode)->Resources->html.
Also , I have created the html file and dragged and dropped in to the Html folder.
My code:
-(void)viewDidLoad
{
[super viewDidLoad];
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"contact"
ofType:#"html" inDirectory:#"Resources/html"] ;
NSURL *url = [NSURL fileURLWithPath:htmlFile ];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_webView loadRequest:request];
_webView.delegate=(id)self;
}
What am I missing here . Looks a silly one but no luck..
Please help.
Replace your view did load method with this method. I checked with it. It's working for me. See that your html file should be placed as shown in image.(As per your need)
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIWebView *webView=[[UIWebView alloc]init];
webView.frame=CGRectMake(0, 0, 320, 568);
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"contact" ofType:#"html" inDirectory:nil];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
//Append javascript
//NSString *script = #"<script>alert(\"This is an alert!!\");</script>";
//htmlString = [htmlString stringByAppendingString:script];
webView.delegate = self;
[webView loadHTMLString:htmlString baseURL:nil];
[self.view addSubview:webView];
}
I'm trying to run a web form on an ipad that has no web connection. So far i've successfully been able to load the form into safari through the use of an app that takes a html file and displays it in a browser.
However, the data store sql component of html 5 just doesn't seem to work. I copied the code from html5rocks.com
http://playground.html5rocks.com/#async_transactions
Seems to work everywhere but in my application.
So my app is very simple, it just loads a web browser... here's the relevant code
- (void)viewDidLoad {
// Example 1, loading the content from a URLNSURL
//NSURL *url = [NSURL URLWithString:#"http://dblog.com.au"];
//NSURLRequest *request = [NSURLRequest requestWithURL:url];
//[webView loadRequest:request];
NSString *webPage = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSData *webData = [NSData dataWithContentsOfFile:webPage];
NSString *imagePath = [[NSBundle mainBundle] resourcePath];
imagePath = [imagePath stringByReplacingOccurrencesOfString:#"/" withString:#"//"];
imagePath = [imagePath stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
// NSString *HTMLData = #"
// <h1>Hello this is a test</h1>
// <img src="sample.jpg" alt="" width="100" height="100" />";
// [webView loadHTMLString:HTMLData baseURL:[NSURL URLWithString: [NSString stringWithFormat:#"file:/%#//",imagePath]]];
//[webView loadHTMLString:webPage baseURL:[NSURL URLWithString: [NSString stringWithFormat:#"file:/%#//",imagePath]]];
//[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:webPage isDirectory:NO]]];
NSString *baseStr = [NSString stringWithFormat:#"file:/%#//",imagePath];
NSURL *baseURL = [NSURL URLWithString: baseStr];
NSLog(#"%#",baseStr);
//webView.scalesPageToFit = YES;
//webView.multipleTouchEnabled = YES;
/*for (id subview in webView.subviews){
if([[subview class] isSubclassOfClass: [UIScrollView class]])
((UIScrollView *)subview).bounces = NO;
}*/
[webView loadData:webData MIMEType:#"text/html" textEncodingName:#"UTF-8" baseURL:baseURL];
//[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.cyberdesignworks.com.au/clients/ideatoaction"]]];
//[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"Test" ofType:#"svg"]isDirectory:NO]]];
[super viewDidLoad];
The problem was that Xcode tries to compile my js files.
DAMN YOU APPLE!
anyway look on the right in the list of items in xcode when you have a project open
look for targets, it has a picture of a bow and arrow target, or like a red and white dart board. open that up, look at compiled resources. Move any js files from there to the copy bundled resources.
Bam! it should all work as intended now.
I have an iPad UIWebView which has to display some rtfd.zip files. I try to do it like this:
-(void) viewWillAppear:(BOOL)animated {
[self loadFile:string];
}
- (void)loadFile:(NSString*)file
{
NSString* resourcePath = [[NSBundle mainBundle] bundlePath];
NSString* sourceFilePath = [resourcePath stringByAppendingPathComponent:file];
NSURL* url = [NSURL fileURLWithPath:sourceFilePath isDirectory:NO];
NSURLRequest* request = [NSURLRequest requestWithURL:url];
[webview loadRequest:request];
[resourcePath release];
}
But I only get a blank page. The outlets are definitely connected properly - the UIWebView can load google main page.
What am I doing wrong?
Thanks in advance!
Try to use this, I got output by using this,
- (void)loadFile:(NSString*)file {
UIWebView *webview=[[UIWebView alloc] init];
NSString *FullstrURL=[[NSBundle mainBundle] pathForResource:file
ofType:#"html" ];
NSLog(#"HTML String = %#",FullstrURL);
[webview loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:FullstrURL]]];
}