stringWithContentsOfFile works, but requestWithURL doesn't - iphone

For some reason, the first method doesn't display anything, while the second does:
//Common
NSString *path=[[NSBundle mainBundle] pathForResource:#"about" ofType:#"html"];
//Method1
[webView loadRequest:[NSURLRequest requestWithURL: [NSURL fileURLWithPath: path isDirectory: NO]]];
//Method 2
NSString *HTML=[NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];
[webView loadHTMLString:HTML baseURL:[NSURL URLWithString:[[NSBundle mainBundle] bundlePath]]];
//Common
UIViewController* controller=[webView controllerWithTitle:#"Help"];
[delegateRef.navController pushViewController:controller animated:YES];
On the other hand, I also tried loading it using a URL as well. Similarly, the first method didn't display anything while the second did.
//Common
NSURL *url=[[NSBundle mainBundle] URLForResource:#"about" withExtension:#"html"];
//Method 1
[webView loadRequest:[NSURLRequest requestWithURL:url]];
//Method 2
NSString *HTML=[NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:NULL];
[webView loadHTMLString:HTML baseURL:[NSURL URLWithString:[[NSBundle mainBundle] bundlePath]]];
Strangely, if I change the URL as follows, then both methods work (of course loading the string doesn't show css):
NSURL *url=[NSURL URLWithString:#"http://www.google.com"];
controllerWithTitle is defined in a category as follows:
-(UIViewController*) controllerWithTitle:(NSString*)title{
UIViewController *vc=[[[UIViewController alloc] init] autorelease];
vc.view=self;
vc.title=title;
return vc;
}

It turns out that the CSS was causing the entire page to not display!

Related

Display local pdf file in UIWebView in iphone

I am display local pdf file in UIWebView but it shows exception in path here is my code
NSString*fileName=content_Source;
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:#"pdf"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView setScalesPageToFit:YES];
[webView loadRequest:request]
You must be using the file name extension while setting the file name. So make sure the extension will not be used in file name if it's setting along with Type (like ofType:#"pdf").
Here's my code:
UIWebView *webView = [[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
webView.backgroundColor = [UIColor redColor];
NSString*fileName= #"About_Downloads";
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:#"pdf"];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:path isDirectory:NO]];
[webView setScalesPageToFit:YES];
[webView loadRequest:request];
[self.view addSubview:webView];
There's a better way to show PDFs:
UIDocumentInteractionController *dc = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:path isDirectory:NO]];
dc.delegate = self;
[dc presentPreviewAnimated:YES];
Make sure use the delegate along with that code to work it properly.
(UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller{
return self;
}
first validate that string and the assign to URL like bellow..
NSString *strURLPath = [self trimString: path];
NSURL *url = [NSURL fileURLWithPath:strURLPath];
use my bellow method...
-(NSString*) trimString:(NSString *)theString {
if (theString != [NSNull null])
theString = [theString stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
return theString;
}
It is working fine with the given code problem was that in my file Name i was giving also the type like ali.pdf they type pdf so it get exception it worked fine now when i use filename like following
NSString*fileName=#"ali";
Instead of
NSString*fileName=#"ali.pdf";
CGRect rect = [[UIScreen mainScreen] bounds];
CGSize screenSize = rect.size;
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,screenSize.width,screenSize.height)];
NSString *path = [[NSBundle mainBundle] pathForResource:#"pdf" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];

Getting application crash during opening a local pdf file on UIWebView

- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSString *path = [[NSBundle mainBundle] pathForResource:#"About" ofType: #"pdf"];
NSLog(#"%#", path);
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[_webView loadRequest:request];
}
just use following code for open PDF in web view
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"About" ofType:#"pdf"];
[self.webVctr loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]]];
And check it out the PDF is exist or not?
This thinks may helping to you
Happy codeing..
Try this:
in the
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];
NSString *path = [[NSBundle mainBundle] pathForResource:#"About" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];
[webView release];
It was some other issue not related to the pdf loading over web view. I cross checked and found the actual cause of crash. Anyways thanks guys for the help

How to use HTML file in iPhone

I want to use an HTML file in my code currently I am using following line of code:
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"htm" inDirectory:nil];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[WebView loadHTMLString:htmlString baseURL:nil];
But the problem which I am facing is: the HTML Page contains some images, and all that images are in a folder in my project but I am not able to see that images in the HTML page at run time.
You can set the baseURL to your resource folder, where your images are located:
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"htm" inDirectory:nil];
NSString *htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
NSString *resourcePath = [[NSBundle mainBundle]resourcePath];
NSURL *baseUrl = [[NSURL alloc]initFileURLWithPath:resourcePath];
[self.myWebView loadHTMLString:htlmString baseURL:baseUrl];
Here the possible ways to use HTML into the project,
Following code for the load HTML file into xcode project
Example 1, loading the content from a URLNSURL
NSURL *url = [NSURL URLWithString:#"http://google.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
Example 2, loading the content from a string
NSString *HTMLData = #"<h1>Hello this is a test</h1>";
[webView loadHTMLString:HTMLData baseURL:nil];
Example 3, loading the content from a local file
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"sample" ofType:#"html"];
NSData *htmlData = [NSData dataWithContentsOfFile:htmlFile];
[webView loadData:htmlData MIMEType:#"text/html" textEncodingName:#"UTF-8" baseURL:[NSURL URLWithString:#""]];
Hope this code helping to develop a application

How to add CSS file to iphone project in xcode?

I am working on a web based application for iphone, I have created new cssfile to the project this css file doesn't affect the html file, but when I have uploaded the css file a host it worked fine and styled the html
What is the problem ?
edit 1
This the head element .
css.css is working fine and is linked to the html correctly
jquery.mobile-1.0.min.css is NOT !
Despite the both files exist in the same border
Onotha.com
<link rel="stylesheet" type="text/css" href="css/jquery.mobile-1.0.min.css" />
edit 2
This is the Objective-C code, I have an exception, I think in the last line of code
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[window makeKeyAndVisible];
NSString *path= [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html" inDirectory:NO];
// [webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]];
// NSString *path = [[NSBundle mainBundle] bundlePath];
// NSURL *baseURL = [NSURL fileURLWithPath:path];
// [webView loadHTMLString:htmlString baseURL:baseURL];
// load css styles
NSString *cssPath = [[NSBundle mainBundle] pathForResource:#"css/jquery.mobile-1.0.min.css" ofType:#"css"];
NSData *cssData = [NSData dataWithContentsOfFile:cssPath];
NSString *cssString = [[NSString alloc] initWithData:cssData encoding:NSASCIIStringEncoding];
// load js
NSString *jsPath = [[NSBundle mainBundle] pathForResource:#"MyJS" ofType:#"js"];
NSData *jsData = [NSData dataWithContentsOfFile:jsPath];
NSString *jsString = [[NSString alloc] initWithData:jsData encoding:NSASCIIStringEncoding];
// compose full html page
NSString *pageContent = [NSString stringWithFormat:#"%#%#%#", cssString, jsString, path];
[webView loadHTMLString:pageContent baseURL:baseURL];
return YES;
}
edit 3
I got this after using code posted by Srikar
You can load CSS from local project directory
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[webView loadHTMLString:htmlString baseURL:baseURL];
detail info check this site.
More elaborate code here -
// load css styles
NSString *cssPath = [[NSBundle mainBundle] pathForResource:#"MyCSS" ofType:#"css"];
NSData *cssData = [NSData dataWithContentsOfFile:cssPath];
NSString *cssString = [[NSString alloc] initWithData:cssData encoding:NSASCIIStringEncoding];
// load js
NSString *jsPath = [[NSBundle mainBundle] pathForResource:#"MyJS" ofType:#"js"];
NSData *jsData = [NSData dataWithContentsOfFile:jsPath];
NSString *jsString = [[NSString alloc] initWithData:jsData encoding:NSASCIIStringEncoding];
// compose full html page
NSString *pageContent = [NSString stringWithFormat:#"%#%#%#", cssString, jsString, actualPageMarkup];
[webView loadHTMLString:pageContent baseURL:[NSURL URLWithString:#""]];
More info here
I don't know if you have solved this, but I think you have to load in "pageContent" the content of the page instead of the path.
Something like this:
NSString *htmlString = [NSString stringWithContentsOfFile:path encoding:NSASCIIStringEncoding];
NSString *pageContent = [NSString stringWithFormat:#"%#%#%#", cssString, jsString, htmlString];
[webView loadHTMLString:pageContent baseURL:baseURL];

Can't load html file to webview

Here is my code :
NSString *path1 = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"NewSouthWales.html"];
NSURL *pageURL = [NSURL fileURLWithPath:path1];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:pageURL];
//Load the request in the UIWebView.
[self.webView loadRequest:requestObj];
I am getting output in my webview as :
like
........
instead of giving me proper look of that file it shows page source code.
Please help ...
Thanks in advance
First prefer pathForRessource ofType
pathNSString *path1 = [[NSBundle mainBundle] pathForResource:#"NewSouthWales" ofType:#"html"];
And then if the file is local why send a request ?
Just use :
NSString *html = [NSString stringWithContentsOfFile:path1 encoding:NSUTF8StringEncoding error:nil];
[self.webView loadHTMLString:html baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]bundlePath]]];
NSString *path1 = [[NSBundle mainBundle] pathForResource:#"NewSouthWales" ofType:#"html"];
NSURL *baseurl = [NSURL fileURLWithPath:path1];
NSData *pathData = [NSData dataWithContentsOfFile:path1];
//Check first if the file path's data is captured. if true, load the html on the web view
if (pathData) {
[webView loadData:pathData MIMEType:#"text/html" textEncodingName:#"UTF-8" baseURL:baseurl];
}