get UIWebView to load in CGRect correctly - iphone

I'm trying to get my webView to load correctly in the CGRect frame I set up, with the activityIndicator running.
It kind of loads correctly when I have it set up with [self.view addSubview:webView]; but the activityindicator doesnt appear. When I set it up with webView.delegate = self;the activityindicator does appear, but the webview covers up the whole screen when it loads up. I have a segmentedbar up top that gets covered up then.
- (void)viewDidLoad {
[super viewDidLoad];
CGRect webFrame = CGRectMake(0.0, 50.0, 320.0, 318.0);
activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[self.view addSubview:activityIndicator];
activityIndicator.frame = CGRectMake(250, 250, 30.0, 30.0);
self.view.center = CGPointMake(160.0f, 190.0f);
activityIndicator.center = self.view.center;
webView = [[UIWebView alloc] initWithFrame:webFrame];
[webView setBackgroundColor:[UIColor whiteColor]];
NSString *urlAddress = #"http://www.google.com";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
webView.delegate = self;
// [self.view addSubview:webView];
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
NSLog(#"activity started");
[activityIndicator startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(#"activity stopped");
[activityIndicator stopAnimating];
self.view = webView;
}

It should be
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(#"activity stopped");
[activityIndicator stopAnimating];
[[self view] addSubView:webView];
}
The views weren't being covered, they were being removed from the hierarchy by resetting the view property.

Related

After removeFromSuperview completes, a white screen is shown in place

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.

Unable to see UIActivityIndicator in UIWebView

I am new to iPhone developer,
I am loading pages in my webview, loading takes too much time so i wan to show ActivityIndicator till the page loads,
Here is my code snippet but it is not working,
activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator.frame = CGRectMake(200.0, 200.0, 100.0, 40.0);
activityIndicator.center = self.view.center;
[self.view addSubview: activityIndicator];
_webview=[[UIWebView alloc]init];
[_webview setBackgroundColor:[UIColor grayColor]];
[_webview setDelegate:(id<UIWebViewDelegate>)self];
[self.view addSubview:_webview];
[_webview bringSubviewToFront:activityIndicator];
...
- (void)webViewDidStartLoad:(UIWebView *)webView {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[activityIndicator startAnimating];
activityIndicator.hidden=FALSE;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[activityIndicator stopAnimating];
activityIndicator.hidden=TRUE;
}
but i am unable to see my activityIndicator
Any help will be appreciated.
You are adding the UIActivityIndicatorView directly to the self.view and later you are trying to bringSubviewToFront from the UIWebView. Check with:
[self.view bringSubviewToFront:activityIndicator];
Change [_webview bringSubviewToFront:activityIndicator]; to [self.view bringSubviewToFront:activityIndicator];
You can also change the order in which you add subviews. Add the webview first, and then add the activity indicator:
_webview=[[UIWebView alloc]init];
[_webview setBackgroundColor:[UIColor grayColor]];
[_webview setDelegate:(id<UIWebViewDelegate>)self];
[self.view addSubview:_webview];
activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator.frame = CGRectMake(200.0, 200.0, 100.0, 40.0);//you should also keep the height and width equal
activityIndicator.center = self.view.center;
[self.view addSubview: activityIndicator];
And get rid of the line that follows:[_webview bringSubviewToFront:activityIndicator];

How to customize UIWebView?

Actually i have one table view ,when Select rowAtIndexPath then it load my webView and play my video, how to return from UIWebView to my tableView ?
-(void)playButtonPressed1:(UIButton*)sender
{
Video *currentVideo= [[xmlParservideo videoNames] objectAtIndex:btn1.tag];
NSLog(#"content============== %#", [currentVideo content2]);
CGRect webFrame = CGRectMake(0.0, 0.0, 320.0, 460.0);
UIWebView *webView = [[UIWebView alloc] initWithFrame:webFrame];
[webView setBackgroundColor:[UIColor greenColor]];
NSString *urlString=currentVideo.content2;
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
[[self view] addSubview:webView];
// [self.navigationController popViewControllerAnimated:YES];
NSLog(#"jointed2 jjjjjjjjj %d",btn1.tag);
Place the Uiwebview in another view controller and push to it when a row is selected and when the video complete pop back to the tableview controller.
On didSelectRowAtIndexPath
videoViewController *newView = [videoViewController alloc]initWithNibName:#"videoViewController" bundle:nil];
[self.navigationController pushViewController:newView animated:YES];
and in your init method of the view controller create the webView programatically
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = #"Video Player";
UIWebView *videoWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 290)];
[videoWebView setDelegate:self];
NSString *urlAddress = #"http://www.google.com";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[videoWebView loadRequest:requestObj];
[[self view] addSubview:videoWebView];
}
return self;
}
Ok, Now just pop your view after completing your video
[self.navigationController popViewControllerAnimated: NO];
A workaround that may work is using this: [webView setHidden:YES];
This hides the webView from the view, so use that line of code whenever you want the webview to disappear. When you want it to return to the view, use this: [webView setHidden:NO];

uiwebview without interface builder

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];
}

Three20 doesn't show website in UIWebView

Hi all
I have a tabbar based application. In one view I have a TTTabStrip with different TTTabItems. Everything works (loading normal UIViewControllers) except loading UIWebViews.
In the app delegate I have set up the url mapping:
TTNavigator* navigator = [TTNavigator navigator];
navigator.supportsShakeToReload = YES;
navigator.persistenceMode = TTNavigatorPersistenceModeAll;
TTURLMap* map = navigator.URLMap;
[map from:#"*" toViewController:[TTWebController class]]; // default klasse wenn nichts anderes gewählt ist.
[map from:#"tt://test" toViewController:[TestController class]];
The test controller contains one UIWebView. I can see (NSLog) that the loadView-Method gets called, but the URL is not being opened:
- (void) loadView {
CGRect applicationFrame = [UIScreen mainScreen].applicationFrame;
self.view = [[[UIView alloc] initWithFrame:applicationFrame] autorelease];
//self.view.backgroundColor = [UIColor blackColor];
NSURL *theURL = [NSURL URLWithString:#"http://www.google.com"];
[webView loadRequest:[NSURLRequest requestWithURL:theURL]];
// support zooming
webView.scalesPageToFit = YES;
}
For instance, I can change the background of the UIWebView to black.
How can I load google in the web view below the TTTabStrip?
Thanks a lot.
Try the following and add the method viewWillAppear:
- (void)loadView
{
CGRect applicationFrame = [UIScreen mainScreen].applicationFrame;
UIView * newView = [[UIView alloc] initWithFrame:applicationFrame];
// support zooming
webView.scalesPageToFit = YES;
[newView addSubview:webView];
self.view = newView;
[newView release];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSURL *theURL = [NSURL URLWithString:#"http://www.google.com"];
[webView loadRequest:[NSURLRequest requestWithURL:theURL]];
}