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
Related
I'm trying to load a UIWebView, but I can't understand why doesn't work, I'm doing this:
Loading *load = [[Loading alloc] init];
[load searchName];
then Loading.h
#interface Loading : NSObject <UIWebViewDelegate>
{
UIWebView *myWebView;
}
- (void) searchName;
Loading.m
- (void) searchName{
myWebView = [[UIWebView alloc] init];
myWebView.delegate = self;
NSString *urlAddress = #"www.google.com";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[myWebView loadRequest:requestObj];
}
- (void)webViewDidFinishLoad:(UIWebView *)thisWebView
{
NSString *yourHTMLSourceCodeString = [myWebView stringByEvaluatingJavaScriptFromString:#"document.body.innerHTML"];
NSLog(#"%#",yourHTMLSourceCodeString);
}
why never call webViewDidFinishLoad method?
The reason your webview is not displayed is you have taken NSObject class and NSObject classes dont have an XIB and the way you have done is just calling your -(void)searchName method and is not adding it to self.view.
Did you created this webview with IB? Did you connected the delegate to the webview?
Try to catch errors with webView:didFailLoadWithError: (post them if there are errors)
Try to set the delegate with code [myWebView setDelegate:self]; or self.myWebView.delegate = self;
Check output of delegate if nothing happend:
self.myWebView.delegate = self;
NSLog(#"%#",self);
Yes, take note into what Nathan said. I am just expanding.
If you are using IB, connect the outlet. Then use [myWebView setDelegate:self];
Be sure to use http://. Http on it's own won't do anything.
Can you get other URL's to load?
Finally, I have an open source basic web browser for Cocoa that you can take a look at.
Basic Web Browser
I am building an app that would run a website through my app. Basically the webView. I had no problems so far since the last xCode update. It basically runs with no errors but it does not work on iOs 6 simulator or device.
.h fle:
#interface ViewController : UIViewController{
IBOutlet UIWebView *nView;
}
.m file:
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL * myURL = [NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:myURL];
[nView loadRequest:myRequest];
}
If anyone can help I would be grateful. Thank you
I managed to get it working!
.h file:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (weak, nonatomic) IBOutlet UIWebView *myWebView;
#end
.m file:
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *url =[NSURL URLWithString:#"http://www.google.com"];
NSURL*request = [NSURLRequest requestWithURL:url];
[[self myWebView]loadRequest:request];
}
I included a property on h file and with some research I got the .m file working too. Although I got an issue saying:
Incompatible pointer types sending 'NSURL *__strong' to parameter of
type 'NSURLRequest *'
Can anybody help to get rid of this issue as well please?
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....
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 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];
}