I have a text field and button and a webview below.
When I enter the string in a text field and press search button, the entire JSON response string is displaying in UIWebview instance.
So my requirement is to display a particular method that matches the string entered in the textfield.
even though i am entering any string in textfield when i press search button its displaying all contents in webview.
is it possible to fetch particular method from responseString ?
Suggest changes that need to be done.
.h file
IBOutlet UITextField *searchtextField;
UIWebView *webView;
}
-(IBAction)search;
#property (nonatomic, retain) UIWebView *webView;
.m file
-(IBAction)search
{
NSString *searchString=searchtextField.text;
NSString *urlAddress = [NSString stringWithFormat:#"http://api.kivaws.org/v1/loans/search.json?status=fundraising",searchString];
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
}
Just Place %# in place of searchString where you want to add...This is all about from your end because I dont know where you want to add
Example
NSString *searchString=searchtextField.text;
NSString *urlAddress = [NSString stringWithFormat:#"http://api.kivaws.org/v1/loans/search.json?status=%#",searchString];
Try It out....
Related
My application has two views and I want to invoke a google search on Second view with a search string (it is name of a product. For example 'Samsung Mobile') passed from application's Main view.
I dont want to enter product name in Google search field manually. It should be done automatically when I press the button on the Main View and the result page should be displayed on Sub View.
-(void) setLabelText:(NSString *) myNewText
{
[productName setText:myNewText];
NSURL *theURL =[NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *theRequest = [NSURLRequest requestWithURL:theURL];
[webSearchView loadRequest:theRequest];
}
Just wondering is it possible to pass the search string as a parameter with the above function.
Try this:
NSString *urlString = [NSString stringWithFormat:#"http://google.com?q=%#", searchString];
NSURL *theURL =[NSURL URLWithString:urlString];
Don't forget to escape urlString before passing it to NSURL if it contains spaces, special characters, etc.
As per H2CO3's answer, but I think the Google search URL may have changed. I also prefer https over http:
NSString *urlString = [NSString stringWithFormat:#"https://google.com/search?q=%#", searchString];
NSURL *theURL =[NSURL URLWithString:urlString];
I put some text into the textField like: "Peter.at", and the UIwebview shows all correct (Mag. pharm. Peter MÜLLER)!
The Problem is when I type in the UItextField: "Püter.at", the UIwebView does nothing!
What is wrong? I think it is the "ü" but I don't know how to fix it!
Yep. I see what the problem is.
Your code should look like this:
NSString *preURL = #"";
NSString *middleURL = #".at";
NSString *fullURLString =
[[NSString stringWithFormat:#"%#%#%#",preURL,1.text,middleURL] stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
NSURL * preparedURL = [[NSURL URLWithString:fullURLString];
[uiwebnamesearch loadRequest: [NSURLRequest requestWithURL: preparedURL]];
[uiwebnamesearch reloadInputViews];
The important change here is that you are "percent escaping" the string, to make it possible to use in a URL.
Here is the documentation for that [NSString stringByAddingPercentEscapesUsingEncoding:] method.
I have a UIWebView and UITextField in my view but am facing a problem.
The problem is how do I replace the URL of the web page in UITextField automatically, as I surf through different web pages?
Use the following property of the UIWebView instance:
#property(nonatomic, readonly, retain) NSURLRequest *request
So you can get the current NSURLRequest associated with the web view by:
NSURLRequest* urlRequest = myWebView.request;
You can then get the associated instance of NSURL using the following instance method:
- (NSURL *)URL
So you can now get the NSURL object form the UIWebView with the following code:
NSURL* url = [myWebView.request URL];
To convert this to a string you can use either the absoluteString or relativeString instance methods of NSURL. For this example I'll use absoluteString.
NSString* urlString = [[myWebView.request URL] absoluteString];
Now we can set the the text of the UITextField using the simple text property:
myTextField.text = [[myWebView.request URL] absoluteString];
So now all we need to do is ensure that the previous line of code gets called at the correct time in order to update the text field with the url text whenever the page changes. To do this, we need to use a class which is a delegate of the UIWebView (and thus conforms to the UIWebViewDelegate protocol). If you're unsure about how to use delegates I strongly suggest you do some reading of the delegate design patterns and Apple Developer documentation, as it's used throughout iOS development.
In your UIWebViewDelegate class, you need to implement the following method as follows:
- (void)webViewDidStartLoad:(UIWebView *)webView
{
myTextField.text = [[myWebView.request URL] absoluteString];
}
This will ensure the text field always displays the url of the page that last started to load (there are other UIWebViewDelegate methods if you'd rather the text field be updated at a different time).
Hope this helps.
In .H file u declare webview, textfield and IbAction to browse the text u enter and it looks like
{
IBOutlet UIWebView *web;
IBOutlet UITextField *text;
}
-(IBAction)search:(id)sender;
and .M file looks in IBaction
-(IBAction)search:(id)sender{
NSLog(#"%#",text.text);
NSString *urlAddress = [NSString stringWithFormat:#"http://%#",text.text];
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[web loadRequest:requestObj];
}
surely this works
You should define – webViewDidStartLoad: method of id delegate of your UIWebView:
- (void)webViewDidStartLoad:(UIWebView *)webView
{
yourUITextField.text = [[webView.request URL] path];
}
All from documentation
There is a method - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType in the webview delegate.
This method will call for every request load in the web view.
In this you can get the request Object. From that you can get the URL.
You can directly paste the URL in the textField in this method.
I have trouble accomplishing something that sounds very simple. Using Interface Builder, I created a UIWebView object. Next, I created a new controller - HomeViewController and assigned it to the WebView under "NIB Name" property.
Next I need to load a web site (let's say http://google.com) into that WebView when user accesses that view. I am not able to find any examples on how to actually accomplish this. Please provide a code example. Sorry about the newbie question. I did RTFM, but no luck.
The .h file:
#interface WebViewController : UIViewController {
IBOutlet UIWebView *webView;
}
#property (nonatomic, retain) UIWebView *webView;
#end
The .m file:
#import "WebViewController.h"
#implementation WebViewController
// Loads the url when the view loads
- (void)viewDidLoad {
NSString *urlAddress = #"http://www.google.com";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Request Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[webView loadRequest:requestObj];
}
#end
try this :
[webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://google.com"]]]; // webview - your Uiwebview object
I am wondering if anyone can advise on the best way to add a FAQ page in iphone app. I would like to display a 'local' file (perhaps HTML) with local images into a web view. Essentialy to enable users access to FAQ within the app.
Any advice on whether to use HTML or any other way of doing this will be very helpful..
Thanks in advance..
mb
I used a UIWebView and loaded a local index.html file into this.
**HelpViewController.h**
#interface HelpViewController : UIViewController {
UIWebView *webView;
}
#property (retain) IBOutlet UIWebView *webView;
#end
Obviously you will need to #sythesize webView.
**HelpViewController.m**
- (void)viewDidLoad
{
self.title = #"Help";
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Close" style:UIBarButtonItemStyleDone target:self action:#selector(btnReturn)];;
NSString *helpPath = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSURL *url = [NSURL fileURLWithPath:helpPath isDirectory:NO];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
NSLog(#"Help loaded");
}
Hope this helps. Also, just a note, if you are presenting this as a modal, it will appear blank for a split second until the HTML file loads into the UIWebView. I got around this using:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[self performSelector:#selector(showAboutWebView) withObject:nil];
}
- (void)showAboutWebView {
[webView setHidden:NO];
}