activity indicator not stopping - iphone

for my following code, why is my activity indicator for my webview not stoping?
//this part ok
NSURL *theURL = [NSURL URLWithString: Link];
NSURLRequest *request = [NSURLRequest requestWithURL: theURL];
UIWebView * webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,300,300)];
webView.scalesPageToFit = YES;
[webView setDelegate: self];
[webView loadRequest: request];
/*this part ok*/
UIViewController *newController = [[UIViewController alloc] init];
newController.view = webView;
[self.navigationController pushViewController:newController animated:YES ];
//activity indicatior not stoping!
CGRect frame = CGRectMake(0.0, 0.0, 25.0, 25.0);
activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:frame];
[activityIndicator startAnimating];
[activityIndicator sizeToFit];
activityIndicator.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleBottomMargin);
UIBarButtonItem *loadingView = [[UIBarButtonItem alloc] initWithCustomView:activityIndicator];
loadingView.target = newController;
newController.navigationItem.rightBarButtonItem = loadingView;
Thks in advance!
:)

you have to do [activityIndicator stopAnimating] when you want it to stop...

You need to implement the UIWebViewDelegate methods:
- (void)webViewDidStartLoad:(UIWebView *)webView {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
[activityIndicator stopAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
[activityIndicator stopAnimating];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
[activityIndicator stopAnimating];
}

Related

Webview content is not readable after stop animating loading image

I am building one iPhone application where I have two views one for login view and another for web view. When user enters the login credentials, then it will redirect to web view. while loading the webview from login view too much time is taking to load. hence I used alertview to show loading image.
#interface FocusViewController ()
#end
#implementation FocusViewController
#synthesize txtsecurecode;
#synthesize webView;
#synthesize OrganizationCode;
UIActivityIndicatorView *indicator;
UIAlertView *alert;
- (void)viewDidLoad
{
[super viewDidLoad];
appDelegate = (FocusAppDelegate *)[[UIApplication sharedApplication]delegate];
if(appDelegate.data.strOrganizationCode == nil || appDelegate.data.strAccessCode == nil)
{
NSLog(#"YOUR FIRST SCREEN");
_loginView.hidden = NO;
webView.hidden = YES;
}
else
{
NSLog(#"LOAD WEBVIEW DIRECTLY\n");
NSLog(#"Organization Code -> %# \n Secure Access Code -> %#",appDelegate.data.strOrganizationCode,appDelegate.data.strAccessCode);
OrganizationCode.text = appDelegate.data.strOrganizationCode ;
txtsecurecode.text = appDelegate.data.strAccessCode;
[self loginClicked:nil];
webView.hidden = NO;
_loginView.hidden = YES;
}
}
- (IBAction)loginClicked:(id)sender {
#try {
if([[txtsecurecode text] isEqualToString:#""] || [[OrganizationCode text] isEqualToString:#""] ) {
[self alertStatus:#"Please enter Access code" :#"Login Failed!":0];
}
else
{
NSString *post =[[NSString alloc] initWithFormat:#"txtsecurecode=%# #&password=%#",[txtsecurecode text],[OrganizationCode text]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"http://myexample.com/AccountService/security/ValidateAccess?accesscode=%#&companycode=%#&type=1", txtsecurecode.text, OrganizationCode.text]];
NSString *responseData = [[NSString alloc]initWithData:[NSData dataWithContentsOfURL:url] encoding:NSUTF8StringEncoding];
if([responseData isEqualToString:#""]){
[self alertStatus:#"Please enter valid Access Code" :#"Login Failed !" :0];
}
else
{
appDelegate.data.strOrganizationCode = OrganizationCode.text;
appDelegate.data.strAccessCode = txtsecurecode.text;
[FocusAppDelegate addCustomObjectToUserDefaults:appDelegate.data key:kCredentails];
//Updated
_loginView.hidden = YES;
webView.hidden = NO;
responseData = [responseData stringByReplacingOccurrencesOfString:#" "" " withString:#""];
NSString* encodedString = [responseData stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(#"Response ==> %#" ,encodedString);
alert = [[[UIAlertView alloc] initWithTitle:#"Loading\nPlease Wait..." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil] autorelease];
[alert show];
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
indicator.center = CGPointMake(alert.bounds.size.width / 2, alert.bounds.size.height - 50);
[indicator startAnimating];
[alert addSubview:indicator];
[indicator release];
webView.backgroundColor = [UIColor clearColor];
webView.opaque = NO;
webView.delegate = self;
webView.frame = self.view.bounds;
NSString* urlTwo = [[encodedString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
stringByReplacingOccurrencesOfString:#"%22" withString:#""];
NSURL *url2;
if([urlTwo hasPrefix:#"http://"]){
url2 = [NSURL URLWithString:urlTwo];
}else{
url2 = [NSURL URLWithString:[NSString stringWithFormat:#"http://%#" , urlTwo]];
}
NSLog(#"url2:%#", url2);
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url2];
[webView loadRequest:requestObj];
[[self view] addSubview:webView];
}
}
}
#catch (NSException * e) {
NSLog(#"Exception: %#", e);
[self alertStatus:#"Login Failed." :#"Login Failed!" :0];
}
}
-(void)webViewDidStartLoad:(UIWebView *)webView{
NSLog(#"webViewDidStartLoad");
[self.view addSubview:self.indicator];
alert.hidden = NO;
}
- (void) webViewDidFinishLoad:(UIWebView *)webView {
NSLog(#"webViewDidFinishLoad");
[self.indicator removeFromSuperview];
[self.alert dismissWithClickedButtonIndex:1 animated:NO] ;
}
- (IBAction)backgroundClick:(id)sender
{
[txtsecurecode resignFirstResponder];
[OrganizationCode resignFirstResponder];
}
#end
Until content in webview displays, loading image is displaying and working good. but content of webview is not editable and remaining static as I think I used 'web view.hidden = YES'. when I comment the alert method and run then content of webview is also editable and working good as required. I need to load the content of url after loading image stops loading.
I would suggest show the activity indicator from the Viewcontroller having the WebView and implement the WebView Delegate methods
- (void)webViewDidStartLoad:(UIWebView *)webView
{
[actvityIndicator startAnimating];
}
and
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[actvityIndicator stopAnimating];
}
this will keep your webview Interactive and also show the indicator till the page loads completely
you can try something like this. The Delegate is important in .h file
FocusViewController.h
#interface FocusViewController : UIViewController <UIWebViewDelegate> {
UIActivityIndicatorView *indicator;
}
FocusViewController.m
- (void)viewDidLoad
{
// Loading Indicator
indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[indicator setColor:[UIColor colorWithRed:50.0/255.0 green:120.0/255.0 blue:200.0/255.0 alpha:1.0]];
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
[indicator startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[indicator stopAnimating];
}

How to show loading image while loading the content of the web view in iPhone application

I have an url which need to access in web view and the url is accessing successfully. but the issue is time is taking more to load the url in webview and hence I need to show a loading image while loading an url. I am trying for that, but not able to get it to show loading bar, loading icon or whatever it may be.
And my code is here goes
NSLog(#"Response ==> %#" ,encodedString);
//'encodedstring' is my url which need to access
// code to show a loading image
indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
indicator.frame = CGRectMake(0, 0, 320, 470);
[self.view addSubview:indicator];
[indicator release];
[indicator startAnimating];
UIWebView *webView;
webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0, 320, 470)];
[webView setDelegate:self];
NSString* urlTwo = [[encodedString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
stringByReplacingOccurrencesOfString:#"%22" withString:#""];
NSURL *url2;
url2 = [[NSURL alloc] initWithString:urlTwo];
NSLog(#"url2:%#", url2);
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url2];
[webView loadRequest:requestObj];
[[self view] addSubview:webView];
}
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
[indicator stopAnimating];
[[self view] addSubview:webView];
}
-(void)webViewDidStartLoad:(UIWebView *)webView
{
[indicator startAnimating];
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
[webView stopLoading];
}
Can anybody is here to solve my issue and make the day happy. Thanks in advance.
-(void)webViewDidFinishLoad:(UIWebView *)aWebView
{
[indicator stopAnimating];
}
-(void)webViewDidStartLoad:(UIWebView *)aWebView
{
[indicator startAnimating];
}
-(void)webView:(UIWebView *)aWebView didFailLoadWithError:(NSError *)error
{
[activityView stopAnimating];
}
have u tried that?

Issue in UIActivityIndicatorView in iPhone?

Currently i am working in iphone app, Using UIWebView (Webpage shown in presentModelViewController) to show webpage on the screen, then i add UIActivityIndicatorView to show in load url request, but the UIActivityIndicatorView didn't show in the screen, i tried my level best, please help me.
Thanks in Advance
I tried this:
- (void)viewDidLoad
{
[super viewDidLoad];
activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activity.frame=CGRectMake(140, 240, 40, 40);
[self.view addSubview:activity];
web = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
web.delegate=self;
web.backgroundColor=[UIColor clearColor];
NSURL *url = [NSURL URLWithString:#"http://wrwr.rww.com/erqrrq"];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[web loadRequest:req];
[self.view addSubview:web];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[activity stopAnimating];
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
[activity startAnimating];
}
adding your activitiy indicatore View in to your webView like this:-
- (void)viewDidLoad
{
[super viewDidLoad];
activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activity.frame=CGRectMake(140, 240, 40, 40);
web = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
web.delegate=self;
web.backgroundColor=[UIColor clearColor];
NSURL *url = [NSURL URLWithString:#"http://wrwr.rww.com/erqrrq"];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[web loadRequest:req];
[web addSubview:activity];
[self.view addSubview:web];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[activity stopAnimating];
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
[activity startAnimating];
}
Try This,
- (void)viewDidLoad
{
[super viewDidLoad];
web = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
web.delegate=self;
web.backgroundColor=[UIColor clearColor];
NSURL *url = [NSURL URLWithString:#"http://wrwr.rww.com/erqrrq"];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[web loadRequest:req];
[self.view addSubview:web];
activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activity.frame=CGRectMake(140, 240, 40, 40);
[self.view addSubview:activity];
[self.view bringSubviewToFront:activity];
}
Define UIActivityIndicatorView after defining UIWebView.
- (void)viewDidLoad
{
[super viewDidLoad];
web = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
web.delegate=self;
web.backgroundColor=[UIColor clearColor];
NSURL *url = [NSURL URLWithString:#"http://wrwr.rww.com/erqrrq"];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[web loadRequest:req];
[self.view addSubview:web];
activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activity.frame=CGRectMake(140, 240, 40, 40);
[self.view addSubview:activity];
}
The order you add the sub views to the super view is significant. The UIActivityIndicatorView gets added and then the UIWebView overlays it. If you add the UIWebView first, i.e
[self.view addSubview:web];
[self.view addSubview:activity];
the UIActivityIndicatorView will overlay the UIWebView.

UIActivityIndicatorView not working on programmatically UIWebView in mySubView

My myViewController.h contains:
#import <UIKit/UIKit.h>
#interface myViewController : UIViewController {
UIScrollView *myScrollView;
UIView *mySubView;
UIWebView *myWebView;
UIActivityIndicatorView *myIndicator;
}
#property (nonatomic, retain) IBOutlet UIScrollView *myScrollView;
#end
My myViewController.m contains:
#import "myViewController.h"
#implementation myViewController
#synthesize myScrollView;
...
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *bgColors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor blueColor], nil];
for (int i = 0; i < bgColors.count; i++)
{
CGRect frameNew;
frameNew.origin.x = self.myScrollView.frame.size.width * i;
frameNew.origin.y = 0;
frameNew.size = self.myScrollView.frame.size;
mySubView = [[UIView alloc] initWithFrame:frameNew];
mySubView.backgroundColor = [bgColors objectAtIndex:i];
[self.myScrollView addSubview:mySubView];
myIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
myIndicator.center = CGPointMake(160, 100);
myIndicator.hidesWhenStopped = NO;
CGRect webFrame = CGRectMake(self.myScrollView.frame.size.width * i, 0, 320, 320);
myWebView = [[UIWebView alloc] initWithFrame:webFrame];
[myWebView setDelegate:(id)self];
[myWebView addSubview:myIndicator];
NSString *urlAddress = #"http://www.google.com/";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[myWebView loadRequest:requestObj];
[self.myScrollView addSubview:myWebView];
[myIndicator release];
[myWebView release];
[mySubView release];
}
self.myScrollView.contentSize = CGSizeMake(self.myScrollView.frame.size.width * bgColors.count, self.myScrollView.frame.size.height);
}
-(void)webViewDidStartLoad: (UIWebView *)webView
{
NSLog(#"Web view did start loading");
[myIndicator startAnimating];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
-(void)webViewDidFinishLoad: (UIWebView *)webView
{
NSLog(#"Web view did finish loading");
[myIndicator stopAnimating];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
[myIndicator stopAnimating];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
NSString* errorString = [NSString stringWithFormat:#"<html>%#</html>", error.localizedDescription];
[myWebView loadHTMLString:errorString baseURL:nil];
}
...
Load indicator does not work on the first two mySubView in myWebView, works only on the last mySubView in myWebView.
In the first two mySubView in myWebView, myIndicator displayed but not active.
Tell me what's wrong doing?
That is because myIndicator is pointing to the last activity indicator view. All previous references are lost. Luckily, since it is a subview of the web view, we can get a reference to it. This should help –
-(void)webViewDidStartLoad: (UIWebView *)webView
{
UIActivityIndicatorView *actView;
for ( id object in webView.subviews ) {
if ([object isMemberOfClass:[UIActivityIndicatorView class]]) {
actView = (UIActivityIndicatorView*)object;
}
}
NSLog(#"Web view did start loading");
[actView startAnimating];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
-(void)webViewDidFinishLoad: (UIWebView *)webView
{
UIActivityIndicatorView *actView;
for ( id object in webView.subviews ) {
if ([object isMemberOfClass:[UIActivityIndicatorView class]]) {
actView = (UIActivityIndicatorView*)object;
}
}
NSLog(#"Web view did finish loading");
[actView stopAnimating];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
UIActivityIndicatorView *actView;
for ( id object in webView.subviews ) {
if ([object isMemberOfClass:[UIActivityIndicatorView class]]) {
actView = (UIActivityIndicatorView*)object;
}
}
[actView stopAnimating];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
NSString* errorString = [NSString stringWithFormat:#"<html>%#</html>", error.localizedDescription];
[myWebView loadHTMLString:errorString baseURL:nil];
}

UIWebView leaking

I have a class that extends UIViewController and implements the UIWebViewDelegate, like this:
#interface TableViewController : UIViewController <UIWebViewDelegate, UIAlertViewDelegate>{
UIWebView *articleWebView;
NSString *link;
UIActivityIndicatorView *activityIndicator;
NSURL * safariUrl;
}
I'm trying to load a web page into the uiwebview, everything works fine, but i'm getting some leaks (like GeneralBlock-56, GeneralBlock-1024 etc) and i can't find out where they come from.
This is what i am doing:
- (void)viewDidLoad {
[super viewDidLoad];
if (articleWebView){
[articleWebView loadHTMLString: #"" baseURL: nil];
[articleWebView release];
articleWebView = nil;
}
articleWebView = [[UIWebView alloc] initWithFrame: CGRectMake(0,0,320,380)];
if (activityIndicator){
[activityIndicator release];
activityIndicator = nil;
}
activityIndicator = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(self.view.bounds.size.width/2-24, self.view.bounds.size.height/3-12, 50, 50)];
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
[self.view addSubview:activityIndicator];
link = [link stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
link = [link stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
link = [(NSString*) CFURLCreateStringByAddingPercentEscapes (NULL, (CFStringRef)link, NULL,
NULL, kCFStringEncodingUTF8) autorelease];
NSURL *url = [NSURL URLWithString:link];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[[self articleWebView] setDelegate: self];
[articleWebView loadRequest:requestObj];
[link release];
}
//////////////////////////////////////////////
-(void)webViewDidFinishLoad:(UIWebView *)webView{
if (activityIndicator){
[activityIndicator stopAnimating];
[activityIndicator release];
activityIndicator = nil;
self.view = articleWebView;
}
//////////////////////////////////////////////
- (void)viewDidUnload {
[super viewDidUnload];
[articleWebView loadHTMLString: #"" baseURL: nil];
[self.articleWebView release];
self.articleWebView = nil;
}
//////////////////////////////////////////////
- (void)dealloc {
self.articleWebView.delegate=nil;
[super dealloc];
}
What am i doing wrong? Thank you in advance.
Along with setting the webView delegate to nil, you should ask your webView to stop loading. Add the following lines of code and then try:
- (void)dealloc {
self.articleWebView.delegate = nil;
[articleWebView stopLoading];
[articleWebView release];
[super dealloc];
}
If articeWebview is an IBOutlet why is it that you want to allocate it??