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.
Related
I used the GPPSignIn to login google in the first time.
But it's rejected by App Store because it will open the Safari when login
So I replace the GPPSignIn with GIDSignIn.
And it works! It can login with WebView!
But the question is.... how should I back when I open the WebView!
Or say how should add the "Back Button" in this WebView
Thanks for your helping :)
You can Try html markup such as My Button into the content of your UIWebview using stringByEvaluatingJavaScriptFromString method.
To capture the click event and prevent it from reloading content of your webview use this delegate:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if(navigationType==UIWebViewNavigationTypeLinkClicked && [[request.URL absoluteString] isEqualToString: #"yourTag01"])
{
//your action code goes here
return NO;
}
return YES;
}
For Action of your button You can use this :
if ([yourWebView canGoBack])
{
[yourWebView goBack];
}
I recently had a similar problem. I solved it with import libraries SafariServices.framework and run app again.
http://i.stack.imgur.com/eonwU.png
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];
}
}
Say there's a custom link embedded in a WebUIView, and I would want to redirect a user not to Safari but to a different screen in the app. Is it possible to change the click handler for that link?
You can assign a delegate to the webView and use the webView:shouldStartLoadWithRequest:navigationType: method to detect the links you want a return NO.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if ([[request.URL absoluteString] isEqualToString:#"http://www.customurl.com"]) {
// Do something
return NO;
} else {
return YES;
}
}
You may also want to check the navigationType against UIWebViewNavigationTypeLinkClicked.
I have this method that goes back to previous webpage in a web view under certain circumstance and it works fine. However, I want to implement a function that checks if I am on the original web page and if so, when I push the back button it takes me to a navigation view controller. The original page is this code [webView loadHTMLString:self.item.description baseURL:[NSURL URLWithString:self.item.link]];. Now I need to figure out how to check if I am on that page and if so I need to execute this code: [self.navigationController popViewControllerAnimated:YES];. Any help will be appreciated.
- (void)back
{
if ([webView canGoBack]) {
[webView goBack];
} else {
[webView loadHTMLString:self.item.description baseURL:[NSURL URLWithString:self.item.link]];
}
}
You can use the delegate method shouldStartLoadWithRequest of UIWebViewDelegate to check what contains in the url. if you find it then just go NavigationController
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
if ([[request.URL absoluteString] rangeOfString:#"http://yourURL"].location!=NSNotFound) {
[self.navigationController popViewControllerAnimated:YES];.
}
}
You need to add
<UIWebViewDelegate>
to the header. Then set webView.delegate to yes;
Once you have it set as a delegate, then you can get the absoluteString and the requestURL, and override the method. With the absoluteString you can check for a substring, text, domain, basically anything you want! You override shouldStartLoadWithRequest or webViewDidFinishLoad:
-(void)webViewDidFinishLoad:(UIWebView *)webView
-(void)shouldStartLoadWithRequest :(UIWebView *)webView
That calls everytime any webpage is loaded.
I have a ios program for iPad. It is used 2 view, A view has webview, another has imageview. When I returned webview from imageview, webview is opened homepage. I want to return last page . How can I do?
If I understand you correctly, you want to make sure, that the last page that was loaded in the UIWebView will be displayed again when you switch back to the UIWebView.
To achieve that you can keep track of the last URL that was loaded in the UIWebView. You can do that in the following UIWebViewDelegate method (declare currentURL as property):
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
self.currentURL = request.URL;
return YES;
}
And then when you bring back your UIWebView:
- (void)showWebView {
[yourWebView loadRequest:[NSURLRequest requestWithURL:currentURL]];
}