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];
}
Related
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.
I use a local HTML file displayed in a UIWebview to display "Help" in my iPad app. The web view is embedded in another view which also has back and forward buttons linked to the goBack and goForward methods.The buttons work as expected in the iOS 10.2 simulator, but not in in the iOS 11 simulators. In iOS 11 when I tap on a link in the HTML file the link works, but the back button does not, despite the fact that canGoBack is true. Oddly enough the back button starts to work after I tap on a link that returns me to the top of the page. Is this an Apple bug or is there a workaround? My method:
-(IBAction)goBack:(id)sender {
NSLog(#"HVC in goBack canGoBack is %d", self.webView.canGoBack);
[self.webView goBack];
}
Output when the go back button does not work:
2017-11-10 09:32:41.752614-0700 MHRS[2436:151546] HVC in goBack canGoBack is 1
If your initial page in UIWebView is not remote page after clicking any link in this first page you will be redirected, but goBack method will became unavailable try this method it may work for you.
- (void) goBackToRederect {
if ([self.webView canGoBack]) {
[self.webView goBack];
} else {
[self.webView loadData:htmlData MIMEType:#"text/html" textEncodingName:#"UTF-8" baseURL:url];
} }
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];
}
}
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;
}
}
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.