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];
}
Related
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];
I have to parse the return of a JSON API for which I've taken the URL into a string and after that I passed it NSURL but in NSLog it is showing nil.
Here is my code,
NSString *urlstring=[NSString stringWithFormat:#"http://api.v3.factual.com/t/places?q=%#&geo={\"$circle\":{\"$center\":[19.9909631,73.8034808],\"$meters\":40000}}&limit=20&KEY=123456",[self.strurl lowercaseString]];
NSURL *url=[[NSURL alloc]initWithString:urlstring ];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
Thanks in advance.
You must encode your url as it contains special characters, Try something like this
NSString *paramString=[NSString stringWithFormat:#"/t/places?q=%#&geo={\"$circle\":{\"$center\":[19.9909631,73.8034808],\"$meters\":40000}}&limit=20&KEY=123456",[self.strurl lowercaseString]];
NSString *encodedSearchString = [paramString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *urlString = [NSString stringWithFormat:#"http://api.v3.factual.com%#", encodedSearchString];
NSURL *url = [NSURL URLWithString:urlString];
NSLog(#"urlstring is %#",url)
Hope this helps
Try this, Tested and Working
-(void) sample
{
// NSString *strurl = #"hello";
NSString *urlstring=[NSString stringWithFormat:#"http://api.v3.factual.com/t/places?q=%#&geo={\"$circle\":{\"$center\":[19.9909631,73.8034808],\"$meters\":40000}}&limit=20&KEY=123456",[strurl lowercaseString]];
NSURL *url=[NSURL URLWithString:[urlstring stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
NSLog(#"%#",req);
}
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.
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.
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.