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.
Related
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;
}
}
I've to switch a part of my app: I will implement a web view insread of a picture (imageview).
So I tried to make it with the interface builder but there is too many errors so I prefer to use code!
for the pictures, I user those lines :
image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
self.imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0,64,320,367);
[self.view addSubview:imageView];
[self.imageView release];
Now, I tried to use this code but nothing's showed:
#property (nonatomic, retain) IBOutlet UIWebView *webView;
...
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.google.fr"]]];
[self.view addSubview:webView];
If you know how to make .... would be nice for me,
THANKS!
Declare in your h file:
UIWebView *contentWebView;
And also tell to your viewController that you will implement <UIWebViewDelegate>
In viewDidLoad Add:
contentWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
contentWebView.delegate = self;
contentWebView.multipleTouchEnabled = YES;
contentWebView.scalesPageToFit = NO;
[self.view addSubview:contentWebView];
In dealloc don't forget to release the webView:
[contentWebView release];
And implement the delegate methods:
#pragma mark -
#pragma mark WebViewDelegates
- (void)webViewDidStartLoad:(UIWebView *)webView{
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
}
And also you need to load an request. You can doit in viewDidLoad after you have added the webView to your view:
[contentWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.apple.com/"]]];
adjust the frame as u needed and if add anything like imageview as subview to webview then u have to use bringsubviewtofront function.
- (void) initUIWebView {
NSLog(#"DetailViewController->initUIWebView {");
UIWebView *aWebView;
// init and create the UIWebView
aWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 400)];
aWebView.autoresizesSubviews = YES;
aWebView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
//set the web view delegates for the web view to be itself
[aWebView setDelegate:self];
//Set the URL to go to for your UIWebView
NSString *urlAddress = #”http://www.google.com”;
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//load the URL into the web view.
[aWebView loadRequest:requestObj];
//add the web view to the content view
[self.view addSubview:aWebView];
[aWebView release], aWebView = nil;
}
- (void)viewDidLoad {
[self initUIWebView]; // <<<<<<<<<<<< this calls the UIWebView Function
[super viewDidLoad];
}
I have a refresh button on my navigationbar
buttonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(buttonItemClicked)];
self.navigationItem.rightBarButtonItem = buttonItem;
-(void)buttonItemClicked{
NSLog(#"buttonItemclicked");
myView.labelName.text = nil;
myView.otherLabelName.text = nil;
[spinner startAnimating]
[spinnerView setHidden:NO];
[self requestAPI];
[spinner stopAnimating];
[spinnerView setHidden:YES];
}
If I go in and out of the view, it works fine. But when I call the same methods in buttonItemClicked, it doesn´t work. I also tried calling the view methods inside my action method, but that doesn´t work either.
What I´m trying to do is set my labels to nil, add my UIActivityIndicatorView and remove it after the labels are set again.
I have already tried [self.view setNeedsDisplay];
The refresh it self works, but the animations doesn´t work.
Any suggestions?
The animation is not working because you call startAnimating and stopAnimating (and setHidden) in the same method.
The render start at the end of the method call.
You need to set
[spinner stopAnimating];
[spinnerView setHidden:YES];
in requestAPI.
Edit:
Using Grand Central Dispatch. Like:
- (void)buttonItemClicked {
myView.labelName.text = nil;
myView.otherLabelName.text = nil;
[spinner startAnimating]
[spinnerView setHidden:NO];
[self requestAPI];
}
- (void)requestAPI {
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSURL *url = [NSURL URLWithString:#"http://example.com"];
NSData *data = [NSData dataWithContentsOfURL:url];
NSString *stringResult = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
dispatch_async(dispatch_get_main_queue(), ^{
[spinner stopAnimating];
[spinnerView setHidden:YES];
myView.labelName.text = stringResult;
});
});
}
Try [myView setsNeedToDisplay];.
I have a refresh button on my navigationbar
buttonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(buttonItemClicked)];
self.navigationItem.rightBarButtonItem = buttonItem;
-(void)buttonItemClicked{
NSLog(#"buttonItemclicked");
myView.labelName.text = nil;
myView.otherLabelName.text = nil;
[spinner startAnimating]
[spinnerView setHidden:NO];
[self requestAPI];
[spinner stopAnimating];
[spinnerView setHidden:YES];
}
If I go in and out of the view, it works fine. But when I call the same methods in buttonItemClicked, it doesn´t work. I also tried calling the view methods inside my action method, but that doesn´t work either.
What I´m trying to do is set my labels to nil, add my UIActivityIndicatorView and remove it after the labels are set again.
I have already tried [self.view setNeedsDisplay];
The refresh it self works, but the animations doesn´t work.
Any suggestions?
The animation is not working because you call startAnimating and stopAnimating (and setHidden) in the same method.
The render start at the end of the method call.
You need to set
[spinner stopAnimating];
[spinnerView setHidden:YES];
in requestAPI.
Edit:
Using Grand Central Dispatch. Like:
- (void)buttonItemClicked {
myView.labelName.text = nil;
myView.otherLabelName.text = nil;
[spinner startAnimating]
[spinnerView setHidden:NO];
[self requestAPI];
}
- (void)requestAPI {
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSURL *url = [NSURL URLWithString:#"http://example.com"];
NSData *data = [NSData dataWithContentsOfURL:url];
NSString *stringResult = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
dispatch_async(dispatch_get_main_queue(), ^{
[spinner stopAnimating];
[spinnerView setHidden:YES];
myView.labelName.text = stringResult;
});
});
}
Try [myView setsNeedToDisplay];.
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;
}