Binding webview navigation actions - ios

I have webview and a Go-Back button for webview.
I need two functionality from Go-back button(as in standard browser)
Button should be disabled when it is not possible to go back
When pressed, should go back
Disabling the button is achieved by cocoa binding.
And go back action is done by binding following selector from Owner class
- (IBAction)buttonPressed:(id)sender {
[_webView goBack];
}
Since the buttonPressed: selector is only calling goBack selector, can I bind self.webView.goback to the button, eliminating the need of buttonPressed selector in owner class.

You can use following code Disabling your button
- (void)webViewDidFinishLoad:(UIWebView *)webView;{
if ([webView canGoBack]) {
_button.enabled = NO;
}else{
_button.enabled = YES;
}
}

Related

How do I detect that I'm on the initial webView page?

I have a webView in my app that works well. In order to enable the user to go back to the previous url link I have a custom back button in my navigationBar. Linked to that button is a simple code line that takes the user back to the previous screen:
-(void)webBackButton {
NSLog(#"BACK BACK BACK");
[_webView goBack];
}
Works great. Now the real issue here is that there obviously shouldn't be a back button in the initial webView (there's no place to go back to). So, I would like to hide and disable the button on the first webView shown. For that to happen I guess I need to somehow check to see that the current webView is not the initial webView. Like this:
if(!_webView.URL.absoluteURL) {
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:self action:#selector(webBackButton)];
}
And I suppose this check has to be done every time the ´webViewchange itsurlpath; that is when the user uses alink. If the currentwebViewis_webView.URL.absoluteURLthe backbutton` should be hidden, otherwise it should be shown.
How do I accomplish this. As far as I understand, the delegate webViewDidFinishLoad is deprecated. I have tried using observeValueForKeyPath but it doesn't seem to trigger when I go to another url. Is there an easier way to hide the back button on the initial screen?
UIWebview Class
open var canGoBack: Bool { get }
WKWebView Class
/** #abstract A Boolean value indicating whether there is a back item in
the back-forward list that can be navigated to.
#discussion #link WKWebView #/link is key-value observing (KVO) compliant
for this property.
#seealso backForwardList.
*/
open var canGoBack: Bool { get }
Add a check on webBackButton Method :
if([self.webView canGoBack]) {
[backBtn setEnabled:YES];
} else {
[backBtn setEnabled:NO];
}
Sorry for mixing up Swift & Objective C, the pseudocode remains the same.

Copying UIAlertController button behaviour

I am creating custom dialogs for my app and some what copying UIAlertController in some aspects. How should I implement the behaviour where when you click any action from alert/dialog the controller is dismissed.
How does Apple do it without making us manually specify for each action handler that it should dismiss the view controller?
I have like them one view controller class:
#interface MyAlertViewController : UIViewController
- (void)addAction:(MyAlertAction *) action;
//...
And one class for the actions:
#interface MyAlertAction : NSObject
- (instancetype)initWithTitle:(nullable NSString *)title handler:(void (^)(MyAlertAction *action))handler;
EDIT: How I did it taking in accord the answer feedback:
//MYAlertViewController.m
- (void)viewDidLoad {
for (int i = 0; i < self.actions.count; i++) {
MYAlertAction *action = self.actions[i];
button = [[UIButton alloc] initWithFrame:CGRectZero];
button.tag = i;//this here is how I link the button to the action
[button addTarget:self action:#selector(executeAction:) forControlEvents:UIControlEventTouchUpInside];
[actionStackView addArrangedSubview:button];
[self.actionsStackView addArrangedSubview:actionLayout];
}
}
- (void)executeAction:(UIButton *) sender{
[self dismissViewControllerAnimated:YES completion:^{
//this is where the button tag comes in handy
MYAlertAction *actionToExecute = self.actions[sender.tag];
actionToExecute.actionHandler();
}];
}
How does Apple do it without making us manually specify for each action handler that it should dismiss the view controller?
You are confusing two different things:
The UIAlertAction's last initialization parameter, the handler parameter, which you get to set from outside, and which is to run after the button is tapped and after the alert has been dismissed. It is a block.
The actual button's action, which the client can't set or see. It is configured by the alert controller. It is a selector.
So now, you play the role of the UIAlertController. In your
- (instancetype)initWithTitle:(nullable NSString *)title handler:(void (^)(MyAlertAction *action))handler;
the client hands you the first action I mentioned, the block, and you store it for later execution. But the second action, the button action, the selector, is entirely up to you as you create the button in response to this call.
So as you configure the button, just configure it with a target/action pair that calls into a method of your view controller, just as for any button. In method, when called, the view controller dismisses itself, and in the completion handler of the dismissal, calls the block.

How to set the custom previous and next button for my UIWebView in iOS

i have create a custom UIWebView for my project.now i load one website that UIWebView and also create two button like previous page and next page on top of navigation bar. i want to know how to handle the UIWebView pages,when i click the previous or next button.Those buttons handle the UIWebView pages.i need exact code for the those previous and next buttons.could any one help please. thanks in advance
Try this code to goForward and GoBack.
- (IBAction) buttonGoBack
{
if ([webView canGoBack])
{
[webView goBack];
}
}
- (IBAction) buttonGoForward
{
if ([webView canGoForward])
{
[webView goForward];
}
}

Assign goBack to button for UIWebView

I am making a simple web browser, and have created a UIWebView, as well as two Buttons which each contain the text "<" and ">". I attempted assigning goBack and goForward functions to these buttons by Ctrl+Dragging onto the UIWebView, as you would if you used this official buttons. However, goBack, goForward, reload, and stopLoading are not present on the menu which appears. How would I, therefore, assign these functions to my buttons?
You could do it programmatically in your UIButton action.
- (IBAction)yourBackButtonClick:(id)sender {
[self.yourWebView goBack];
}
- (IBAction)yourForwardButtonClick:(id)sender {
[self.yourWebView goForward];
}
- (IBAction)yourStopButtonClick:(id)sender {
[self.yourWebView stopLoading];
}

How to hide uiwebview navigation buttons

I have a UIWebView that I use to load a webpage. I also have navigation buttons so you can go back and forward between previous pages loaded. Is there a way to hide the navigation buttons when there is no previous webpage?
Check here: Why is UIWebView canGoBack=NO in iOS7?
You can enable/disable your navigation buttons in the shouldStartLoadWithRequest method with canGoBack and canGoForward methods on UIWebView:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType
{
if ([webView canGoBack])
{
[_browserBackItem setEnabled:YES];
}
else
{
[_browserBackItem setEnabled:NO];
}
if ([webView canGoForward])
{
[_browserForwardItem setEnabled:YES];
}
else
{
[_browserForwardItem setEnabled:NO];
}
return YES;
}
I'm not aware of anything built in to UIWebView that allows this but maybe you could try keeping track of the pages with a variable.
Every time a url request is made the UIWebView delegate method gets called and you could increment the variable there (see here). Then decrement the variable once the user has selected the back button.
Hope that helps.
you can save the loaded pages in an NSArray for example and test if that array is empty you hide the button.
to know the opened pages, you implement the UIWebViewDelegate in your class, and in the – webView:shouldStartLoadWithRequest:navigationType: callback you save the url.

Resources