Error in code: two ViewController with PDF file… - ios

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

Related

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

How do I display multiple UIWebViews on a single UIViewController with local files?

I'm trying to show multiple UIWebView's on a single UIViewController, each should show a single HTML file.
In my camView.h file, I've got the following code:
#interface camView : UIViewController
#property (weak, nonatomic) IBOutlet UIWebView *cam01_valk01;
#property (weak, nonatomic) IBOutlet UIWebView *cam02_valk02;
#property (weak, nonatomic) IBOutlet UIWebView *cam03_valk03;
#property (weak, nonatomic) IBOutlet UIWebView *cam04_tunnel;
and in my camView.m file:
#interface camView ()
#end
#implementation camView
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSString *path = [[NSBundle mainBundle] pathForResource:#"cam1_valk01" ofType:#"html"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_cam01_valk01 loadRequest:request];
NSString [[[NSBundle mainBundle] pathForResource:#"cam2_valk02" ofType:#"html"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_cam02_valk02 loadRequest:request];
NSString *path = [[NSBundle mainBundle] pathForResource:#"cam3_valk03" ofType:#"html"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_cam03_valk03 loadRequest:request];
NSString *path = [[NSBundle mainBundle] pathForResource:#"cam4_tunnel" ofType:#"html"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_cam04_tunnel loadRequest:request];
}
Now the issue is that the first UIWebview is displaying the HTML content, but all the next ones are getting the error message
"redefinition of 'path'"
"redefinition of 'url'"
"redefinition of 'request'"
I can also clearly see that I'm defining the same thing 4 times, but removing those definitions will completely wreck the code. My experience with xcode is very low, so I don't actually know how to solve it. Please write in depth what the correct solution to showing multiple UIWebview's is.
#interface camView ()
#end
#implementation camView
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSString *path = [[NSBundle mainBundle] pathForResource:#"cam1_valk01" ofType:#"html"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_cam01_valk01 loadRequest:request];
NSString * pathSec = [[NSBundle mainBundle] pathForResource:#"cam2_valk02" ofType:#"html"];
NSURL *urlSecond = [NSURL fileURLWithPath:pathSec];
NSURLRequest *requestSecond = [NSURLRequest urlSecond];
[_cam02_valk02 loadRequest:requestSecond];
NSString *pathThird = [[NSBundle mainBundle] pathForResource:#"cam3_valk03" ofType:#"html"];
NSURL *urlThird = [NSURL fileURLWithPath:pathThird];
NSURLRequest *requestThird = [NSURLRequest requestWithURL:urlThird];
[_cam03_valk03 loadRequest:requestThird];
NSString *pathFour = [[NSBundle mainBundle] pathForResource:#"cam4_tunnel" ofType:#"html"];
NSURL *urlFour = [NSURL fileURLWithPath:pathFour];
NSURLRequest *requestFour = [NSURLRequest requestWithURL:urlFour];
[_cam04_tunnel loadRequest:requestFour];
}

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.

UIwebView - load the URL it was left with upon dismissView

I have the following code that allows me to load a set URL each time, placed in my viewDidLoad method:
NSString *urlAddress = #"http://google.com";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_webView loadRequest:requestObj];
Now the user can of course go to any webpage from here and if the user decides to dismiss the view I want for them to be able to return to it.
Now I have this code, through reading various forums and searches:
again the viewDidLoad - now looks like this:
NSURLRequest *currentRequest = [_webView request];
NSURL *currentURL = [currentRequest URL];
if(currentURL != nil)
{
NSString *urlAddress = currentRequest;
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_webView loadRequest:requestObj];
}
else {
NSString *urlAddress = #"http://google.com";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_webView loadRequest:requestObj];
}
and in my viewDidUnLoad I have this:
-(void)viewDidUnload {
NSURLRequest *currentRequest = [_webView request];
NSURL *currentURL = [currentRequest URL];
NSLog(#"Current URL is %#", currentURL.absoluteString);
[NSURLConnection connectionWithRequest:currentRequest delegate:self];
}
a) Am I on the right track for achieving this?
b) I have a warning created on this line : `NSString *urlAddress = currentRequest;
which is:
Incompatible pointer types initializing 'NSString *__strong' with an expression of type 'NSURLRequest *__strong'
Any help is appreciated:-)
Thank you:-)
PS I started this problem out in this question, but felt it was worth a new one, so forgive me if you do not think the same.
`
NSString *urlAddress = currentRequest;
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_webView loadRequest:requestObj];
Could just be
[_webView loadRequest:currentRequest];
You might want to experiment with not unloading the view/view controller when the user dismisses it then there won't be a time delay when the user brings it back. (Maybe you could instead make it hidden instead of unloading it for example when the user dismisses it, or send it to the back of the navigation controller stack, assuming you have one as the root view controller).
P.S.
Your warning is because you're trying to assign an NSURLRequest object to an NSString object.

UIWebView is crashing when trying to load a local html file

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.

Resources