Add NSURLConnection loading process on UIProgressView
In your didReceiveResponse function you could get the total filesize like so:_totalFileSize = response.expectedContentLength;
.
In your didReceiveData function you can then add up to a total bytes received counter:_receivedDataBytes += [data length];
Now in order to set the progressbar to the correct size you can simply do:MyProgressBar.progress = _receivedDataBytes / (float)_totalFileSize
(either in the didReceiveData function or somewhere else in your code)
Don't forget to add the variables that hold the number of bytes to your class!
I hope this helps..
EDIT: Here's how you could implement the delegates in order to update progressview
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
_totalFileSize = response.expectedContentLength;
responseData = [[NSMutableData alloc] init];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
_receivedDataBytes += [data length];
MyProgressBar.progress = _receivedDataBytes / (float)_totalFileSize;
[responseData appendData:data];
}
Add UIProgressView to a NSURLConnection?
In your didReceiveResponse
function you could get the total filesize like so -
_totalFileSize = response.expectedContentLength;
.
In your didReceiveData function you can then add up to a total bytes received counter -
_receivedDataBytes += [data length];
Now in order to set the progressbar to the correct size you can simply do -
MyProgressBar.progress = _receivedDataBytes / (float)_totalFileSize
(either in the didReceiveData
function or somewhere else in your code)
Don't forget to add the variables that hold the number of bytes to your class!
Here's how you could implement the delegates in order to update progressview
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
_totalFileSize = response.expectedContentLength;
responseData = [[NSMutableData alloc] init];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
_receivedDataBytes += [data length];
MyProgressBar.progress = _receivedDataBytes / (float)_totalFileSize;
[responseData appendData:data];
}
I hope this helps..
Set the progress to UIProgressView when upload with NSURLConnection
You should really read a C tutorial on numerical types. Presumably both totalBytesWritten
and totalBytesExpectedToWrite
are an integer type, so dividing them will result in truncation - that is, the fractional part of the result will be gone. Unless the result is at 100%, the integral part is always 0, so all those divisions will result in zero. Try casting one or both of the variables to float
or double
to get sensible results.
Also, UIProgressView
doesn't accept values between 0 and 100 by default but between 0 and 1. All in all, you should write
self.delegate.progressView.progress = ((float)totalBytesWritten / totalBytesExpectedToWrite);
and it should work fine.
Edit: the problem was that the data you were trying to upload was too small and it didn't need to be broken down to smaller chunks, so it was necessary to call this method only once. If you supply a great amount of data, then it will be able to be sent only in separate pieces, so the progress handler callback will be called multiple times.
How to use UIProgressView while loading of a UIWebView?
UIWebView doesn't give you any progress information in the normal mode. What you need to do is first fetch your data asynchronously using an NSURLConnection.
When the NSURLConnection delegate method connection:didReceiveResponse
, you're going to take the number you get from expectedContentLength
and use that as your max value. Then, inside the delegate method connection:didReceiveData
, you're going to use the length
property of the NSData instance to tell you how far along you are, so your progress fraction will be length / maxLength
, normalized to between 0.0 and 1.0.
Finally, you're going to init the webview with data instead of a URL (in your connection:didFinishLoading
delegate method).
Two caveats:
It is possible that the
expectedContentLength
property of the NSURLResponse is going to be -1 (NSURLReponseUnknownLength
constant). In that case, I would suggest throwing up a standard UIActivityIndicator that you shut off insideconnection:didFinishLoading
.Make sure that any time you manipulate a visible control from one of the NSURLConnection delegate methods, you do so by calling
performSelectorOnMainThread:
- otherwise you'll start getting the dreaded EXC_BAD_ACCESS errors.
Using this technique, you can show a progress bar when you know how much data you're supposed to get, and a spinner when you don't know.
add ui progress view ios
If you haven't decided on what kind of progress dialog to use yet, I recommend MBProgressHUD:
https://github.com/jdg/MBProgressHUD
It's fairly easy to use, and seems to be what you would need for this case. If you're set on making the request synchronous, at the desired point in your view lifecycle e.g. viewDidLoad:
you could have something like the following:
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
// synchronously pull down the necessary JSON
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"my web service "]];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
// perform the necessary view configuration you need to do based on this data
// .
// .
// .
[MBProgressHUD hideHUDForView:self.view animated:YES];
This will display a blocking spinner while your configuration is occurring and then clear it when the process is complete.
UIWbView multiple URL loading with UIProgressView in iPhone SDK
allocate each UIWebView with different url and and addSubView UIActivityIndicatorView to every UIWebView.Give tag for each UIActivityIndicator ..
-(void)action {
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Request Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
UIWebView *webView = [[UIWebView alloc]initWithFrame:frame];//give y value incremented frame.
UIActivityIndicatorView *av = [[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray] autorelease];
av.frame=CGRectMake(145, 160, 25, 25);
av.center = webView.center;
av.tag = INDICATOR_TAG;
[webView addSubview:av];
webView.delegate = self;
[av startAnimating];
//Load the request in the UIWebView.
[webView loadRequest:requestObj];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
UIActivityIndicatorView *indicator = [webView viewWithTag:INDICATOR_TAG];
indicator.hidden = YES;
}
call this action{} method for showing multiple times.
EDIT
UIWebView doesn't give you any progress information in the normal mode. What you need to do is first fetch your data asynchronously using an NSURLConnection. When the NSURLConnection delegate method connection:didReceiveResponse, you're going to take the number you get from expectedContentLength and use that as your max value. Then, inside the delegate method connection:didReceiveData, you're going to use the length property of the NSData instance to tell you how far along you are, so your progress fraction will be length / maxLength , normalized to between 0 and 1.
Finally, you're going to init the webview with data instead of a URL (in your connection:didFinishLoading delegate method).
Reference :
How to use UIProgressView while loading of a UIWebView?
Loading...Please wait for UIWebView - iPhone
If you're using UIWebView, there's not much fine-grained information you can get from it. If you look at the UIWebViewDelegate Protocol Reference, the only real callbacks you'll get are (1) asking if it should load the page, (2) when it starts loading the page, (3) when it finishes loading the page successfully, (4) when there was an error.
In other words, there is no delegate method indicating the incremental progress of the load.
Related Topics
Wkwebview Didn't Finish Loading, When Didfinishnavigation Is Called - Bug in Wkwebview
How to Deselect a Segment in Segmented Control Button Permanently Till Its Clicked Again
Modulename-Swift.H File Not Found in Xcode8
Convert String to Date Type in Swift 3
Autolayout Multiline Uilabel Cutting Off Some Text
Ios: Determine If Device Language Is Right to Left (Rtl)
iOS Playground Doesn't Show UI Preview
Firebase Dynamic Link Not Opening the App iOS
Swift - Instantiating a Navigation Controller Without Storyboards in App Delegate
iOS Steps to Create Custom Uitableviewcell with Xib File
Nsstring Containsstring Crashes
Swift - Uiimagepickercontroller - How to Use It
How to Set Uiviewcontroller "Extend Edges" Properties