Loading PDF file from Resource directory in iPhone crashes - iphone

NSURL *pdfURL = [[NSBundle mainBundle] URLForResource:#"sampleLayout.pdf" withExtension:nil];
This above line warns NSBundle may not respond to -URLForResource:withExtension:
and the app crashes while loading the PDF file from this URL path.

Why dont you try like this..?
NSString *urlPath = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"pdf"];
NSURL *url = [NSURL fileURLWithPath:urlPath];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webViewOutlet loadRequest:urlRequest];

NSURL *pdfPath = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"test" ofType:#"pdf"]];
i got working from this above line, i think this is alternative for ipad 3.2 from iphone 4.0 os

Related

NSURL add parameters to fileURLWithPath method

I use this line of code to load a local html file into a web view:
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"index" ofType:#"html" inDirectory:#"html"]];
However I want to add some http parameters to the url with no luck so far.
I've tried this:
url = [url URLByAppendingPathComponent:#"?param1=1"];
But after this a html doesn't load in webview.
Is there a way to load local html file in webview with params ?
Do this:
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"index" ofType:#"html" inDirectory:#"html"]];
NSString *URLString = [url absoluteString];
NSString *queryString = #"?param1=1";
NSString *URLwithQueryString = [URLString stringByAppendingString: queryString];
NSURL *finalURL = [NSURL URLWithString:URLwithQueryString];
NSURLRequest *request = [NSURLRequest requestWithURL:finalURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:(NSTimeInterval)10.0 ];
[web loadRequest:request];
The most upvoted answer by Prince doesn't always work.
In iOS 7 Apple introduced NSURLComponents class, we can leverage that to securely add a query into NSURL.
NSURL *contentURL = ...;
NSURLComponents *components = [[NSURLComponents alloc] initWithURL:contentURL resolvingAgainstBaseURL:NO];
NSMutableArray *queryItems = [components.queryItems mutableCopy];
if (!queryItems) queryItems = [[NSMutableArray alloc] init];
[queryItems addObject:[NSURLQueryItem queryItemWithName:#"access_token" value:#"token_here"]];
components.queryItems = queryItems;
NSURL *newURL = components.URL;
I use like this way:
NSURL *url=[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"index" ofType:#"xml"]];
If your file included into the Resource folder of project navigator. otherwise you have to set your full path in NSString and use that in your NSURL path.

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

NSBundle pathForResource method

NSURL *modelURL1 = [[NSBundle mainBundle] URLForResource:#"MyTest" withExtension:#"momd"];
NSLog(#"%#", [modelURL1 absoluteURL]);
NSString *path = [[NSBundle mainBundle] pathForResource:#"MyTest" ofType:#"momd"];
NSURL *modelURL = [NSURL URLWithString:path];
NSLog(#"%#", [modelURL absoluteURL]);
The first line is generated by XCode when I use Core Data, but it does not work because the method doesn't work prior to iOS 4. So I need another method, I thought I could use the second one but it returns nil...Why does it not work?
Thanks
The second one doesn't work because path isn't a URL. Use this instead:
NSURL *modelURL = [NSURL fileURLWithPath:path];

iPhone SDK: WebView how to specify a local URL?

I have HTML files that I want to load locally. I have included the files in the resources folder in XCode. I am not sure what the syntax is to load them.
This is the code I use to connect to say google.
NSString *urlAddress=#"http\\someurl";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webHelpView loadRequest:requestObj];
Could someone provide an example where a HTML file is loaded locally.
Thanks in advance.
This is the only code that worked for me.
NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"SettingsHelp.html"];
NSURL *url = [NSURL fileURLWithPath:path isDirectory:NO];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
Thanks all for the help.
Can't you just do
NSURL *url = [NSURL URLWithString:#"file:///path/to/file"];
?
Sure, here is some code from an app that loads a locallly stored html page into a web view:
- (void) viewDidLoad
{
// Load the content into the view
NSString* path = [NSString stringWithFormat: #"%#/index.html",
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex: 0]];
[webView_ loadRequest: [NSURLRequest requestWithURL: [NSURL fileURLWithPath: path]]];
}

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];
}