Cancel button/touch for UIWebView - objective-c

I wonderd if there is any way to make the user dismiss the WebView window if he doesn't want it anymore on the screen...?
I looked at this post but i didn't understand it well.
How to cancel a UIWebView?
can anyone give me an example please?
This is the code i have:
CGSize webScreen1;
webScreen1 = [[UIScreen mainScreen] applicationFrame].size;
CGRect webFrame1 = CGRectMake((webScreen1.width/11.0) ,(webScreen1.height/19.0) ,webScreen1.width/1.2,webScreen1.height/1.25);
defaultWebView.frame = webFrame1;
self.defaultWebView = [[UIWebView alloc] initWithFrame:webFrame1];
self.defaultWebView.backgroundColor = [UIColor whiteColor];
self.defaultWebView.scalesPageToFit = YES;
self.defaultWebView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
defaultWebView.inputView.hidden = YES;
[self.view addSubview: self.defaultWebView];
[self.defaultWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:
[NSString stringWithFormat:(NSString *)#"%#", #"http://www.google.com"]]]];
Thanks!

There is no such thing as cancel a webView, what you need to do is remove the UIWebView from the parent view. If your UIWebView is a subview of self.view then you can provide a button named Close which behaves like this -
- (IBAction)closeWebView:(id)sender
{
[self.webView removeFromSuperView];
self.webView = nil;
return;
}
This should remove the webview from your view.

...way to make the user dismiss the WebView window...
[self.defaultWebView removeFromSuperview];

look I will explain what happenes in the post you attached above and you will understand the technique,
the UIWebview is a component which views an web page, it may be html or other types,
there is one way to hide your webview is to add an action in the html of the webview and ovveride the request in the code of your app,
when you click on link or ahref or any action on UIwebview there is a delegate method which automatically runs before continuing with the request
(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
you can add a button or link on the html page and ovveride the request on this method
for example
Thing to click
and in the delegate method
(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if ([[[request URL] absoluteString] isEqualToString:#"http://hideWebView/"]) {
[self.webview setHidden:YES];
return NO;
}
return YES;
}
here you ovveride the request http://hideWebView/ and in the delegate you searched for this request and then hiddes the web view or anything else you want to do

Related

Custom BackButton in iOS webview

I want to create custom back button on each page in my iOS web-view (objective-C). Can anyone please suggest me how to implement.
Thanks in Advance
I am new to iOS Web-view that is why I posted directly what I want this is my code :
#import "ViewController.h"
#interface ViewController () <UIWebViewDelegate>
#end
#implementation ViewController{
__weak IBOutlet UIImageView *logoImage;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.webView.delegate = self;
NSString *urlString = #"https://www.anything.com";
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:requestObj];
self.webView.hidden = YES;
if ([_webView canGoBack]) {
[_webView goBack];
}}
- (void)webViewDidStartLoad:(UIWebView *)webView {
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
self.webView.hidden = NO;
logoImage.hidden = YES;
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
NSLog(#"%#", error);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I am passing a url of my website and I want to show a back button on every page of my application so that user can go to previous page. I have one more issue that whenever I minimise my application and opens it back from recent apps it get restarted while I want it to be resumed from the same screen
For the UIWebView, check the method - (void)goBack; out.
For the WKWebView, check the method - (WKNavigation *)goBack; and - (WKNavigation *)goBack:(id)sender; out.
UPDATE
// init the back button
[btnBack addTarget:self action:#selector(backButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
- (void)backButtonTapped:(id)sender
{
[self.webview goBack];
}
Assume you have set up your webview and back button (in the navigation bar or somewhere else) in the full code.
The browser could go back when the current (tab) page has a history list and the current page url is not the first and last one.
Make sure you really want to goBack in viewDidLoad, it needs some time to finishing loading the target page you specified (https://www.anything.com) just like you do it in a browser.
The method loadRequest of webview is asynchronous, it pushes the url request to webview and returns it immediately.
Bind the webview's goBack method to your custom back button like the above code snippets.
Try to click a sample link in the initial page (https://www.anything.com), once the new page finishes loading, it could be able to goBack with satisfying [self.webview canGoBack].
Make a breakpoint in the implementation of method backButtonTapped, if needed.
Drop the logoImage or other unrelated code issues in your next question, that makes your question more clear and helpful.

Why isn't activity indicator working for web view?

I am trying to load a web view (called widget) and set an indicator (called indicator) to show that the view is loading (the web page is no larger than www.google.com). The widget loads fine (I haven't included that code) but this snippet, which should send startAnimating to a UIActivityIndicatorView is not working (no animation or even appearance). Everything is connected in storyboard and "not loading" is always logged. This makes me think there is something wrong with my use of UIWebView's loading property.
sleep(2);
[super viewDidLoad];
[indicator setHidesWhenStopped:YES];
if (widget.loading == YES) {
NSLog(#"loading");
[indicator startAnimating];
} else {
NSLog(#"not loading");
[indicator stopAnimating];
}
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
Any thoughts?
Thanks
Try putting your start and stop animating in the delegate methods for the webview:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[indicator stopAnimating];
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
[indicator startAnimating];
}
Make sure you set the webview's delegate and are conforming to UIWebViewDelegate in your interface.

Generic "help" view which is accessible from all views in uinavigationView

Assuming an application has many views pushed to a uinavigationViewController, each view is different in content.
since the elements in the app are complex, I would like to show a small help view for specific views or elements in a specific view.
Imagine a "?" button that when pressed on will pop a new view in the center of the screen, playing youtube help video, or just a textual HTML loaded from a remote server.
Question: what is the best strategy for doing such a thing?
where should I place this code (App Delegate?)
if so, how would I call it from other views (with URL Parameter)
-(void)showHelpView:(NSString *)theURLString{
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 800, 600)];
//webView.delegate= self;
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:theURLString]]];
[window addSubview:webView];
[window makeKeyAndVisible];
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
}
I'd prefer using pseudo-modal views. So that you create view every time, and add it on top of current window. I've used this approach for OAuth 2.0 authorisation popup in my last app.
Basically you create custom UIViewController subclass, provide custom initialisator, such as, for example:
// - (id) initWithURL: (URL*)url {
// ...
// self.url = url;
// ...
// return self;
PseudoModalViewController* pmvc = [[PseudoModalViewController alloc] initWithURL:#"http://www.google.com/"];
After you've created view you add it on top of current window:
UIWindow* window = [[UIApplication sharedApplication] keyWindow];
if(!window)
{
window = [[[UIApplication sharedApplication] windows] objectAtIndex:0];
}
[window addSubview:pmvc.view];
In, for example, viewDidLoad you load url obtained in initialisator:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.webView loadRequest:[NSURLRequest requestWithURL:url]];
}
You may even design UI in Interface Builer for that. As a last piece I'd recommend add to include full screen transparent background view to ensure that all views below are disabled.
Oh, almost forgot, you hide that view with just a simple [self.view removeFromSuperview]. Don't forget to provide correct memory management for that view (i.e. release it in time, etc).

UIActivityIndicatorView animation issue

I am using a UIActivityIndicatorView as an IBOutlet. I am trying to control it using [activityView startAnimating] and [activityView stopAnimating].
I have enabled 'Hides when stopped' and 'Animating' behaviors in XIB file.
I want to start animate the spinner for several user actions inside the controller. But after the first [activityView stopAnimating] call it does not response to [activityView startAnimating] call again.
That is mean the spinner is disappeared after the first [activityView stopAnimating] call.
I tried activeView.hidden = NO; before the next [activityView startAnimating] call. But it does not work.
Any idea about this issue?
Edited
After my controller loaded I do follow thing,
do {
[self.activityIndicator startAnimating];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[webServiceCallOperation getResults];
[self.activityIndicator stopAnimating];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
} while (![webServiceCallOperation isValideResponse] || [webServiceCallOperation isServerError]);
In the same controller I have a IBAction for a button click.
- (IBAction)tappedSearchButton:(id)sender
{
[self.activityIndicator startAnimating];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[self tappedSearchButtonAction];
[self.activityIndicator stopAnimating];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
I noticed that the spinner not begin to animate just after the [self.activityIndicator startAnimating]; call. Using [self tappedSearchButtonAction]; I call to a web service and go to the search results view. The spinner begin to animate just before change the view. That's why I did not see it. But I suppose to animate it before web service call and should animate it while my web service call.
Same behavior of the network activity indicator.
As aadhira suggested check for release and if you are making the calls on the main thread.
[NSObject performSelectorOnMainThread:#selector(SEL) withObject:nil waitUntilDone:YES];
It starts animating because as you say you have enabled Animating behavior in the XIB file. But after you stop it for the first time, it is not animating anymore until you start it again programatically.

How to cancel a UIWebView?

I'm modally presenting a UIViewController with a UIWebView as its view. How do I dismiss the UIWebView when the user hits a cancel button on it?
One idea I have is to have the cancel button link to http://cancel and then check in
- (void)webViewDidStartLoad:(UIWebView *)webView
If webView.request.URL.host isEqualToString:#"cancel", then dismiss the view controller.
Does the host have to have a dot in it, e.g., "cancel.com"?
You're on the right path, but you're approach is slightly off. You don't need or want this to be an HTTP URL. Make your URL cancel:.
Thing to click
Then implement webView:shouldStartLoadWithRequest:navigationType: in your web view delegate. Something like this (untested):
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if ([request.URL.scheme isEqualToString:#"cancel"]) {
[self dismissModalViewControllerAnimated:YES];
return NO;
}
return YES;
}
It's not entirely clear if you want to stop loading in a web view or just dismiss the modal view controller that's containing it.
To stop loading:
[webView stopLoading];
To dismiss the view controller:
[self dismissModalViewControllerAnimated:YES];
Don't forget to set the web view's delegate to nil before you release it.