load UIWebView when clicking a button - ios

-(IBAction)loadWebView
{
NSString * url = [NSString stringWithFormat:#"%#", [_urlArray objectAtIndex:_urlInt] ];
NSLog(#"_urlInt: %d",_urlInt);
NSLog(#"get url: %#",url);
//[self.webView loadRequest:[NSURLRequest requestWithURL:[ NSURL URLWithString: url ] ] ];
[self.webView loadRequest:[NSURLRequest requestWithURL:[ NSURL URLWithString:#"http://www.google.com"]]];
}
The above is my code in an iPad project.
It triggers this method when clicking a button on the same View Controller or click a row of a table on different View Controller.
Values of _urlArray and _urlInt are from the table when I click a row of the table. I can get the url in this method but I cannot relaod the UIWebView. However, when I modify the url to the home page of google and I click the button, the page can be reload to google.com.
I cannot get the reason. Could someone teach me that?
Thanks...

Related

if I clicked IBAction login button in login form,go to web view in ios

If I pressed login action button
- (IBAction)login button:(id)sender
I would like to go webview without using triggered segues.
Because I have two web views.If I pressed login button,I would like to go login webview, else I would like to go home webview. It depends on one button click.
I used delegate protocol and create two instance method and then,I would like to call method by using [self method name].delegate is okay but login button action didn't go to webview.
How can I do that?
piece of code in viewcontroller.m
- (void)viewDidLoad {
[super viewDidLoad];
}
-(void)loginCompleted{
NSString *fullURL = #"http://www.example.com/login";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[homeWebView loadRequest:requestObj];
}
-(void)homeCompleted{
NSString *fullURL = #"http://www.example.com/home";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[homeWebView loadRequest:requestObj];
}
- (IBAction)loginEvent:(id)sender {
if ([sender isEqual:loginbtn]) {
[self loginCompleted];
NSLog(#"Button pressed");
} else {
NSLog(#"Button pressed2"); }
}
Where is your loginbtn? How you set it?
set it's tag
loginbtn.tag = 1
In loginEvent compare the tag like this
if (sender.tag == 1) {
[self loginCompleted];
NSLog(#"Button pressed");
} else {
NSLog(#"Button pressed2");
}
You can select and deselect your button depending on what action you want to trigger: loginbtn.isSelected=true. And then:
- (IBAction)loginEvent:(id)sender {
if ([sender isSelected]) {
[self loginCompleted];
NSLog(#"Button pressed");
} else {
NSLog(#"Button pressed2");
}
}
(I write in Swift so it might be written a bit differently)
What you say 'If I pressed login button,I would like to go login web view,else I would like to go home web view.It depends on one button click.' causes me confused.What are the 'else cases' ?
I guest you are hoping to load the home page first,if the user press login button,it goes to the login page.
If it's true,you should add requestObj to the view controller's property,cancel the request if needed when the user press login button.
You say you have two webViews,but you load login request to the homeWebView see loginCompleted method

Obj C UIWebview go button click action

Here I'm loading one uiwebview in coding and show one pdf file successfully. In that pdf file have one Textfield and Go button. when i click the textfield means the keyboard will open will go button in down. Show in screen shot. I need that uiwebveiw that loaded pdf textfield keyboard go button action. help me. thanks advance
[textField setReturnKeyType:UIReturnKeyGo];
- (BOOL) textFieldShouldReturn:(UITextField *)textField
{
NSString *urlAddress = textField.text;
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *aRequest = [NSURLRequest requestWithURL:aURL];
[WebView loadRequest:aRequest];
}

iOS how to know if the html5 button was clicked loaded on an UIWebView

I have an iOS app with an UIWebView, and I need to receive a href url from server side and then present the web page which is a HTML5 from that href in my UIWebView:
MyViewController *myVC = [[MyViewController alloc] init];
myVC.url = serverSideResponse.href;
[self.navigationController presentViewController:myVC animated:YES completion:nil];
In the MyViewController.m, I have this:
_webView = [[UIWebView alloc] initWithFrame:myFrame];
[self.view addSubView:_webView];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
[_webView loadRequest:request];
So the web page is from server side and I want my iOS app/webview to know if the button was clicked on that page. To be more specific, if that button was clicked, then I shut my webview (meaning removeFromSuperView).
Now what I can see is that when I check the source code of that web page, I know the button id.
The url I received was: https://api.uber.com/v1/surge-confirmations/edbfdd9a-276b-4b85-b3f4-5a0c948a17fe. And when I was loading it in my web view, it has a button and when I clicked it nothing happened, only console logged the click event and the request was: https://api.uber.com/v1/surge-confirmations/edbfdd9a-276b-4b85-b3f4-5a0c948a17fe# <--there comes an extra #.
But when I put the url in my desktop browser to present it and click the button, it can redirect to the next url (https://www.uber.com) as expected.
Please advise, thanks!
Thanks to #Wain and #palme, I found one of the UIWebView delegate callback methods
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
can do this.
Parameter request and navigationType can tell you what navigationType triggered what request.

Loading url link in the another instance of same ViewController

I have a ViewController with uiwebvew. This is called to show the webcontents.
My problem is , if the webcontent has links, I want to show that link page in the another instance of same ViewController.
So far I'm unable to do so. I guess I'm missing something here.
Could anyone point my problem here.
Thanks in advance.
-(BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
PageDetailsViewController *pdvc = [self.storyboard instantiateViewControllerWithIdentifier:#"PageDetails"];
NSURL *websiteUrl = [NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:websiteUrl];
[pdvc.webview loadRequest:urlRequest];
[self.navigationController pushViewController:pdvc animated:YES];
return NO;
}
return YES;
When I try to load in same viewcontroller, it's fine it loads. So, my guess is , there may be something wrong with my webview delegate or navigationcontroller.
Any ideas??
pdvc.webview will be nil because its view has not been loaded at the time you're trying to reference it. You should pass the URL to pdvc, and let that controller load its web view in viewDidLoad.

iPad SplitView detailView default load

I have an iPad split-view app. The detailview is a webview. In which method, can I get a default page loaded when the app launch before the user clicks on an item in the table view?
IN detailviewcotroller.h , create outlet as follows,
IBOutlet UIWebView *WEBVIEW;
connect outlet in xib by drag n droping uiwebview control.
In your detailviewcontroller.m's viewdidload method u can write as follows-
NSString *str=#"yr default webpage link here";
NSURL *url=[NSURL URLWithString:str];
NSURLRequest *req=[NSURLRequest requestWithURL:url];
[WEBVIEW loadRequest:req];
Just a good advice, by creating Outlets, when you synthesize do:
#synthesize webView = _webView;
and use :
[_webView loadRequest:req];
for loading your request.

Resources