Currently i am working in iphone app, Using UIWebView (Webpage shown in presentModelViewController) to show webpage on the screen, then i add UIActivityIndicatorView to show in load url request, but the UIActivityIndicatorView didn't show in the screen, i tried my level best, please help me.
Thanks in Advance
I tried this:
- (void)viewDidLoad
{
[super viewDidLoad];
activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activity.frame=CGRectMake(140, 240, 40, 40);
[self.view addSubview:activity];
web = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
web.delegate=self;
web.backgroundColor=[UIColor clearColor];
NSURL *url = [NSURL URLWithString:#"http://wrwr.rww.com/erqrrq"];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[web loadRequest:req];
[self.view addSubview:web];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[activity stopAnimating];
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
[activity startAnimating];
}
adding your activitiy indicatore View in to your webView like this:-
- (void)viewDidLoad
{
[super viewDidLoad];
activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activity.frame=CGRectMake(140, 240, 40, 40);
web = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
web.delegate=self;
web.backgroundColor=[UIColor clearColor];
NSURL *url = [NSURL URLWithString:#"http://wrwr.rww.com/erqrrq"];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[web loadRequest:req];
[web addSubview:activity];
[self.view addSubview:web];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[activity stopAnimating];
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
[activity startAnimating];
}
Try This,
- (void)viewDidLoad
{
[super viewDidLoad];
web = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
web.delegate=self;
web.backgroundColor=[UIColor clearColor];
NSURL *url = [NSURL URLWithString:#"http://wrwr.rww.com/erqrrq"];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[web loadRequest:req];
[self.view addSubview:web];
activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activity.frame=CGRectMake(140, 240, 40, 40);
[self.view addSubview:activity];
[self.view bringSubviewToFront:activity];
}
Define UIActivityIndicatorView after defining UIWebView.
- (void)viewDidLoad
{
[super viewDidLoad];
web = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
web.delegate=self;
web.backgroundColor=[UIColor clearColor];
NSURL *url = [NSURL URLWithString:#"http://wrwr.rww.com/erqrrq"];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[web loadRequest:req];
[self.view addSubview:web];
activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activity.frame=CGRectMake(140, 240, 40, 40);
[self.view addSubview:activity];
}
The order you add the sub views to the super view is significant. The UIActivityIndicatorView gets added and then the UIWebView overlays it. If you add the UIWebView first, i.e
[self.view addSubview:web];
[self.view addSubview:activity];
the UIActivityIndicatorView will overlay the UIWebView.
Related
I have an url which need to access in web view and the url is accessing successfully. but the issue is time is taking more to load the url in webview and hence I need to show a loading image while loading an url. I am trying for that, but not able to get it to show loading bar, loading icon or whatever it may be.
And my code is here goes
NSLog(#"Response ==> %#" ,encodedString);
//'encodedstring' is my url which need to access
// code to show a loading image
indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
indicator.frame = CGRectMake(0, 0, 320, 470);
[self.view addSubview:indicator];
[indicator release];
[indicator startAnimating];
UIWebView *webView;
webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0, 320, 470)];
[webView setDelegate:self];
NSString* urlTwo = [[encodedString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
stringByReplacingOccurrencesOfString:#"%22" withString:#""];
NSURL *url2;
url2 = [[NSURL alloc] initWithString:urlTwo];
NSLog(#"url2:%#", url2);
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url2];
[webView loadRequest:requestObj];
[[self view] addSubview:webView];
}
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
[indicator stopAnimating];
[[self view] addSubview:webView];
}
-(void)webViewDidStartLoad:(UIWebView *)webView
{
[indicator startAnimating];
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
[webView stopLoading];
}
Can anybody is here to solve my issue and make the day happy. Thanks in advance.
-(void)webViewDidFinishLoad:(UIWebView *)aWebView
{
[indicator stopAnimating];
}
-(void)webViewDidStartLoad:(UIWebView *)aWebView
{
[indicator startAnimating];
}
-(void)webView:(UIWebView *)aWebView didFailLoadWithError:(NSError *)error
{
[activityView stopAnimating];
}
have u tried that?
I am trying to do the FourSquare integration in iPhone..i couldn't found any good guidelines through Goggling.... Can any one suggest me the Good guidelines or URLs for FourSquare integration in iPhone..
Thanks
Here is the foursquare api link
Example of Foursquare integration link.
Further code for Foursquare integration
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Foursquare";
//check the authentication
if ([Foursquare2 isNeedToAuthorize]) {
//If needed show the webview
webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
NSString *url = [NSString stringWithFormat:#"https://foursquare.com/oauth2/authenticate?display=touch&client_id=%#&response_type=code&redirect_uri=%#",OAUTH_KEY,REDIRECT_URL];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
[webView loadRequest:request];
[webView setDelegate:self];
[self.view addSubview:webView];
[webView release];
mTableView.hidden = YES;
}else{
//Show your view
mTableView.hidden = NO;
[Foursquare2 searchVenuesNearByLatitude:#"40.763" longitude:#"-73.990" accuracyLL:nil altitude:nil accuracyAlt:nil query:#"" limit:#"15" intent:#"" callback:^(BOOL success, id result){
if (success) {
tableData = [[FoursquareParser parseSearchVenuesResultsDictionary:(NSDictionary*)result] retain];
[mTableView reloadData];
}
}];
}
}
Currently i am developing a sample where i am displaying 4 webview in ipad screen and trying to show 4 different URL in them.
All the web pages are showing in the webview perfectly.
Also i am displaying 4 activity indicator once the webpage starts loading.
But i can not figure out how to stop activity indicator in all the respective webpage one by one once they are loaded in to the screen.
-(void)createWebView
{
//******************** First *********
firstWebView=[[UIWebView alloc]init];
firstWebView.frame=CGRectMake(10, 50, 350, 470);
firstWebView.delegate=self;
firstIndicator = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
firstIndicator.center = CGPointMake(160, 240);
firstIndicator.hidesWhenStopped = YES;
[self.firstWebView addSubview:firstIndicator];
[firstIndicator startAnimating];
firstWebView.backgroundColor=[UIColor grayColor];
NSURL *firstUrl = [NSURL URLWithString:#"http://www.techtree.com"];
NSURLRequest *firstRequestObj = [NSURLRequest requestWithURL:firstUrl];
[firstWebView loadRequest:firstRequestObj];
[self.view addSubview:firstWebView];
//******************* Second *********
secondWebView=[[UIWebView alloc]init];
secondWebView.frame=CGRectMake(405, 50, 350, 470);
secondWebView.delegate=self;
secondIndicator = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
secondIndicator.center = CGPointMake(160, 240);
secondIndicator.hidesWhenStopped = YES;
[self.secondWebView addSubview:secondIndicator];
[secondIndicator startAnimating];
secondWebView.backgroundColor=[UIColor grayColor];
NSURL *secondUrl = [NSURL URLWithString:#"http://www.facebook.com"];
NSURLRequest *secondRequestObj = [NSURLRequest requestWithURL:secondUrl];
[secondWebView loadRequest:secondRequestObj];
[self.view addSubview:secondWebView];
//****************** Third ************
thirdWebView=[[UIWebView alloc] init];
thirdWebView.frame=CGRectMake(10, 528, 350, 470);
thirdWebView.delegate=self;
thirdIndicator = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
thirdIndicator.center = CGPointMake(160, 240);
thirdIndicator.hidesWhenStopped = YES;
[self.thirdWebView addSubview:thirdIndicator];
[thirdIndicator startAnimating];
thirdWebView.backgroundColor=[UIColor grayColor];
NSURL *thirdUrl = [NSURL URLWithString:#"http://www.yahoo.com"];
NSURLRequest *thirdRequestObj = [NSURLRequest requestWithURL:thirdUrl];
[thirdWebView loadRequest:thirdRequestObj];
[self.view addSubview:thirdWebView];
//***************** Fourth ************
fourthWebView=[[UIWebView alloc] init];
fourthWebView.frame=CGRectMake(405,528, 350, 470);
fourthWebView.delegate=self;
fourthIndicator = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
fourthIndicator.center = CGPointMake(160, 240);
fourthIndicator.hidesWhenStopped = YES;
[self.fourthWebView addSubview:fourthIndicator];
[fourthIndicator startAnimating];
fourthWebView.backgroundColor=[UIColor grayColor];
NSURL *fourthUrl = [NSURL URLWithString:#"http://stackoverflow.com"];
NSURLRequest *fourthRequestObj = [NSURLRequest requestWithURL:fourthUrl];
[fourthWebView loadRequest:fourthRequestObj];
[self.view addSubview:fourthWebView];
//******************** Memory Managemt **********
[firstWebView release];
[secondWebView release];
[thirdWebView release];
[fourthWebView release];
[firstIndicator release];
[secondIndicator release];
[thirdIndicator release];
[fourthIndicator release];
}
overwrite below delegate method and check which webview is loading finished like below
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
if(webView== firstWebView)
{
[firstIndicator setHidden:YES];
}
else
{
so on...
}
}
I am developing an sample iPad app where I am displaying some webapages in UIWebview , the code is working properly.But I think the webpages are taking too much time to load in to UIWebview.My code is as below:
-(void)createWebView
{
//******************** First Webview*********
firstWebView=[[UIWebView alloc]init];
firstWebView.frame=CGRectMake(10, 50, 350, 470);
firstWebView.delegate=self;
firstIndicator = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
firstIndicator.center = CGPointMake(160, 240);
firstIndicator.hidesWhenStopped = YES;
[self.firstWebView addSubview:firstIndicator];
[firstIndicator startAnimating];
firstWebView.backgroundColor=[UIColor grayColor];
NSURL *firstUrl = [NSURL URLWithString:#"http://www.techtree.com"];
NSURLRequest *firstRequestObj = [NSURLRequest requestWithURL:firstUrl];
[firstWebView loadRequest:firstRequestObj];
[self.view addSubview:firstWebView];
//******************* Second Webview*********
secondWebView=[[UIWebView alloc]init];
secondWebView.frame=CGRectMake(405, 50, 350, 470);
secondWebView.delegate=self;
secondIndicator = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
secondIndicator.center = CGPointMake(160, 240);
secondIndicator.hidesWhenStopped = YES;
[self.secondWebView addSubview:secondIndicator];
[secondIndicator startAnimating];
secondWebView.backgroundColor=[UIColor grayColor];
NSURL *secondUrl = [NSURL URLWithString:#"http://www.facebook.com"];
NSURLRequest *secondRequestObj = [NSURLRequest requestWithURL:secondUrl];
[secondWebView loadRequest:secondRequestObj];
[self.view addSubview:secondWebView];
//****************** Third Webview************
thirdWebView=[[UIWebView alloc] init];
thirdWebView.frame=CGRectMake(10, 528, 350, 470);
thirdWebView.delegate=self;
thirdIndicator = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
thirdIndicator.center = CGPointMake(160, 240);
thirdIndicator.hidesWhenStopped = YES;
[self.thirdWebView addSubview:thirdIndicator];
[thirdIndicator startAnimating];
thirdWebView.backgroundColor=[UIColor grayColor];
NSURL *thirdUrl = [NSURL URLWithString:#"http://www.yahoo.com"];
NSURLRequest *thirdRequestObj = [NSURLRequest requestWithURL:thirdUrl];
[thirdWebView loadRequest:thirdRequestObj];
[self.view addSubview:thirdWebView];
//***************** Fourth Webview************
fourthWebView=[[UIWebView alloc] init];
fourthWebView.frame=CGRectMake(405,528, 350, 470);
fourthWebView.delegate=self;
fourthIndicator = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
fourthIndicator.center = CGPointMake(160, 240);
fourthIndicator.hidesWhenStopped = YES;
[self.fourthWebView addSubview:fourthIndicator];
[fourthIndicator startAnimating];
fourthWebView.backgroundColor=[UIColor grayColor];
NSURL *fourthUrl = [NSURL URLWithString:#"http://stackoverflow.com"];
NSURLRequest *fourthRequestObj = [NSURLRequest requestWithURL:fourthUrl];
[fourthWebView loadRequest:fourthRequestObj];
[self.view addSubview:fourthWebView];
//******************** Memory Managemt **********
[firstWebView release];
[secondWebView release];
[thirdWebView release];
[fourthWebView release];
}
So how can I make it more faster, please guide me.
For this you have to use NSOpertaionQueue or GCD queue is another options
for my following code, why is my activity indicator for my webview not stoping?
//this part ok
NSURL *theURL = [NSURL URLWithString: Link];
NSURLRequest *request = [NSURLRequest requestWithURL: theURL];
UIWebView * webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,300,300)];
webView.scalesPageToFit = YES;
[webView setDelegate: self];
[webView loadRequest: request];
/*this part ok*/
UIViewController *newController = [[UIViewController alloc] init];
newController.view = webView;
[self.navigationController pushViewController:newController animated:YES ];
//activity indicatior not stoping!
CGRect frame = CGRectMake(0.0, 0.0, 25.0, 25.0);
activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:frame];
[activityIndicator startAnimating];
[activityIndicator sizeToFit];
activityIndicator.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleBottomMargin);
UIBarButtonItem *loadingView = [[UIBarButtonItem alloc] initWithCustomView:activityIndicator];
loadingView.target = newController;
newController.navigationItem.rightBarButtonItem = loadingView;
Thks in advance!
:)
you have to do [activityIndicator stopAnimating] when you want it to stop...
You need to implement the UIWebViewDelegate methods:
- (void)webViewDidStartLoad:(UIWebView *)webView {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
[activityIndicator stopAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
[activityIndicator stopAnimating];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
[activityIndicator stopAnimating];
}