UIWebView Delegate Methods not Working [closed] - iphone

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
**When I Use self.myWeview.delegate = self ,
UIWebView can not load URL in UIWebView...
But If I set it to self.myWeview.delegate = nil,
then methods(delegate) can't load but URL is load
this is the code:---
{
self.myWeview.delegate = nil;
NSURL *url = [NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[myWebView loadRequest:request];
}
This is working fine but couldn't called delegate methods.
On the other hand
{
self.myWeview.delegate = self;
NSURL *url = [NSURL URLWithString:#"http:http://www.google.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[myWebView loadRequest:request];
}
It Couldn't load URL but calls the delegate methods

Finally I got my answer by lot of experiment,if we not using this method
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
except that all methods are working fine in UIwebViewDelegate
1. -(void)webViewDidFinishLoad:(UIWebView *)webView
2. -(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
3. -(void)webViewDidStartLoad:(UIWebView *)webView

remove that extra http
{
self.myWeview.delegate = self;
NSURL *url = [NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[myWebView loadRequest:request];
}

You have made the mistake in calling the URL ...
Try this :
NSURL *url = [NSURL URLWithString:#"http://www.google.com"];

Use this code in your viewcontroller.h file
#interface ViewController : UIViewController<UIWebViewDelegate>

set delegate to self then you need to implement the delegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
return YES;
}
and also don't forget to confirm the protocol in .h file like this
#interface YourViewController : UIViewController<UIWebViewDelegate>

Related

IOS - UIWebview - Get the URL of the link clicked and edit the url

I'm creating an app which has a webview. When user click to a link or a button in the webview, I want to be able to get the new url, and edit the new url.
First I initialize the webview with the following url:
- (void)viewDidLoad
{
NSString *id = #"12212323";
[super viewDidLoad];
NSString *fullURL = [[NSString alloc] initWithFormat:#"www.website.com/index.php?id=%#", id];
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
}
When I click to a link, for example www.website.com/contact.php
Then I edit the new url with www.website.com/contact.php*?id=12212323*
I need to keep the id parameter in all the urls.
The UIWebViewDelegateProtocol should suit your requirements.
In particular the method - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
this is a code to get the current URL
- (void)viewDidLoad
{
NSString *id = #"12212323";
[super viewDidLoad];
NSString *fullURL = [[NSString alloc] initWithFormat:#"www.website.com/index.php?id=%#", id];
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
NSURL *currentURL = [requestObj URL];
NSLog(#"Current URL is %#", currentURL.absoluteString);
}
but to create a URL bar hidden or not is a lot of code, you can start from this tutorial on YouTube is simple and work very well, is possible give you other idea to make a good job ;)
Web Browser Tutorial
hope this help you

Get updated url from uiwebview

I have a uiwebview I open http://google.com in it. Then I type something in google search bar and search for something. I want to get that updated url that is generated as a result of my search on google.
- (void)viewDidLoad
{
[super viewDidLoad];
[self setUrlAddress:#"http://google.com.pk"];
// Do any additional setup after loading the view from its nib.
NSURL *url = [NSURL URLWithString:self.urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[self.webView setScalesPageToFit:YES];
[self.webView loadRequest:requestObj];
}
- (void)webViewDidStartLoad:(UIWebView *)webView{
[appDelegate showActivityView:self.view];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
[appDelegate hideActivityView];
}
Try this
NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:#"window.location.href"];
Hope it will work for you
Below is a correct answer but its not perfectly working for Facebook and youtube
NSSTRing * currentURL = webView.request.URL.absoluteString;
If anyone knows a better answer, please share.
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *urlAddress = [NSString stringWithFormat:#"http://www.google.com/pk];
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[webView loadRequest:requestObj];
}
try this delegate method :
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
you can get hold of request and nslog that request .

how to call two url in one webview in iphone

i want to call two url in one webview one after the another but how i know i can load one url in webview but to call two url please help me one this how to timeinterval on both the url
this is mu single url if but i have to give here two url the how to give and call them
- (void)viewDidLoad {
[super viewDidLoad];
CheapFlightsAppDelegate* app=(CheapFlightsAppDelegate*)[[UIApplication sharedApplication]delegate];
self.navigationController.navigationBarHidden = true;
NSLog(#"Book: %#",app.selectedOutgoingFlight.FlightKey);
NSLog(#"Book: %#",app.selectedInComingFlight.FlightKey);
NSString *hel = #"http://www.bookryanair.com/SkySales/FRSelect.aspx?AvailabilityInputFRSelectView %24ButtonSubmit=Select%20and%20Continue&AvailabilityInputFRSelectView%24m arket1=";
NSString *hel1 =[NSString stringWithFormat:#"%#&",app.selectedOutgoingFlight.FlightKey];
NSString *hel2 = #"AvailabilityInputFRSelectView%24market2=";
NSString *hel3 = [NSString stringWithFormat:#"%#&",app.selectedInComingFlight.FlightKey];
NSString *hel4 = #"__EVENTARGUMENT=&__EVENTTARGET=";
NSString *urlAddress = [NSString stringWithFormat:#"%#%#%#%#%#",hel,hel1,hel2,hel3,hel4];
[spinner startAnimating];
spinner.hidden = NO;
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
NSLog(#"Book: %#",urlAddress);
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[webView loadRequest:requestObj];
webView.delegate = self;
}
use this method
NSURLRequest *request=[[NSURLRequest alloc] initWithURL:[[NSURL alloc] initWithString:#"http://stackoverflow.com/questions/7670889/how-to-call-two-url-in-one-webview-in-iphone/7670992#7670992"]];
[webView loadRequest:request];
[self performSelector:#selector(loadAnother:) withObject:nil afterDelay:10];
-(IBAction)loadAnother:(id)sender{
NSURLRequest *requests=[[NSURLRequest alloc] initWithURL:[[NSURL alloc] initWithString:#"http://stackoverflow.com/questions/7683196/uitableview-with-text-that-is-both-right-aligned-and-indented"]];
[webView loadRequest:requests];
}
Set new url of NSURLRequest and call [webView loadRequest:requestObj]; after any time interval you want again.
NSTimer will help you to call method after some interval.
// after loading first url:
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:60 target:self selector:#selector(loadNewUrl:) userInfo:nil repeats:NO];
//somewhere in delegate define selector
- (IBAction)loadNewUrl:(NSTimer *)timer
{
NSURL *url = [NSURL URLWithString:#"http://www.apple.com/stevejobs"];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[webView loadRequest:requestObj];
}
UPDATED:
Implement in your `` methods to detect when webview finished loading first url:
- (void)webViewDidFinishLoad:(UIWebView *)webView;
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error;
In "webViewDidFinishLoad" delegate method get the current URL thats loaded using
currentURL = webView.request.URL.absoluteString;
Then check if it matches with your first URL and if it does, Load the second URL that you need to call. If you want, call this second URL after some time using a timer.

uiwebview not loading the web page

in my program uiwebview is not loading the url address.when i nslogged the request object is not null.however when i nslog the webview.request it returns null.what may be the reason it is not loading
- (void)viewDidLoad {
[super viewDidLoad];
self.web = [[UIWebView alloc] init];
NSString *urlAddress = #"http://www.google.com";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
NSLog(#"%#",requestObj);
[self.web loadRequest:requestObj];
[web setFrame:CGRectMake(0, 0, 320, 370)];
NSLog(#"%# %#",self.web,self.web.request);
}
the nslog results are
<NSURLRequest http://www.google.com>
<UIWebView: 0xbb17ff0; frame = (0 0; 320 370); layer = <CALayer: 0xbb12f20>> (null)
as per suggestions from this site i changed it from IB and made it to code .i made the class confirm to uiwebview delegate..both the webviewdidstart and webview did finish are being called these are the nslog outputs from these methods
webviewdidstart
webView-----><NSMutableURLRequest >
webView-----><NSMutableURLRequest http://www.google.com>
webview did finish
webView-finished----><NSMutableURLRequest http://www.google.com>
webView-finished----><NSMutableURLRequest http://www.google.com>
still nothing is being called and i think both these methods are called twice
This is a working code, test this out in your application and let us know the outcome
in .h file, add UIWebViewDelegate
in .m file's viewDidLoad, add this:
UIWebView *webview=[[UIWebView alloc] initWithFrame:CGRectMake(0.0,0.0,320.0,460.0)];
NSURL *url = [NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
webview.delegate = self;
[webview loadRequest:requestObj];
[self.view addSubview:webview];
Now check in the delegates if your page is loading or not:
- (void)webViewDidStartLoad:(UIWebView *)webView {
NSLog(#"page is loading");
}
-(void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(#"finished loading");
}
try like this
CGRect webFrame = CGRectMake(0.0, 0.0, 320.0, 460.0);
UIWebView *webView = [[UIWebView alloc] initWithFrame:webFrame];
NSString *urlAddress = #"http://www.google.com";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
[self addSubview:webView];
[webView release];

url not loading in UIWebView

i have a tab bar control. 1st tab contains a navigation control. On the 2nd tab i want to load a web page (say google.com). I have written the code as
NPIViewController.h
#interface NPIViewController : UIViewController {
IBOutlet UIWebView *webView;
}
#property (nonatomic,retain) IBOutlet UIWebView *webView;
#end
NPIViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView setScalesPageToFit:YES];
[self.webView loadRequest:request];
}
The page just does not load. No compilation or runtime errors. What is wrong in this ?
to know whats wrong you can do this.
- (void)viewDidLoad {
[super viewDidLoad];
webView.delegate = self;
NSURL *url = [NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView setScalesPageToFit:YES];
[self.webView loadRequest:request];
}
add this new method in your class
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
NSLog(#"Error : %#",error);
}
I hope you have connected webView object with its outlet in Interface builder?
Thanks,
You have to use https instead of http. If you use http you will get this error message (use Ravin's code to see the error):
"The resource could not be loaded because the App Transport Security policy requires the use of a secure connection."
Create you webView
IBOutlet UIWebView *webView;
Try this code
NSString *urlAddress = #"http://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.
[webView loadRequest:requestObj];
Restart your IOS simulator.
It is really not evident, but at first check some site in Safari at IOS simulator.
After restart of IOS simulator, my webView opened successfully both at simulator and device.
See this famous link.