UIActivityIndicator problem

Joined
Jun 3, 2011
Messages
54
Reaction score
0
Points
6
Dear All,

In my spliview based app, once I tap the file name located on main view panel, one jsp (that lies on some server) is called and then jsp returns the file location on server. Then I create NSURLConnection object to stram the file from that location and save the file to application document directory. Once streaming is completed, I pass the file's local location (as id type) to detailitem in detilviewcontroller. And then file is viewed in uiwebview in detailview.
Everything is happening properly.

But what I want is that as soon as jsp is called and while it takes time to stream the file locally , activityindicator should rotate in detailview, so that user has ad idea that something is happening.And as soon as file is loaded in webview (after file is streamed and available locally), that indicator is stopped.


I am sure this has to do something with delegat method of NSURLConnection, but I am unable to implement it properly. Also, I know the exact file size as soon as I tapped the file (Is that of any use?? May be If I use progressbar).


Part of code is:

- (void)tableView: (UITableView *)tableView didSelectRowAtIndexPath:
(NSIndexPath *)indexPath {

NSString *post = [NSString stringWithFormat:mad:"DocId=%@",docid];
NSData *postdata = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSMutableURLRequest *request= [[[NSMutableURLRequest alloc] init] autorelease];
file_expected_size=[[file_size_list objectAtIndex:indexPath.row] longLongValue];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:mad:"%@%@",DV_instance,@"/jsp/DV3_CreateFileLink_iPad.jsp"]]];
[request setHTTPMethod:mad:"POST"];
[request setHTTPBody: postdata];

NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];

if(conn){

NSURLResponse *response;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
NSString *serveroutput= [[NSString alloc]initWithData:data encoding:NSASCIIStringEncoding];
NSString *stringurl = [serveroutput stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

NSURL *url_for_filetapped_on_Server = [NSURL URLWithString:stringurl];
NSData *data_of_file = [NSData dataWithContentsOfURL:url_for_filetapped_on_Server];



// Then I determine the local file path (which is relative_local_file_full_path) and then

detailview.detailItem =relative_local_file_full_path; (where detailview is an object of class DetailViewController)

}
}

Any idea is appreciated!!
Thanks
 
Joined
Aug 13, 2011
Messages
200
Reaction score
7
Points
18
Location
West Sussex
I would first create an IBOutlet variable for the UIActivityIndicatorView class, in the header
file for your controller class, and add the NSURLConnection protocol attribute to the class,
like this.

Code:
@interface className : NSObject  <NSURLConnectionProtocol>
{
        IBOutlet  UIActivityIndicatorView  *activityIndicator ;
}

Then connect the ActivityIndicatorView in your xib file to this outlet, also set the hidden
property to true, and the hides when stopped property to true, using the attributes inspector.

then you would call the startAnimating: method on the activityIndicator variable, from the
method you use to start the NSURLConnection request, like this.

Code:
- (void)startConnection
{
      [activityIndicator startAnimating] ;
}

And use the stopAnimating: method of the activityIndicator, from the method you use to
verify the connection data has been recieved, like this.

Code:
- (void)stopConnection
{
      [activityIndicator stopAnimating] ;
}

In iOS 4.3 the NSURLConnection has a connectionDidFinishLoading : method, and a
delegate method connection : didFailWithError : , which you could use to stop the
activity indicator in either case, but I'm not sure if these methods are still available in iOS 5.

Check out the developer documentation, on the NSURLConnection class, and delegate
methods to find the appropriate methods for checking the completed download.

This guide is probably worth reading as well.

https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/URLLoadingSystem/URLLoadingSystem.html#//apple_ref/doc/uid/10000165i

Hope this is of some help.

Regards Mark
 
OP
S
Joined
Jun 3, 2011
Messages
54
Reaction score
0
Points
6
Thanks for quick reply but I am not creating GUI using interface builder rather I m making splitviewer programatically.

So, when I tap a file in mainview, activityindicator should start in detailview.
So, when NSURLConnection class and delegates are being used in mainview class then how can I relate this with the activity indicator which is in detailview?

Thanks
 
Joined
Aug 13, 2011
Messages
200
Reaction score
7
Points
18
Location
West Sussex
The UIActivityIndicatorView is a subclass of UIView, so you can create the activity indicator
with code like this.

Code:
//in your header file

@interface yourView : UIView
{
        UIActivityIndicatorView *indicatorView;
}

//in the appropriate method in your view controller implementation file

    indicatorView = [[UIActivityIndicatorView alloc]     initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    [indicatorView setHidden:NO];
    [indicatorView hidesWhenStopped];
    [indicatorView startAnimating];
    [self addSubview:indicatorView];
    [self setNeedsDisplay];
    [indicatorView release];

You will have to set the position of the indicatorView in the super view, in the same way
you would with any view object, for the example, above I simply tested the code in a UIView
that I dragged on to a blank window application, so did not bother with the position.

Also you will have to stop the UIActivityIndicatorView in the appropriate method like this.

Code:
[indicatorView stopAnimating];

Hope this helps.

Regards Mark
other view object
 
OP
S
Joined
Jun 3, 2011
Messages
54
Reaction score
0
Points
6
Thanks for the reply.
I got the problem. It seems that indicator placement is not possible with synchronous nsurlconnection.
While I changed it to asynchronous, I could properly placed, started and stopped the indicatorview.

Thanks a ton.
 
Joined
Aug 13, 2011
Messages
200
Reaction score
7
Points
18
Location
West Sussex
Yes, it's worth mentioning to others that using the NSURLConnection class in a synchronous mode, will
freeze the client application until the connection completes it's data transfer.
So if you need to make sure your app can continue to update it's UI, then use the NSURLConnection class
in a asynchronous mode.

Great to here you got things working.

Regards Mark
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top