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

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.

Related

Binding webview navigation actions

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;
}
}

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];
}

Setting UIButton Enabled and setting an Image for each state is not working

I have a viewController which contains a button. I am showing this view controller over another viewController not modal, but via
[self.storyboard instantiateViewControllerWithIdentifier:#"MyButtonView"]
So the result is one viewController floating over the another viewController, where the top viewController only takes up about half the screen.
The bottom view controller at certain times, tells the top viewController to disable and enable its button. When this call happens (and yes it is happening because I am debugging it). The button is not being enabled or disabled.
=== This is in my viewdidload method =====
[self.myCirlceButton setImage:[UIImage imageNamed:#"Checkmark_v1_normal.png"] forState:UIControlStateNormal];
[self.myCirlceButton setImage:[UIImage imageNamed:#"Checkmark_v1_selected.png"] forState:UIControlStateHighlighted];
[self.myCirlceButton setImage:[UIImage imageNamed:#"Checkmark_v1_disabled.png"] forState:UIControlStateDisabled];
=== These are delegate method on the top viewController that get called by the bottom viewcontroller ===
-(void)enableConfirmArrow
{
[self.myCirlceButton setEnabled:YES];
}
-(void)disableConfirmArrow
{
[self.myCirlceButton setEnabled:NO];
}
When I break point on enable and disable methods the self.myCircleButton is NOT nil.
I have no idea why the UI is NOT updating? The only thing I can think of is the above enable and disable methods sometimes get called back and forth about 10 times in 3 seconds.
/// UPDATE ///
As a test I changed the following and it does remove it. More proof that the disable is being called.
-(void)disableConfirmArrow
{
[self.myCirlceButton setEnabled:NO];
[self.locationCirlceButton removeFromSuperview];
}

iOS: show same "Please sign-in" screen for some view controllers, then change it to real content?

I have an app with tabbed view: search | subscribes | messages
All tab buttons are visible at app start even for unauthorized user.
But when unauthorized user clicks on let's say messages tab, I want to show "MessagesViewController" but it must show grey screen:
Please sign-in. [button sign-in] [button registration]
When user clicks [button sign-in] - modal UIViewController appears. After sign-in user goes back to "MessagesViewController" tab, but this time user can see his messages. Same grey screen must be in subscribes tab.
I'm new to iOS so I want to know what is a proper way to do this. Subscribes and messages are table views. Do I need to place another view on top of tables to overlap them and then hide it after authorization? Or can I create one separate "unauthorized" controller, connect it with tabs and reuse it? If so, how can I connect messages and subscribes controllers back to tabs after sign in?
Create a baseViewController, make all three viewController's extend that base.
Think of a method that will show the user "please sign-in" message. A gray UIView (with a UILabel and two UIButtons) overriding all the content seems good to me.
Add the buttons on that gray UIView, with self s as targets
Check in the viewWillAppear method whether the user is not logged in or not. Show the gray view if not.
Create a boolean and before you fire the segue you proof, if the boolean is yes or no. When the boolean is yes, the segue is performed. Otherwise a segue to your Login View is performed.
I hope this solves your problem
you can use
- (void) viewWillAppear:(BOOL)animated
to do this
#interface Messages
{
BOOL loggedIn;
UILabel *pleaseSignIn;
UIButton *signin;
UIButton *register;
}
- (void) viewWillAppear:(BOOL)animated
{
if([[[NSUserDefaults standardUserDefaults] valueForKey:#"logged"] isEqualToString:#"YES"])
{
loggedIn = YES;
}
else
{
loggedIn = NO;
}
if(loggedIn)
{
//Display messages
pleaseSignin.hidden = YES;
signin.hidden = YES;
register.hidden = YES;
}
else
{
//Remove messages view
pleaseSignin.hidden = NO;
signin.hidden = NO;
register.hidden = NO;
}
}

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