Well, after reading a bunch of SO posts on this issue, I still can't fix problems with my activity indicator. This indicator is in a view in a tab under the control of its own view controller. It has a view with a UIWebView which loads a local html page just fine. The view is loaded with initWithNibName and then awakeFromNib. Here's the part I think is relevant:
#implementation HelpViewController
#synthesize webView = _webView;
#synthesize back = _back;
#synthesize forward = _forward;
#synthesize aI = _aI;
- (void) viewDidLoad {
_aI = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[_aI stopAnimating];
// Now add the web view
NSString *filePath = [[NSBundle mainBundle]
pathForResource:#"Help"
ofType:#"html"];
[self.view addSubview:_webView];
_webView.delegate = self;
NSURL* fileURL = [NSURL fileURLWithPath:filePath];
NSURLRequest *request = [NSURLRequest requestWithURL:fileURL];
[_webView loadRequest:request];
[super viewDidLoad];
}
-(void) webViewDidStartLoad:(UIWebView *)webView {
[_aI startAnimating];
_back.enabled = NO;
_forward.enabled = NO;
}
-(void) webViewDidFinishLoad:(UIWebView *)webView {
[_aI stopAnimating];
if (webView.canGoBack) {
_back.enabled = YES;
_back.highlighted = YES;
}
if (webView.canGoForward) {
_forward.enabled = YES;
_forward.highlighted = YES;
}
}
The navigation buttons work fine. The activity indicator was placed in the nib, but in the main view, not on/over/under the webView. In the attributes, I have Hides When Stopped checked. If I check Animating the indicator is always visible and animated, no matter how I navigate through the UIWebView.. If I uncheck Animating, Hidden is automatically checked for me. In this case, the indicator never shows up. So it's either always on or always off.
I've read quite a bit about cases where you need to have the indicator on a different thread. I'm not sure, but I don't think that applies here (I load a local html page but allow users to navigate off and then back to the local page). But, I seem to have some disconnect; perhaps it is the fact that the indicator is in the main view but the pages are in the webView? Or I'm not calling things in the right methods. Or who knows... Thanks for any help!
You overwrite _aI in your viewDidLoad and never place it in your view hierarchy, so the object you are sending the messages to is never visible and the activity indicator placed in interface build will never change its state, so thats why its either always animating or hidden.
Related
I have a screen that shows a web view. However, I allow people to cycle through different web views by updating an object. When I load the first page, the web view loads fine. However, when I try to update the page from the new object in viewwillappear, while other elements such as a title that goes above the webview load fine, the web view does not refresh.
//in viewWillAppear
self.title = self.file.imageDescript;//The title loads fine
NSString *urlBase = #"https://www.~com/images/";
NSString * imagename = self.file.imagename;
NSString *url = [NSString stringWithFormat:#"%#%#",urlBase,imagename];
NSURL *realUrl = [NSURL URLWithString:url];
NSURLRequest *request = [NSURLRequest requestWithURL:realUrl];
[webView loadRequest:request];
I have tried various versions of setNeedsDisplay and also applied these to subviews of the web view without success as in this method:
- (void) forceRedrawInWebView:(UIWebView*)webView {
NSArray *views = webView.scrollView.subviews;
for(int i = 0; i<views.count; i++){
UIView *view = views[i];
//if([NSStringFromClass([view class]) isEqualToString:#"UIWebBrowserView"]){
[view setNeedsDisplayInRect:webView.bounds]; // Webkit Repaint, usually fast
[view setNeedsLayout]; // Webkit Relayout (slower than repaint)
// Causes redraw & relayout of *entire* UIWebView, onscreen and off, usually intensive
[view setNeedsDisplay];
[view setNeedsLayout];
// break; // glass in case of if statement (thanks Jake)
//}
}
}
I have read in one SO answer that since IOS8.2, refresh of UIWebView no longer works (as WKWebView) has replaced it. I would switch to WKWebView except that it is not supported in storyboards, apparently, and I have a bunch of stuff going on in the storyboard.
Note: this has nothing to do with refreshing the same web url because content has changed on a server. It has to do with loading an entirely different web url in the UIWebView
Can anyone suggest way to fix this?
I want to load a URL on a UIViewController with an UIWebView and UIActivityIndicatorView, but UIActivityIndicator never appears and UIWebView never loads the URL.
This is my code:
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.title = #"Web";
[self displayURL:[NSURL URLWithString:#"(Website)"]];
}
-(void) webViewDidFinishLoad:(UIWebView *)webView {
[self.loadView stopAnimating];
self.loadView.hidden = YES;
}
-(void) displayURL:(NSURL *) aURL {
self.web.delegate = self;
self.loadView.hidden = NO;
[self.loadView startAnimating];
[self.web loadRequest:[NSURLRequest requestWithURL:aURL]];
}
If you add your webView by code, you need add
[self.view addSubview: web];
after you created webView, and of course you need to add UIActivityIndicatorView as a subView of webView or superView, so you need to add [web addSubview: loadView] or [self.view insertSubview: loadView aboveSubview: web];.
And about the URL, if your website URL is like www.google.com, you need to change #"(Website)" to the real URL. If your website is a file copied in your project, you need to change [NSURL URLWithString: ]; to [NSURL fileURLWithPath:[NSBundle mainBundle][pathForResource: ofType:]];.
Really poorly formulated question.
UIWebViewController does not work
UIWebViewController does not exists, you are talking about a UIWebView in a UIViewController
Code is very incomplete
I'm trying to make it so that the back button will only be enabled when the user presses a link and/or if canGoBack = TRUE. I tried using an if statement in the body but that didn't work. I then tried using what I could find online and it still didn't work. My code is below. What am I doing wrong? Thanks!
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest: (NSURLRequest *)request navigationType: (UIWebViewNavigationType)navigationType{
if (navigationType == UIWebViewNavigationTypeLinkClicked)
{
NSString *url = webView.request.URL.absoluteString;
NSURL *nsurl = [NSURL URLWithString:url];
NSURLRequest *nsrequest = [NSURLRequest requestWithURL:nsurl];
[webView loadRequest:nsrequest];
backButton.enabled = TRUE;
}
return TRUE;
}
shouldStartLoadWithRequest() won't get called if you haven't set the web view's delegate.
In order to use UIWebView its typically set as an outlet property of a view controller, so you should have something like this:
IBOutlet UIWebView* webView;
The webView has a delegate property which needs setting to your view controller, you can set delegates in a storyboard, but its more explicit to set it in code:
- (void)viewDidLoad
{
[super viewDidLoad];
self.webView.delegate = self;
99% of the time you just set delegates, but UIWebView's delegate is funny in that it needs setting to nil when finished with. So add this:
- (void)viewDidUnload
{
[self.webView stopLoading];
self.webView.delegate = nil;
UIWebView and WKWebView both have a canGoBack and canGoForward property on them. You can use KVO to see when these are enabled, and enable/disable your back and forward buttons accordingly.
http://nshipster.com/key-value-observing/
I have a UIWebView that opens up a webpage in my app where a user can log in to their account. What I want the app to do is to change view controllers immediately after the user has logged into their account without seeing the next webpage in the webview. This is the code in my
viewDidLoad function:
- (void)viewDidLoad {
[super viewDidLoad];
NSString *webpage = #"myfakewebpage.com";
self.defaults = [NSUserDefaults standardUserDefaults];
[self.activityIndicator startAnimating];
[self.myWebView setHidden:YES];
self.myWebView.delegate = self;
NSURL *myURL = [NSURL URLWithString:triggerPage];
self.myRequest = [NSURLRequest requestWithURL:myURL];
[self.myWebView loadRequest:self.myRequest];
}
And this is what my webViewDidFinishLoad function looks like:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
if ([self.activityIndicator isAnimating] == YES) {
[self.activityIndicator stopAnimating];
[self.activityIndicator setHidden:YES];
[self.myWebView setHidden:NO];
}
[self loadViewControllerAfterAuthentication];
}
[self loadViewControllerAfterAuthentication]; calls a function I built myself that will change the view controller through a poor workaround based on the response of the webpage after the user has authenticated. As of right now, the user can authenticate, but the next webpage will load for about half a second before my code kicks in and a new view controller is displayed. If anyone has an idea on how I can make the transition described above any smoother, that would be greatly appreciated.
In my app, I am trying to make a splash image appear as my UIWebView loads so it is not just a blank screen. However my webViewDidFinishLoad method will not work. This means that the splash image appears but does not disappear from the screen once the UIWebView has loaded.
My code for the method is:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(#"content loading finished");
[loadingImageView removeFromSuperview];
}
Any help on why the method will not work would be appreciated greatly.
My .h:
#interface ViewController : UIViewController
-(IBAction)makePhoneCall:(id)sender;
#property (nonatomic, strong) IBOutlet UIWebView *webView;
#property(nonatomic, strong) UIImageView *loadingImageView;
#end
My ViewDidLoad and webViewDidFinishLoading:
- (void)viewDidLoad {
UIWebView *mWebView = [[UIWebView alloc] init];
mWebView.delegate = self;
mWebView.scalesPageToFit = YES;
[super viewDidLoad];
}
//**************** Set website URL for UIWebView
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.sleafordpizza.com/food"]]];
//**************** Add Static loading image to prevent white "flash" ****************/
UIImage *loadingImage = [UIImage imageNamed:#"LittleItalyLogo.png"];
loadingImageView = [[UIImageView alloc] initWithImage:loadingImage];
loadingImageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"LittleItalyLogo.png"],
nil];
[self.view addSubview:loadingImageView];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(#"content loading finished");
// Remove loading image from view
[loadingImageView removeFromSuperview];
}
Hi probably you do not set proper delegate.
This is small code tip for you.
-(void)viewDidLoad {
mWebView = [[UIWebView alloc] init];
mWebView.delegate = self;
mWebView.scalesPageToFit = YES;
}
-(void)webViewDidFinishLoad:(UIWebView *)webView {
[loadingImageView removeFromSuperview];
NSLog(#"finish");
}
In you're .h file add.
#interface MyView: UIViewController <UIWebViewDelegate> {
UIWebView *webView;
}
Code fixes.
For .h file
#interface ViewController : UIViewController<UIWebViewDelegate>
-(IBAction)makePhoneCall:(id)sender;
#property (nonatomic, strong) IBOutlet UIWebView *webView;
#property(nonatomic, strong) UIImageView *loadingImageView;
#end
For .m file
- (void)viewDidLoad
{
[super viewDidLoad];
webView.delegate = self;
//**************** Set website URL for UIWebView
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.sleafordpizza.com/food"]]];
//**************** Add Static loading image to prevent white "flash" ****************/
UIImage *loadingImage = [UIImage imageNamed:#"LittleItalyLogo.png"];
loadingImageView = [[UIImageView alloc] initWithImage:loadingImage];
loadingImageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"LittleItalyLogo.png"],
nil];
[self.view addSubview:loadingImageView];
}
At certain times, this delegate method actually never gets fired. I have had severe problems with the same thing in some of my projects.
At one occasion, I actually had to solve it with a timer, checking the state of the web view every second or so to see if I could proceed.
In that particular case, I just needed a certain element to be present. Still, the view did not trigger the finish loading event, due to external script errors being injected.
So, I just started a trigger when the web view begun loading, then called a method every now and then to see if the web view contained the element in question.
- (void)methodCalledByTimer {
if (<I still do not have what I need>) {
//The web view has not yet finished loading; keep checking
} else {
//The web view has finished loading; stop the timer, hide spinners and proceed
}
}
You could also check if the web view is actually loading, if that is absolutely necessary:
- (void)methodCalledByTimer {
if (self.webView.isLoading) {
//The web view has not yet finished loading; keep checking
} else {
//The web view has finished loading; stop the timer, hide spinners and proceed
}
}
Then, naturally, I'd check for the finishedLoading event as well, just to be sure. Remember to also implement the webView:didFailLoadWithError: method as well.
When waiting for a web page to finish loading, there are some things to keep in mind.
For instance, do you really need it to stop loading, or is there anything else you can do? In my case, I needed an element. Being able to properly execute a script is another thing that may be required.
Second, is the loading page using any external resources? I once had external script errors causing the webViewDidFinishLoad: method to not being called at all. If I removed the external scripts, it worked.
Third, if the page is using external resources, you are exposed not only to the loading capacity of your own resources, but that of the external resources as well. Tracking scripts, ads etc...if one resource provider is delivering content sloooowly (or not at all), you could page could be stuck in loading state forever.
So, I'd go with checking for something else. :)
I see you aren't handling errors. If there is an error, all subsequent delegate calls will not happen. I was surprised to find that this is true when the webview uses a plugin too. It calls this error method telling you that the webview handed off to the delegate, in my case the movie player.
implement this and see if that is it.
-(void) webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
if (error.code == 204) {
//request was handled by a plugin instead of by the webview directly
...
}
else
{
NSLog(#"didFailLoadWithError. ERROR: %#", error);
}
}
I was able to do all the remaining loading work in this method instead of the webviewdidfinishLoad