Hinzufügen NSURLConnection laden-Prozess auf UIProgressView
Erstellte ich eine UIProgressView
. Aber ich NSTimer
zu UIProgressView's
Prozess . Jetzt muss ich integrieren UIProgressView
Prozess, wenn URL geladen wird. UIProgressView's
wird die Größe hängt von der NSURLConnection's
Daten.
Ich verwendete den folgenden code, um NSURLConnection
.
-(void)load {
NSURL *myURL = [NSURL URLWithString:@"http://feeds.epicurious.com/newrecipes"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myURL
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:60];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
responseData = [[NSMutableData alloc] init];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[connection release];
UIAlertView *alert = [[UIAlertView alloc] init];
[alert setTitle:@"Warning"];
[alert setMessage:@"Network Connection Failed?"];
[alert setDelegate:self];
[alert addButtonWithTitle:@"Yes"];
[alert show];
[alert release];
NSLog(@"Error");
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
responsetext = [[[NSString alloc] initWithData:responseData encoding: NSASCIIStringEncoding] autorelease];
}
InformationsquelleAutor Velmurugan | 2010-11-23
Du musst angemeldet sein, um einen Kommentar abzugeben.
In Ihrem didReceiveResponse Funktion können Sie die Gesamtgröße etwa so:
_totalFileSize = response.expectedContentLength;
.In Ihrem didReceiveData Funktion können Sie fügen Sie dann bis zu insgesamt empfangenen bytes-Zähler:
_receivedDataBytes += [data length];
Nun um die progressbar-Komponente die richtige Größe, Sie können einfach tun:
MyProgressBar.progress = _receivedDataBytes /(float)_totalFileSize
(entweder in der didReceiveData Funktion oder irgendwo anders im code)
Vergessen Sie nicht, fügen Sie die Variablen, die die Anzahl der bytes an, die Ihre Klasse!
Ich hoffe das hilft..
EDIT: Hier ist, wie Sie das umsetzen konnte den Delegierten, um zu aktualisieren progressview
didReceiveResponse und didReceiveData sind 2 verschiedene Personen. Es sollte: - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { totalFileSize=Reaktion.expectedContentLength; } und entfernen Sie die Zeile aus didReceiveData
Bearbeitet meine Antwort. Ich vergaß zu erwähnen, dass einer der Werte, die Sie teilen mit, sollte gegossen werden, um eine float. Da könnte man sonst am Ende mit einer ganzen Zahl, ohne float-Präzision.
B_vdl : vielen Dank für deine Antwort. mein problem ist gelöst. aber ich habe den code auch Bearbeiten. bitte überprüfen Sie und sagen Sie Ihren Befehl...
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { totalFileSize = Reaktion.expectedContentLength; responseData = [[NSMutableData alloc] init]; } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { receivedDataBytes += [Daten Länge]; NSLog(@"receivedDataBytes / (float)totalFileSize; = %2f",(1+((float)totalFileSize / (float)receivedDataBytes)*1000)); // MyProgressBar.Fortschritt = receivedDataBytes / (float)totalFileSize; [responseData appendData:data]; }
InformationsquelleAutor Hless