UIWebView is crashing when trying to load a local html file - ios

The code I am using in my view controller viewDidLoad method:
NSString *path = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"html"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest* request = [[NSURLRequest requestWithURL:url] autorelease];
[webView loadRequest:request];
I get EXE_BAD_ACCESS

requestWithURL: will give you an autoreleased object so you don't need to autorelease it again.

Related

Can we load the terms and conditions as a PDF in a UIWebview for IOS apps?

I am adding terms and conditions to my app in a UIWebview. What i really want to know is can i show it as a page by page pdf document or should i use any other method? Will App store accept the pdf format?
Yes, this can be done using UIWebview and surely will get accepted by Apple.
If you are trying to display a PDF file from a web URL , use the below code.
NSURL *targetURL = [NSURL URLWithString:#"http://yourdomain.com/file_toopen.pdf"];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
Or if you have a PDF file bundled with in your application, use below code.
NSString *path = [[NSBundle mainBundle] pathForResource:#"filetoopen" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
Apple Surely approve , in here you can implement in two ways
UIWebview
NSString *pathofFile = [[NSBundle mainBundle] pathForResource:#"sample" ofType:#"pdf"];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:pathofFile]];
[webView loadRequest:request];
UIDocumentInteractionController preferable for PDF
NSURL *URL = [[NSBundle mainBundle] URLForResource:#"sample" withExtension:#"pdf"];
if (URL) {
// Initialize Document Interaction Controller
self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:URL];
// Configure Document Interaction Controller
[self.documentInteractionController setDelegate:self];
// Preview PDF
[self.documentInteractionController presentPreviewAnimated:YES];
}
Sample Tutorial

Parameter in loadWebView

In my app i have this function:
- (void)loadWebView {
NSBundle *bundle=[NSBundle mainBundle];
NSString *filePath = [bundle pathForResource:#"namefile" ofType: #"html"];
NSURL *fileUrl = [NSURL fileURLWithPath:filePath];
NSURLRequest *request = [NSURLRequest requestWithURL:fileUrl];
[wv loadRequest:request];
}
which loads an HTML page in UIWebView. Is possible to pass a parameter in the call ?
You can try something like this:
-(void)loadWebView
{
NSURL *relativeURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
NSURL *URL = [NSURL URLWithString:#"namefile.html?parameter=10" relativeToURL:relativeURL];
//If your HTML file is in a subfolder, add it to the string like 'HTML/namefile.html?parameter=10
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
[wv loadRequest:request];
}

Error in code: two ViewController with PDF fileā€¦

I have a ViewController with a PDF file inside it. Now, I have another ViewController and want to integrate a PDF file, too.
But I get some errors. This is the code:
}
- (void)viewDidLoad {
[self refresh:self];
NSString *path = [[NSBundle mainBundle] pathForResource:#"Plan" ofType:#"pdf"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_ImageInWebView loadRequest:request];
[_ImageInWebView setScalesPageToFit:YES];
NSString *path = [[NSBundle mainBundle] pathForResource:#"Plan" ofType:#"pdf"]; (Error: Redefinition of 'path')
NSURL *url = [NSURL fileURLWithPath:path]; (Error: Redefinition of 'url')
NSURLRequest *request = [NSURLRequest requestWithURL:url]; (Error: Redefinition of 'request')
[_PDFInWebView loadRequest:request];
[_PDFInWebView setScalesPageToFit:YES];
[super viewDidLoad];
_myBotton.layer.borderWidth =2.0f;
_myBotton.layer.borderColor = [[UIColor redColor]CGColor];
}
I wrote the error message I get in brackets behind the code.
What can I do to solve this problem? Please help me.
You're declaring the same variables twice within one method.
Why not change that second block of code to just reuse the already declared variables?
e.g., this code:
NSString *path = [[NSBundle mainBundle] pathForResource:#"Plan" ofType:#"pdf"]; (Error: Redefinition of 'path')
NSURL *url = [NSURL fileURLWithPath:path]; (Error: Redefinition of 'url')
NSURLRequest *request = [NSURLRequest requestWithURL:url]; (Error: Redefinition of 'request')
[_PDFInWebView loadRequest:request];
[_PDFInWebView setScalesPageToFit:YES];
becomes
path = [[NSBundle mainBundle] pathForResource:#"Plan" ofType:#"pdf"];
url = [NSURL fileURLWithPath:path];
request = [NSURLRequest requestWithURL:url];
[_PDFInWebView loadRequest:request];
[_PDFInWebView setScalesPageToFit:YES];

UIWebView not loading local webpage

I have a super simple webpage that I just want to load a single local file. The file is /Resources/About.html
Here is my code:
NSString *urlPath = [[NSBundle mainBundle] URLForResource:#"About" withExtension:#"html"];
NSURL *url = [NSURL fileURLWithPath:urlPath];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView loadRequest:urlRequest];
What's wrong?
The method URLForResource:withExtension returns an NSURL, not an NSString. You can use the result of that directly in your request.
NSURL *URL = [[NSBundle mainBundle] URLForResource:#"About" withExtension:#"html"];
[webView loadRequest:[NSURLRequest requestWithURL:URL]];
NSString * urlPath = [[NSBundle mainBundle] pathForResource:#"About" ofType:#"html"];
NSString *content = [NSString urlPath encoding:NSUTF8StringEncoding error:nil];
[webView content baseURL:[NSURL urlPath]];
I bet URLForResource is returning nil (Can you confirm that it is not?). It will not perform a recursive search, and will fail if your "Resources" folder is an actual folder (blue in XCode) as opposed to a group (yellow in Xcode). Try using the main bundle's resourceURL and appending your path to the end of it.

NSURLCache don't support file://?

I would like to load [NSURL fileURLWithPath:#"2.html"] in UIWebView use NSURLCache, the cachedResponseForRequest: function is executed and return a NSCachedURLResponse object which is performed correctly at http:// scheme.
But The UIWebView load failed finally. Did I miss some optional?
I want to use UIWebView load html file in a compressed file, and I do not expect to uncompress the files to disk. is there any suggestion.
Do this:
NSURL *url = [[NSBundle mainBundle] URLForResource:#"2" withExtension:#"html"];
NSString *html = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSURL *baseUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
[webView loadHTMLString:html baseURL:baseUrl];

Resources