I want to add activity indicator to a web view.
But i don't know when web view finish loading.
I start animating in viewdidload..
You shouldn't start animating in viewDidLoad. Conform to the
UIWebViewDelegate
protocol and make your web view's delegate your view controller, then use the delegate methods:
#interface MyVC: UIViewController <UIWebViewDelegate> {
UIWebView *webView;
UIActivityIndicatorView *activityIndicator;
}
#end
#implementation MyVC
- (id)init
{
self = [super init];
// ...
activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
activityIndicator.frame = CGRectMake(x, y, w, h);
[self.view addSubview:activityIndicator];
webView = [[UIWebView alloc] initWithFrame:CGRectMake(x, y, w, h)];
webView.delegate = self;
// ...
return self;
}
- (BOOL)webView:(UIWebView *)wv shouldStartLoadWithRequest:(NSURLRequest *)rq
{
[activityIndicator startAnimating];
return YES;
}
- (void)webViewDidFinishLoading:(UIWebView *)wv
{
[activityIndicator stopAnimating];
}
- (void)webView:(UIWebView *)wv didFailLoadWithError:(NSError *)error
{
[activityIndicator stopAnimating];
}
#end
Implement the UIWebViewDelegate protocol
These are the delegates you need to implement in your code:
- (void)webViewDidStartLoad:(UIWebView *)webView; //a web view starts loading
- (void)webViewDidFinishLoad:(UIWebView *)webView;//web view finishes loading
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error; //web view failed to load
You will want to listen for the web view delegate callbacks to correctly show your activity indicator.
Specifically you will want to listen for:
webViewDidStartLoad: (start your activity indicator animation)
webViewDidFinishLoad: (end it)
webView:didFailLoadWithError: (end it)
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIWebViewDelegate_Protocol/Reference/Reference.html
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.webViewRef.delegate = self;
NSURL *websiteUrl = [NSURL URLWithString:Constants.API_TERMS_AND_CONDITIONS_URL];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:websiteUrl];
[self.webViewRef loadRequest:urlRequest];
}
#pragma mark
#pragma mark -- UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
[self.activityIndicator startAnimating];
return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView{
[self.activityIndicator startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
[self.activityIndicator stopAnimating];
self.activityIndicator.hidden = YES;
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(nullable NSError *)error{
[self.activityIndicator stopAnimating];
self.activityIndicator.hidden = YES;
}
Related
I am trying to start imageView's animation on viewDidAppear phase.
I would like to stop animation when content of webview loaded on viewDidLoaded phase.
However my UIImageView does not stop animation when stopAnimation is called
on ViewDidLoaded phase.
can anybody tell me what should I do to stop animation on viewDidLoaded?
any help will be appreciated.
UIImageView *customActivityIndicator;
-(void)viewDidAppear:(BOOL)animated{
...
customActivityIndicator = [[UIImageView alloc] initWithFrame:CGRectMake(loadingPosX, loadingPosY, loadingImageWidth, loadingImageHeight)];
[self.view addSubview: customActivityIndicator];
customActivityIndicator.animationImages = [NSArray arrayWithObjects:[UIImage imageNamed:#"1.png"],[UIImage imageNamed:#"2"],[UIImage imageNamed:#"3.png"],[UIImage imageNamed:#"4.png"],[UIImage imageNamed:#"5.png"],nil];
customActivityIndicator.animationDuration = 1.0; // in seconds
customActivityIndicator.animationRepeatCount = 0; // sets to loop
[customActivityIndicator startAnimating]; // starts animating
}
- (void)viewDidLoad {
[super viewDidLoad];
....
[webView loadRequest : requestObj];
[customActivityIndicator stopAnimating]; // stop animating
}
Use the UIWebViewDelegate and protocol method webViewDidFinishLoad
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[customActivityIndicator stopAnimating]; // stop animating
}
- (void)viewDidLoad {
[super viewDidLoad];
....
webView.delegate = self;
[webView loadRequest : requestObj];
}
I have an apps written with Objective C. I can't load web page to UIWebView. UIWebViewDelegate not working. My app code ;
#import <UIKit/UIKit.h>
#interface Reklamlarim : UIView <UIWebViewDelegate>
- (void)webSayfasiYukle;
#end
I did it this way .m page. webViewDidFinishLoad and webView didFailLoadWithError: not working.
#import "Reklamlarim.h"
#implementation Reklamlarim
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self webSayfasiYukle];
}
return self;
}
- (void)webSayfasiYukle{
NSLog(#"webSayfasiYukle");
UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
webView.delegate = self;
NSString *url=#"http://www.google.com";
NSURL *nsurl=[NSURL URLWithString:url];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[webView loadRequest:nsrequest];
[self addSubview:webView];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(#"webViewDidFinishLoad");
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
NSLog(#"webView didFailLoadWithError");
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)aRequest navigationType:(UIWebViewNavigationType)navigationType {
return YES;
}
I have, ios 8 device, xcode 6.3. I'm sorry for my bad english.
thanks ...
You need to check how you are initiating the Reklamlarim view because
- (id)initWithFrame:(CGRect)frame
is only calling when you are initiating from some where and call this init function it doesn't call automatically.. so move [self webSayfasiYukle]; to
-(void)awakeFromNib {
[self webSayfasiYukle];
}
like this and try.
I want the progressView working while my URL is loading. What should I do for that?
here's my .m code:
-(void)openURL{
UIWebView *webView = [[UIWebView alloc] init];
[webView setFrame:CGRectMake(0, 0, 320, 460)];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://google.com"]]];
[[self view] addSubview:webView];
}
- (IBAction)goto:(id)sender {
[self openURL];
}
Implement UIWebViewDelegate, specifically:
-(void)openURL{
UIWebView *webView = [[UIWebView alloc] init];
webView.delegate = self;
// ... rest of method as above ...
}
- webViewDidStartLoad:(UIWebView *)webView {
// Start progress.
}
- webViewDidFinishLoad:(UIWebView *)webView {
// Stop progress.
}
You could do something like this
[progressView startAnimating];
dispatch_async(dispatch_get_main_queue(), ^{
[webView loadRequest:yourRequest];
[progressView stopAnimating];
[progressView removeFromSuperView];
}
This will start the progress view before the operation begins, and will stop and remove it when it finishes.
I've researched and researched and still don't understand why shouldStartLoadWithRequest is never called. My page loads fine and some of the UIWebview delegate protocol methods are called. Please find relevant snippets from my code below:
Synthesize my webview in my .m (defined in a header file):
#implementation PortViewController
#synthesize WebView = myWebView;
I load my webview successfully:
myURLString = [NSString stringWithFormat:#"https://%#", defaultWebsite];
myURL = [NSURL URLWithString: myURLString];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:myURL];
[myWebView loadRequest:myRequest];
set my delegate to self
- (void)viewDidLoad
{
[super viewDidLoad];
[myWebView setOpaque:NO];
[myWebView setBackgroundColor:[UIColor clearColor]];
//myWebView.description.
myWebView.delegate = self;
}
all of my protocol methods are called EXCEPT shouldStartLoadWithRequest
- (void)webViewDidStartLoad:(UIWebView *)myWebView {
[activityIndicator startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)myWebView {
[activityIndicator stopAnimating];
}
- (BOOL)WebView:(UIWebView *)myWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSLog(#"inside Webview");
if([[request.URL absoluteString] hasPrefix:#"http://www.nzx"]) {
// do stuff
NSLog(#"Have caught the prefix");
return YES;
}
return NO;
}
Thanks in advance.
Try setting the delegate in ViewWillAppear instead of ViewDidLoad
-(void)viewWillAppear:(BOOL)animated
{
myWebView.delegate = self;
}
and include the webview delegate in your interface of PortViewController.m file
#interface PortViewController ()<UIWebViewDelegate>
#end
It should work.
delegate method should be:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
And if you have UIWebView in xib, then probably you are trying to load request when UIWebView is nil
So make sure your myWebView instance exists.
I have a view controller that houses a UIWebView. I want to display an Activity indicator in the top right so the user knows the webpage is loading; however on build&run, the activity monitor does not show.
I have a UIWebView outlet called webView and a UIActivityIndicatorView outlet called activityIndicator.
Here is my implementation:
- (void)viewDidLoad
{
[super viewDidLoad];
webView.delegate=self;
[self.view addSubview:activityIndicator];
NSString *fullURL = #"http://www.oncologyeducation.com"; NSURL *url = [NSURL URLWithString:fullURL]; NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
}
- (void)viewDidUnload
{
[self setWebView:nil];
[self setActivityIndicator:nil];
[self setActivityIndicator:nil];
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (interfaceOrientation==UIInterfaceOrientationPortrait || interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown) {
return NO;
} else {
return YES;
}
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
return YES;
}
-(void) webViewDidStartLoad:(UIWebView *)webView{
NSLog(#"load started");
[activityIndicator startAnimating];
activityIndicator.hidden = NO;
}
-(void) webViewDidFinishLoad:(UIWebView *)webView{
NSLog(#"load finished");
[activityIndicator stopAnimating];
activityIndicator.hidden = YES;
}
-(void) webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
[activityIndicator stopAnimating];
activityIndicator.hidden = YES;
}
#end
On my storyboard, I have an activity indicator view which is connected to File's Owner. Thank you for any and all advice!
#CodaFi - you're right, that was not necessary. No need to add the subview.
Anybody who wants to reuse the code, lose the line:
[self.view addSubview:activityIndicator];
Turns out xCode was acting kind of wonky and copying the wrong storyboard into the project on compile. I renamed my storyboard, set it to the new name in the project settings, and the indicator spun with glee!