views:

95

answers:

2

Hello, I would like to know how to do the next:

I have one app that make some connections to receive datas (this datas are show in the screen)

I would like to know wich code i can use to wait each connection, I mean, the time between the connection start and this finish i would like to show in the screen one spin (Loading...).

I have created the connections, and the spin. My problem is that i don't know wich code i can use to manage this and where to write this code.

Thanks!

A: 

You should look at using NSURLConnection with a delegate. An NSURLConnection lets you get data from a server asynchronously (it runs in the background, and notifies the delegate when certain events happen).

Then, in your view controller class, you can start the spinner right before you launch the connection, and have one of your delegate methods stop the spinner when the connection finishes.

Tim
can you show me this with one example please? I do not know well how to create a delegate.
The code you posted in your answer looks good - now all you have to do is give the Connection class an instance of your view controller, which should `@synthesize` the spinner. Then, right before you call `[theConnection start]`, start your spinner, and in `connectionDidFinishLoading:`, stop it again.
Tim
How do i create the instance? should i create in Connection.h an object 'view controller'??
Yep - if your view controller is of class MyViewController, add an instance variable in Connection.h that's something like `MyViewController *controller;`, then just set that either in your Connection's `init` method or right after you initialize it from the controller: `connection.controller = self;`
Tim
Thanks, that's running well:)I would like to know ( because my app has tab bar items) how to reload the view for the tab bar i'm clicking on (after the loading view dissapear)Thanks
A: 

this is my code in the class: Connection.h, and i create an object Connection in every class where i want to call a new connection to get data (i don't know then if this is the correct way to do)

import "Connection.h" import "XMLParser.h"

@implementation Connection @synthesize webData, soapResults, xmlParser;

-(Connection *) Init:(NSInteger *) methodNumber{ [super init]; methodNum = methodNumber; return self; }

-(void)Connect:(NSString *) soapMessage{

NSLog(soapMessage);

NSURL *url = [NSURL URLWithString:@"http://.....?WSDL"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];

[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

//NSURLConnection *theConnection =  [NSURLConnection connectionWithRequest:theRequest delegate:self];

NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
[theConnection start];

if( theConnection )
{
 webData = [[NSMutableData data] retain];
}
else
{
 NSLog(@"theConnection is NULL");
}

}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { [webData setLength: 0]; }

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [webData appendData:data]; }

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { NSLog(@"ERROR with theConenction"); [connection release]; [webData release]; }

-(void)connectionDidFinishLoading:(NSURLConnection *)connection {

NSLog(@"DONE. Received Bytes: %d", [webData length]);

NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];

NSLog(theXML);
[theXML release];




if( xmlParser )
{
 [xmlParser release];
}

xmlParser = [[NSXMLParser alloc] initWithData: webData];

//Initialize the delegate.
XMLParser *parser = [[XMLParser alloc] initXMLParser:methodNum];

//Set delegate
[xmlParser setDelegate:parser];


//[xmlParser setDelegate: self];
[xmlParser setShouldResolveExternalEntities: YES];
[xmlParser parse];

[connection release];
[webData release];

}

@end