got a little problem with MBProgressHUD. I have a webview and want to display the HUD while the data is loading.
The HUD appears but stays only for a few seconds and disappears already but the webview didn't finish with loading.
-(void)viewDidAppear:(BOOL)animated{
// Should be initialized with the windows frame so the HUD disables all user input by covering the entire screen
HUD = [[MBProgressHUD alloc] initWithWindow:[UIApplication sharedApplication].keyWindow];
// Add HUD to screen
[self.view.window addSubview:HUD];
// Regisete for HUD callbacks so we can remove it from the window at the right time
HUD.delegate = self;
HUD.labelText = #"Loading";
HUD.detailsLabelText = #"updating data";
// Show the HUD while the provided method executes in a new thread
[HUD showWhileExecuting:#selector(loadingWebView) onTarget:self withObject:nil animated:YES];}
- (void) loadingWebView {
NSString *fullURL = beverageViewString;
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[beverageView loadRequest:requestObj];
NSLog(#"%#",beverageViewString);}
remove the showWhileExecuting method and hide hud in the below delegate method of UIWebView then it will work fine
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[HUD hide:YES];
}
We never integrated web view with MBProgressView HUD , instead of using this you should to use UIActivityIndicator over here and stop & resignFromSuperView at this delegate of webView:
(void)webViewDidFinishLoad:(UIWebView *)webView
u can manually hide HUD at this delegate :
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[HUD hide:YES];
if(HUD!=nil)
{
[HUD removeFromSuperview];
[HUD release];
HUD=nil;
}
}
Related
So for my application, I add a view (aView) on top of my current view (classesWebView) as a Subview. All the aView is, is a UIView with a UIActivityIndicatorView on top of it that is supposed to animate while the view underneath (classesWebView) loads the appropriate Web Page.
I can see that the classesWebView webpage does appear (aView has an alpha of .5), but as soon as it finishes loading all the way, aView is sent [aView removeFromSuperview] and it disappears but after it goes away, all that's left is a white screen in it's place.
I have done this for two other methods and I don't know why, on only this method, it refuses to cooperate.
Any suggestions would be appreciated. The App is for iOS 6.
viewDidLoad method:
-(void)viewDidLoad
{
[super viewDidLoad];
classesWebView.delegate = self;
[classesWebView addSubview:aView];
NSURL *class = [NSURL URLWithString:#"mywebistelink"];
NSURLRequest *classRequest = [NSURLRequest requestWithURL:class];
[classesWebView loadRequest:classRequest];
}
webViewDidStartLoad method:
preView and switchView are Activity Indicators.
- (void)webViewDidStartLoad:(UIWebView *) webview
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
if(aView.superview != nil)
{
[preView startAnimating];
}
else
{
[switchView startAnimating];
}
}
webViewDidFinishLoad method:
- (void)webViewDidFinishLoad:(UIWebView *) webview
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
if(aView.superview != nil)
{
[aView removeFromSuperview];
[preView stopAnimating];
[preView setOpaque:false];
}
else
{
[switchView stopAnimating];
[switchView setOpaque:false];
}
}
[Further clarity: The reason I have the if-statement, is because I want another indicator for the classesWebView when loading pages but I do NOT want it to appear unless aView is gone (since aView already has it's on indicator: preView)]
EDIT: Just to prove that it is ONLY THE removeFromSuperview that is causing the problem, if I call [aView setAlpha:0.0] it disappears and the webPage below it loads properly. But the second that I call [aView removeFromSuperview] the web page turns into a white screen. T_T
try this:
NSURL *class=[NSURL URLWithString:[yourwebistelink stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURLRequest *request=[NSURLRequest requestWithURL:class];
[self.classesWebView loadRequest:request];
try this one.. and try to dont add aView for activityIndicator., try this this one for indicating activityIndicator
UIWebView * classesWebView = [[UIWebView alloc] init];
[classesWebView addSubview:activityIndicator];
[self.activityIndicator startAnimating]; //for activityIndicator
NSURLRequest *request = [NSURLRequest requestWithURL:weburl];
classesWebView.frame = CGRectMake(0, 200, 768, 400);
[classesWebView setScalesPageToFit:NO];
[classesWebView loadRequest:request];
- (void)webViewDidStartLoad:(UIWebView *)thisWebView
{
}
- (void)webViewDidFinishLoad:(UIWebView *)thisWebView
{
[self.activityIndicator stopAnimating];
[activityIndicator removeFromSuperview];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
NSLog(#"Error : %#",error);
}
if still webView is displaying blank white, then you have to encode your url by this one
NSString *encodedString=[graphStringUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *weburl = [NSURL URLWithString:encodedString];
NSURLRequest *request = [NSURLRequest requestWithURL:weburl];
then load this request in webView
[classesWebView loadRequest:request];
I don't think you should be adding subviews to a UIWebView. Try adding aView to the view that contains your classesWebView and see if that fixes the problem.
So I never got it to work but I cheated it by just setting [aView setAlpha:0]. That worked for what I needed. Thanks for the suggestions though, guys.
i want to hide MBProgressHUD when i'm receive xml response from server and i use httpconnection to get xml from server,,anybody help me?? thx before..
follow this steps to hide the ProgressHUD
take a class level variable for hud
MBProgressHUD *hud;
then make two functions
-(void)showProgress
{
if (!hud)
hud = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:hud];
//hud.delegate = self;
hud.labelText = #"Loading...";
[hud show:YES];
}
-(void)hideProgress
{
[hud hide:YES];
[hud removeFromSuperview];
[hud release];
hud=nil;
}
call showProgress() when you initiate the network hit
and call hideProgress() when callback of success or failure called.
You should retain an instance of MBProgressHUD in the class or the delegate that is getting the data from the server,
in the interface
{
MBProgressHUD *hud;
}
do [hud show..] when the progress starts and
[hud hide] when it finishes.
I think you use HUD delgete for hideen
try this:-
-(void)hudWasHidden // for remove the hud
{
[HUD removeFromSuperview];
}
call the Hud
HUD = [[MBProgressHUD alloc] initWithView:self.view];
HUD.graceTime = .1;
HUD.navigationBar = self.navigationController.navigationBar;
HUD.labelFont = [UIFont fontWithName:#"Arial" size:14];
HUD.delegate = self;
[self.view addSubview:HUD];
[HUD showWhileExecuting:#selector(yourFunction name:) onTarget:self withObject:nil animated:YES];
in .h File use this
MBProgressHUD *HUD;
In here said that using MBProgressHUD is easy.
But I can't make it.
Here my code:
- (IBAction)save{
HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
[self.navigationController.view addSubview:HUD];
HUD.delegate = self;
[HUD show:YES];
NSString *title = [page stringByEvaluatingJavaScriptFromString:#"document.title"];
SavePageAs *savePage = [[SavePageAs alloc] initWithUrl:self.site directory:title];
[savePage save];
[HUD hide:YES];
}
The progress indicator is not showing during savePage save method run, but shows after the page completely saved (the indicator is shown for less than 1 second).
I also tried this way:
- (IBAction)save {
HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
[self.navigationController.view addSubview:HUD];
HUD.delegate = self;
HUD.labelText = #"Saving...";
[HUD showWhileExecuting:#selector(performFetchOnMainThread) onTarget:self withObject:nil animated:YES];
}
- (void) savingPage{
NSString *title = [page stringByEvaluatingJavaScriptFromString:#"document.title"];
SavePageAs *savePage = [[SavePageAs alloc] initWithUrl:self.site directory:title];
[savePage save];
}
-(void) performFetchOnMainThread {
[self performSelectorOnMainThread:#selector(savingPage) withObject:nil waitUntilDone:YES];
}
But still no luck. Anyone let me know where I'm lack here?
P.S: savePage save is saving all website contents to local directory. I wish once the saving is complete the progressHUD disappear.
Thanks
Try allocation of HUD on the viewWillAppear: instead of -(IBAction)save because sometimes allocation takes up the whole time and by the time it allocates whole task is finished.
Copy Following links to viewWillAppear: and remove them from -(IBAction)save
HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
[self.navigationController.view addSubview:HUD];
HUD.delegate = self;
HUD.labelText = #"Saving...";
EDIT: Keep allocations on viewWillAppear: and change code as shown below:
- (IBAction)save {
[NSThread detachNewThreadSelector:#selector(showHUD) withObject:nil];
[self performSelectorOnMainThread:#selector(savingPage) withObject:nil waitUntilDone:YES];
}
- (void) savingPage{
NSString *title = [page stringByEvaluatingJavaScriptFromString:#"document.title"];
SavePageAs *savePage = [[SavePageAs alloc] initWithUrl:self.site directory:title];
[savePage save];
[HUD hide:YES];
}
-(void)showHUD {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[HUD show:YES];
[pool release];
}
What this will do is create a separate thread to display HUD as main thread is being engaged by savingPage method.
If this too doesn't work, then just change waitUntilDone:YES to waitUntilDone:NO
Note: As per Apple documentation
Important If you use Automatic Reference Counting (ARC), you cannot
use autorelease pools directly. Instead, you use #autoreleasepool
blocks instead. For example, in place of:
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init;
// Code benefitting from a local autorelease pool.
[pool release];
you would write:
#autoreleasepool {
// Code benefitting from a local autorelease pool.
}
#autoreleasepool blocks are more efficient than using an instance of
NSAutoreleasePool directly; you can also use them even if you do not
use ARC.
Hope this helps.
check out this code
- (IBAction)save{
HUD = [[MBProgressHUD alloc] initWithView:self.view];
HUD.graceTime = .1;
HUD.navigationBar = self.navigationController.navigationBar;
HUD.labelText = #"Saving";
HUD.delegate = self;
[self.view addSubview:HUD];
[HUD showWhileExecuting:#selector(savingPage) onTarget:self withObject:nil animated:YES];
}
- (void) savingPage{
NSString *title = [page stringByEvaluatingJavaScriptFromString:#"document.title"];
SavePageAs *savePage = [[SavePageAs alloc] initWithUrl:self.site directory:title];
[savePage save];
}
when saveingpage method will be completed MBProgressHUD delegate will be called so implement this delegate method in your class,This will remove HUD from your view
-(void)hudWasHidden
{
[HUD removeFromSuperview];
}
Try changing the HUD allocation lines to
HUD = [MBProgressHUD showHUDAddedTo: self.navigationcontroller.view animated:YES];
HUD.labelText....
And then in your saving method you can have
[MBProgressHUD hideHUDForView:self.navigationcontroller.view animated:NO];
I have the following method for a MBProgressHUD:
[progressHUD performSelector:#selector(hide:)
withObject:[NSNumber numberWithBool:YES]
afterDelay:kMessageHidingDelay];
the delay is 2.0 here, however it's not calling hide after 2.0 seconds. I tried to put a breakpoint in the hide function and it's not getting there. Any idea? Here's the full code:
progressHUD = [[MBProgressHUD alloc] initWithView:viewToAttach];
// Add HUD to screen
[viewToAttach addSubview:progressHUD];
progressHUD.labelText = #"Logging In";
progressHUD.removeFromSuperViewOnHide = YES;
// Show the HUD while the provided method executes in a new thread
[progressHUD show:YES];
May be try to perform selector on Main thread (all UI changes must be done on main thread)?
performSelectorOnMainThread:
you have to hide the MBProgressHud
[progressHUD hide:YES];
To Show MBProgressHUD Use this Code:-
HUD = [[MBProgressHUD alloc] init];
[self.view addSubview:HUD];
HUD.delegate = self;
[HUD showWhileExecuting:#selector(myTask) onTarget:self withObject:nil animated:YES];
where myTask is
- (void)myTask
{
"Your Code"
}
And too Hide the MBProgressHud
- (void)hudWasHidden:(MBProgressHUD *)hud
{
// Remove HUD from screen when the HUD was hidded
[HUD removeFromSuperview];
[HUD release];
HUD = nil;
}
And If you Want to Show Hud With Your CostomView Then Use This Code
HUD = [[MBProgressHUD alloc] init];
[self.view addSubview:HUD];
// Make the customViews 37 by 37 pixels for best results (those are the bounds of the build-in progress indicators)
HUD.customView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Your Image Name.png"]] autorelease];
// Set custom view mode
HUD.mode = MBProgressHUDModeCustomView;
HUD.delegate = self;
HUD.labelText = #"Completed";
[HUD show:YES];
[HUD hide:YES afterDelay:3];
}
I am loading some file from document directory using UIWebView. I have set the delegate of UIWebView and I am responding to 2 methods of delegate that is
webViewDidStartLoad and webViewDidFinishLoad I am receiving the webViewDidStartLoad But I am not receiving webViewDidFinishLoad method.
Below is the code:
#interface MyView: UIViewController <UIWebViewDelegate> {
UIWebView *webView;
}
#property (nonatomic, retain) UIWebView *webView;
========================= Class ===========================
-(void)viewDidLoad {
CGRect webFrame = [[UIScreen mainScreen] applicationFrame];
mWebView = [[UIWebView alloc] initWithFrame:webFrame];
mWebView.delegate = self;
mWebView.scalesPageToFit = YES;
[self.view addSubview:mWebView];
NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#", pathString]];
[mWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]] ];
}
// Delegate methods
-(void)webViewDidStartLoad:(UIWebView *)webView {
NSLog(#"start");
}
-(void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(#"finish");
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
NSLog(#"Error for WEBVIEW: %#", [error description]);
}
Please let me know what is going wrong. I am not getting any error in didFailLoadWithError delegate method.
Note:- the file that I am loading are huge say 3 MB.
Thanks
=============EDITED==================
As I was loading very huge File the delegate was coming after very long duration that I was not able to notice but for small files everything is working fine
Hey probably you should do this,
-(void)viewDidLoad {
//webView alloc and add to view
CGRect webFrame = [[UIScreen mainScreen] applicationFrame];
mWebView = [[UIWebView alloc] initWithFrame:webFrame];
mWebView.delegate = self;
mWebView.scalesPageToFit = YES;
[self.view addSubview:mWebView];
//path of local html file present in documentsDirectory
NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#", pathString]];
//load file into webView
[mWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]] ];
//show activity indicator
[self showActivityIndicator]
}
Call removeLoadingView method in the following UIWebViewDelegate methods
-(void)webViewDidFinishLoad:(UIWebView *)webView {
[self removeLoadingView];
NSLog(#"finish");
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
[self removeLoadingView];
NSLog(#"Error for WEBVIEW: %#", [error description]);
}
The showActivityIndicator method
-(void) showActivityIndicator
{
//Add a UIView in your .h and give it the same property as you have given to your webView.
//Also ensure that you synthesize these properties on top of your implementation file
loadingView = [UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]
loadingView.alpha = 0.5;
//Create and add a spinner to loadingView in the center and animate it. Then add this loadingView to self.View using
[self.view addSubView:loadingView];
}
The removeLoadingView method
-(void) removeLoadingView
{
[loadingView removeFromSuperView];
}
Well, does your webview actually finish loading? You should implement the webView:didFailLoadWithError:, too to catch failures.
Since you're not getting either the failure or success message. There might be something wrong with the data that you're trying to load.
Try telling the webView to load an HTML string ("Hello World!"), and see if that succeeds. If it does, then the problem is with your data resource or the path to it.
WebView Delegates
Download MBProgessHUD
Add to your Project
When the page begins load loading (*) progess started and hide after page loaded successfully
.h file
#interface WebViewViewController : UIViewController <MBProgressHUDDelegate, UIWebViewDelegate>
{
MBProgressHUD *HUD;
}
.m File
- (void)webViewDidStartLoad:(UIWebView *)webView
{
HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
[self.navigationController.view addSubview:HUD];
HUD.delegate = self;
HUD.labelText = #"Loading";
[HUD show:YES];
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
[HUD hide:YES];
}
Probably it is experiencing an error. Implement –webView:didFailLoadWithError: and check.
Make sure to implement the delegate ( ) in the view's .h file and connect the delegate to the view. This fixed it for me.
.h file
#property (retain, nonatomic) IBOutlet UIWebView *webview;
.m file
-(void)viewDidLoad {
NSString *urlAddress = #"YOUR_URL";
_webview.scalesPageToFit = YES;
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[_webview loadRequest:requestObj];
}
- (void)webViewDidStartLoad:(UIWebView *)webView;
{
[self.indicater_web startAnimating];
}
//a web view starts loading
- (void)webViewDidFinishLoad:(UIWebView *)webView;
{
[self.indicater_web stopAnimating];
}
//web view finishes loading
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error;
{
[self.indicater_web stopAnimating];
}
- (BOOL)webView:(UIWebView *)wv shouldStartLoadWithRequest:(NSURLRequest *)rq
{
[self.indicater_web startAnimating];
return YES;
}