UIActivityIndicatorView not working on programmatically UIWebView in mySubView - iphone

My myViewController.h contains:
#import <UIKit/UIKit.h>
#interface myViewController : UIViewController {
UIScrollView *myScrollView;
UIView *mySubView;
UIWebView *myWebView;
UIActivityIndicatorView *myIndicator;
}
#property (nonatomic, retain) IBOutlet UIScrollView *myScrollView;
#end
My myViewController.m contains:
#import "myViewController.h"
#implementation myViewController
#synthesize myScrollView;
...
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *bgColors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor blueColor], nil];
for (int i = 0; i < bgColors.count; i++)
{
CGRect frameNew;
frameNew.origin.x = self.myScrollView.frame.size.width * i;
frameNew.origin.y = 0;
frameNew.size = self.myScrollView.frame.size;
mySubView = [[UIView alloc] initWithFrame:frameNew];
mySubView.backgroundColor = [bgColors objectAtIndex:i];
[self.myScrollView addSubview:mySubView];
myIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
myIndicator.center = CGPointMake(160, 100);
myIndicator.hidesWhenStopped = NO;
CGRect webFrame = CGRectMake(self.myScrollView.frame.size.width * i, 0, 320, 320);
myWebView = [[UIWebView alloc] initWithFrame:webFrame];
[myWebView setDelegate:(id)self];
[myWebView addSubview:myIndicator];
NSString *urlAddress = #"http://www.google.com/";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[myWebView loadRequest:requestObj];
[self.myScrollView addSubview:myWebView];
[myIndicator release];
[myWebView release];
[mySubView release];
}
self.myScrollView.contentSize = CGSizeMake(self.myScrollView.frame.size.width * bgColors.count, self.myScrollView.frame.size.height);
}
-(void)webViewDidStartLoad: (UIWebView *)webView
{
NSLog(#"Web view did start loading");
[myIndicator startAnimating];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
-(void)webViewDidFinishLoad: (UIWebView *)webView
{
NSLog(#"Web view did finish loading");
[myIndicator stopAnimating];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
[myIndicator stopAnimating];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
NSString* errorString = [NSString stringWithFormat:#"<html>%#</html>", error.localizedDescription];
[myWebView loadHTMLString:errorString baseURL:nil];
}
...
Load indicator does not work on the first two mySubView in myWebView, works only on the last mySubView in myWebView.
In the first two mySubView in myWebView, myIndicator displayed but not active.
Tell me what's wrong doing?

That is because myIndicator is pointing to the last activity indicator view. All previous references are lost. Luckily, since it is a subview of the web view, we can get a reference to it. This should help –
-(void)webViewDidStartLoad: (UIWebView *)webView
{
UIActivityIndicatorView *actView;
for ( id object in webView.subviews ) {
if ([object isMemberOfClass:[UIActivityIndicatorView class]]) {
actView = (UIActivityIndicatorView*)object;
}
}
NSLog(#"Web view did start loading");
[actView startAnimating];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
-(void)webViewDidFinishLoad: (UIWebView *)webView
{
UIActivityIndicatorView *actView;
for ( id object in webView.subviews ) {
if ([object isMemberOfClass:[UIActivityIndicatorView class]]) {
actView = (UIActivityIndicatorView*)object;
}
}
NSLog(#"Web view did finish loading");
[actView stopAnimating];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
UIActivityIndicatorView *actView;
for ( id object in webView.subviews ) {
if ([object isMemberOfClass:[UIActivityIndicatorView class]]) {
actView = (UIActivityIndicatorView*)object;
}
}
[actView stopAnimating];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
NSString* errorString = [NSString stringWithFormat:#"<html>%#</html>", error.localizedDescription];
[myWebView loadHTMLString:errorString baseURL:nil];
}

Related

Webview content is not readable after stop animating loading image

I am building one iPhone application where I have two views one for login view and another for web view. When user enters the login credentials, then it will redirect to web view. while loading the webview from login view too much time is taking to load. hence I used alertview to show loading image.
#interface FocusViewController ()
#end
#implementation FocusViewController
#synthesize txtsecurecode;
#synthesize webView;
#synthesize OrganizationCode;
UIActivityIndicatorView *indicator;
UIAlertView *alert;
- (void)viewDidLoad
{
[super viewDidLoad];
appDelegate = (FocusAppDelegate *)[[UIApplication sharedApplication]delegate];
if(appDelegate.data.strOrganizationCode == nil || appDelegate.data.strAccessCode == nil)
{
NSLog(#"YOUR FIRST SCREEN");
_loginView.hidden = NO;
webView.hidden = YES;
}
else
{
NSLog(#"LOAD WEBVIEW DIRECTLY\n");
NSLog(#"Organization Code -> %# \n Secure Access Code -> %#",appDelegate.data.strOrganizationCode,appDelegate.data.strAccessCode);
OrganizationCode.text = appDelegate.data.strOrganizationCode ;
txtsecurecode.text = appDelegate.data.strAccessCode;
[self loginClicked:nil];
webView.hidden = NO;
_loginView.hidden = YES;
}
}
- (IBAction)loginClicked:(id)sender {
#try {
if([[txtsecurecode text] isEqualToString:#""] || [[OrganizationCode text] isEqualToString:#""] ) {
[self alertStatus:#"Please enter Access code" :#"Login Failed!":0];
}
else
{
NSString *post =[[NSString alloc] initWithFormat:#"txtsecurecode=%# #&password=%#",[txtsecurecode text],[OrganizationCode text]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"http://myexample.com/AccountService/security/ValidateAccess?accesscode=%#&companycode=%#&type=1", txtsecurecode.text, OrganizationCode.text]];
NSString *responseData = [[NSString alloc]initWithData:[NSData dataWithContentsOfURL:url] encoding:NSUTF8StringEncoding];
if([responseData isEqualToString:#""]){
[self alertStatus:#"Please enter valid Access Code" :#"Login Failed !" :0];
}
else
{
appDelegate.data.strOrganizationCode = OrganizationCode.text;
appDelegate.data.strAccessCode = txtsecurecode.text;
[FocusAppDelegate addCustomObjectToUserDefaults:appDelegate.data key:kCredentails];
//Updated
_loginView.hidden = YES;
webView.hidden = NO;
responseData = [responseData stringByReplacingOccurrencesOfString:#" "" " withString:#""];
NSString* encodedString = [responseData stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(#"Response ==> %#" ,encodedString);
alert = [[[UIAlertView alloc] initWithTitle:#"Loading\nPlease Wait..." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil] autorelease];
[alert show];
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
indicator.center = CGPointMake(alert.bounds.size.width / 2, alert.bounds.size.height - 50);
[indicator startAnimating];
[alert addSubview:indicator];
[indicator release];
webView.backgroundColor = [UIColor clearColor];
webView.opaque = NO;
webView.delegate = self;
webView.frame = self.view.bounds;
NSString* urlTwo = [[encodedString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
stringByReplacingOccurrencesOfString:#"%22" withString:#""];
NSURL *url2;
if([urlTwo hasPrefix:#"http://"]){
url2 = [NSURL URLWithString:urlTwo];
}else{
url2 = [NSURL URLWithString:[NSString stringWithFormat:#"http://%#" , urlTwo]];
}
NSLog(#"url2:%#", url2);
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url2];
[webView loadRequest:requestObj];
[[self view] addSubview:webView];
}
}
}
#catch (NSException * e) {
NSLog(#"Exception: %#", e);
[self alertStatus:#"Login Failed." :#"Login Failed!" :0];
}
}
-(void)webViewDidStartLoad:(UIWebView *)webView{
NSLog(#"webViewDidStartLoad");
[self.view addSubview:self.indicator];
alert.hidden = NO;
}
- (void) webViewDidFinishLoad:(UIWebView *)webView {
NSLog(#"webViewDidFinishLoad");
[self.indicator removeFromSuperview];
[self.alert dismissWithClickedButtonIndex:1 animated:NO] ;
}
- (IBAction)backgroundClick:(id)sender
{
[txtsecurecode resignFirstResponder];
[OrganizationCode resignFirstResponder];
}
#end
Until content in webview displays, loading image is displaying and working good. but content of webview is not editable and remaining static as I think I used 'web view.hidden = YES'. when I comment the alert method and run then content of webview is also editable and working good as required. I need to load the content of url after loading image stops loading.
I would suggest show the activity indicator from the Viewcontroller having the WebView and implement the WebView Delegate methods
- (void)webViewDidStartLoad:(UIWebView *)webView
{
[actvityIndicator startAnimating];
}
and
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[actvityIndicator stopAnimating];
}
this will keep your webview Interactive and also show the indicator till the page loads completely
you can try something like this. The Delegate is important in .h file
FocusViewController.h
#interface FocusViewController : UIViewController <UIWebViewDelegate> {
UIActivityIndicatorView *indicator;
}
FocusViewController.m
- (void)viewDidLoad
{
// Loading Indicator
indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[indicator setColor:[UIColor colorWithRed:50.0/255.0 green:120.0/255.0 blue:200.0/255.0 alpha:1.0]];
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
[indicator startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[indicator stopAnimating];
}

Can i load images from the device's documents directory to AsyncImageView?

Actually, I am not sure about this but i have this condition where i check whether internet is available or not and accordingly load the image into AsyncImageView. It works fine when i load any image from the internet, but is it possible to load a local image to AsyncImageView?
I guess you mean AsyncImageView by Nick Lockwood? This is simply a category to UIImageView, so when you have the image already, why not set the image property directly?
Actually i got the solution to this problem.All i did was this
NSURL *theURL = [NSURL fileURLWithPath:#"/Users/gaurav/Library/Application Support/iPhone Simulator/5.0/Applications/AC6E9B2E-DB25-4933-A338-755B134E1A61/Documents/Attachment290_294_1344928691.jpeg"];
NSLog(#"local URl is::%#",theURL);
[self.asyncimageview loadImageFromURL:theURL];
Perhaps the following will help you.
Make a new file
Inherit from UIImageView
Copy/Paste the following code.
import
import "ASIHTTPRequest.h"
#interface AsynchronousImageView : UIImageView{
NSMutableData *data;
UIActivityIndicatorView *activityIndicator;
NSString *urlString; // key for image cache dictionary
ASIHTTPRequest *requestLocal;
}
#property(nonatomic,assign) UIActivityIndicatorView *activityIndicator;
(void)loadImageFromURLString:(NSString *)theUrlString;
(void)loadImageFromNSURL:(NSURL *)theUrl;
(void)loadImageFromFile:(NSString *)filePath;
-(void) showLoader;
-(void) stopLoader;
#end
Then In .m file paste the following.
import "AsynchronousImageView.h"
import "ASIDownloadCache.h"
#implementation AsynchronousImageView
#synthesize activityIndicator;
(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
//[self setContentMode:UIViewContentModeScaleAspectFit];
}
return self;
}
-(void)dealloc
{
if (requestLocal) {
[requestLocal setDelegate:nil];
}
requestLocal = nil;
activityIndicator = nil;
[super dealloc];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
(void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
-(void) showLoader{
if(activityIndicator == nil){
activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[self addSubview:activityIndicator];
activityIndicator.frame = CGRectMake(self.bounds.size.width / 2.0f - activityIndicator.frame.size.width /2.0f, self.bounds.size.height / 2.0f - activityIndicator.frame.size.height /2.0f, activityIndicator.frame.size.width, activityIndicator.frame.size.height);
[activityIndicator release];
}
[activityIndicator startAnimating];
}
-(void) stopLoader{
if(activityIndicator){
[activityIndicator stopAnimating];
[activityIndicator removeFromSuperview];
activityIndicator = nil;
}
}
(void)loadImageFromFile:(NSString *)filePath {
UIImage *img = [UIImage imageWithContentsOfFile:filePath];
[self setImage:img];
[self stopLoader];
}
(void)loadImageFromURLString:(NSString *)theUrlString
{
/if (requestLocal) {
[requestLocal setDelegate:nil];
}/
requestLocal = nil;
[self showLoader];
NSURL *url = [NSURL URLWithString:theUrlString];
requestLocal = [ASIHTTPRequest requestWithURL:url];
[requestLocal setDelegate:self];
[requestLocal setDidFailSelector:#selector(requestFailed)];
[requestLocal setDownloadCache:[ASIDownloadCache sharedCache]];
[requestLocal setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
[requestLocal startAsynchronous];
self.image = [UIImage imageNamed:#"playerDefault.png"];
}
-(void)loadImageFromNSURL:(NSURL *)theUrl
{
if (requestLocal) {
[requestLocal setDelegate:nil];
}
requestLocal = nil;
requestLocal = [ASIHTTPRequest requestWithURL:theUrl];
[requestLocal setDelegate:self];
[requestLocal setDidFailSelector:#selector(requestFailed)];
[requestLocal startAsynchronous];
}
(void)requestFinished:(ASIHTTPRequest *)request
{
requestLocal = nil;
[self stopLoader];
UIImage *tempArt = [UIImage imageWithData:[request responseData]];
if (tempArt) {
self.image = tempArt;
}
[data release];
data = nil;
}
-(void)requestFailed:(ASIHTTPRequest *)request{
requestLocal = nil;
[self stopLoader];
[data release];
data = nil;
}
#end
Then in your ViewComtroller .h import AsycnhronousImageView class and write this
Make the IBOutlet of your image like this.
IBOutlet AsycnhronousImageView *image;
Change the class in xib AsycnhronousImageView rather than UIImage View
Call the following Method
(void)loadImageFromFile:(NSString *)filePath;

show images on tabelview that are come from webservice

I have used the below code. In this code I am showing images that are coming from the webservices. To increase the scroll speed of tabelview I use UIImageView+WebCache it increase the scrolling speed fast but image is show when I touch the imageview. How to show the images when tabelview is display
NSString *str=[self.uploadimagearry objectAtIndex:indexPath.row];
// NSString *str1=[str stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
NSURL *uploadimageURL = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
// NSData *imgdata=[NSData dataWithContentsOfURL:uploadimageURL];
//UIImage * uploadimage = [UIImage imageWithData:imgdata];
cell.imageView.frame=CGRectMake(0, -15, 50, 35);
//[cell.imageView setImageWithURL:uploadimageURL];
[cell.imageView setImageWithURL:uploadimageURL];
You have to start NSUrlConnection in a different run-loop, so that you receive data while the table is scrolling.
Just check out below examples :
LazyTableImages
Lazy loading multiple images on background threads on the iPhone
//JImage.h
#import <Foundation/Foundation.h>
#interface JImage : UIImageView {
NSURLConnection *connection;
NSMutableData* data;
UIActivityIndicatorView *ai;
}
-(void)initWithImageAtURL:(NSURL*)url;
#property (nonatomic, retain) NSURLConnection *connection;
#property (nonatomic, retain) NSMutableData* data;
#property (nonatomic, retain) UIActivityIndicatorView *ai;
#end
//JImage.m
#import "JImage.h"
#implementation JImage
#synthesize ai,connection, data;
-(void)initWithImageAtURL:(NSURL*)url {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[self setContentMode:UIViewContentModeScaleToFill];
if (!ai){
[self setAi:[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]];
[ai startAnimating];
[ai setFrame:CGRectMake(27.5, 27.5, 20, 20)];
[ai setColor:[UIColor blackColor]];
[self addSubview:ai];
}
NSURLRequest* request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- (void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)incrementalData {
if (data==nil) data = [[NSMutableData alloc] initWithCapacity:5000];
[data appendData:incrementalData];
NSNumber *resourceLength = [NSNumber numberWithUnsignedInteger:[data length]];
NSLog(#"resourceData length: %d", [resourceLength intValue]);
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(#"Connection error...");
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[ai removeFromSuperview];
}
- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[self setImage:[UIImage imageWithData: data]];
[ai removeFromSuperview];
}
#end
//Include the definition in your class where you want to use the image
-(UIImageView*)downloadImage:(NSURL*)url:(CGRect)frame {
JImage *photoImage=[[JImage alloc] init];
photoImage.backgroundColor = [UIColor clearColor];
[photoImage setFrame:frame];
[photoImage setContentMode:UIViewContentModeScaleToFill];
[photoImage initWithImageAtURL:url];
return photoImage;
}
//How to call the class
UIImageView *imagV=[self downloadImage:url :rect];
//you can call the downloadImage function in looping statement and subview the returned imageview.
//it will help you in lazy loading of images.
//Hope this will help

Is it possibile get thumbail image from .doc or .xls document?

I'm looking for create an image thumbnail from a MS doc/xls document's page,
but I found nothing about it.
For pdf documents I used Quarz framework, but I can't in this case.
Some help?
A web view can be used for making a MS doc preview.
I've tried once to do that with this piece of code.
It works ... but ... the web view need to work in graphical thread, so when this operation is running your interface is slower. Maybe can you optimized that.
Header
#interface WebViewThumbnailGenerationOperation: NSOperation <UIWebViewDelegate> {
BOOL finished;
}
#property(nonatomic,retain) NSURL* documentURL;
#property(nonatomic,retain) UIWebView* webView;
-(void)saveThumbnail:(UIImage*)thumbnail;
#end
Code
/**************************************************************************************************/
#pragma mark - WebViewBased Thumbnails
#implementation WebViewThumbnailGenerationOperation
#synthesize documentURL,webView;
-(void)dealloc {
RELEASE_SAFELY(documentURL);
[super dealloc];
}
- (void)loadWebView {
if (self.isCancelled) {
return;
}
self.webView = [[[UIWebView alloc] init] autorelease];
self.webView.delegate = self;
self.webView.scalesPageToFit = YES;
self.webView.frame = CGRectMake(0, 0, 290, 290);
NSURLRequest *request = [NSURLRequest requestWithURL:documentURL];
[self.webView loadRequest:request];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
if (self.isCancelled) {
return;
}
UIGraphicsBeginImageContext(CGSizeMake(290,290));
CGContextRef context = UIGraphicsGetCurrentContext();
[self.webView.layer renderInContext:context];
UIImage *thumbnail = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self performSelectorInBackground:#selector(saveThumbnail:) withObject:thumbnail];
self.webView = nil;
}
-(void)saveThumbnail:(UIImage*)thumbnail {
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
if (self.isCancelled) {
return;
}
if(!thumbnail) {
return;
}
NSData* thumbnailData = UIImageJPEGRepresentation(thumbnail,0.8);
[IOHelper saveThumbnailData:thumbnailData forDocumentURL:documentURL];
[self willChangeValueForKey:#"isFinished"];
finished = YES;
[self didChangeValueForKey:#"isFinished"];
[pool release];
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
NSLog(#"Preview failed for %# error %#",document.name,error);
[self willChangeValueForKey:#"isFinished"];
finished = YES;
[self didChangeValueForKey:#"isFinished"];
self.webView = nil;
}
-(void)start {
finished = NO;
[super start];
}
- (void)main {
if (self.isCancelled) {
return;
}
[self performSelectorOnMainThread:#selector(loadWebView) withObject:nil waitUntilDone:YES];
}
-(BOOL)isFinished {
return finished;
}
#end
Edit: ARC version!
Header
#interface WebViewThumbnailGenerationOperation: NSOperation <UIWebViewDelegate>
#property(nonatomic, strong) NSURL* documentURL;
#property(nonatomic, strong) UIWebView* webView;
#property(nonatomic) BOOL finished;
-(void)saveThumbnail:(UIImage*)thumbnail;
#end
Code
/**************************************************************************************************/
#pragma mark - WebViewBased Thumbnails
#implementation WebViewThumbnailGenerationOperation
- (void)loadWebView {
if (self.isCancelled) {
return;
}
self.webView = [[UIWebView alloc] init];
self.webView.delegate = self;
self.webView.scalesPageToFit = YES;
self.webView.frame = CGRectMake(0, 0, 290, 290);
NSURLRequest *request = [NSURLRequest requestWithURL:documentURL];
[self.webView loadRequest:request];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
if (self.isCancelled) {
return;
}
UIGraphicsBeginImageContext(CGSizeMake(290,290));
CGContextRef context = UIGraphicsGetCurrentContext();
[self.webView.layer renderInContext:context];
UIImage *thumbnail = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self performSelectorInBackground:#selector(saveThumbnail:) withObject:thumbnail];
self.webView = nil;
}
-(void)saveThumbnail:(UIImage*)thumbnail {
#autoreleasepool {
if (self.isCancelled) {
return;
}
if(!thumbnail) {
return;
}
NSData* thumbnailData = UIImageJPEGRepresentation(thumbnail,0.8);
[IOHelper saveThumbnailData:thumbnailData forDocumentURL:documentURL];
[self willChangeValueForKey:#"isFinished"];
finished = YES;
[self didChangeValueForKey:#"isFinished"];
}
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
NSLog(#"Preview failed for %# error %#", document.name, error);
[self willChangeValueForKey:#"isFinished"];
finished = YES;
[self didChangeValueForKey:#"isFinished"];
self.webView = nil;
}
-(void)start {
finished = NO;
[super start];
}
- (void)main {
if (self.isCancelled) {
return;
}
[self performSelectorOnMainThread:#selector(loadWebView) withObject:nil waitUntilDone:YES];
}
-(BOOL)isFinished {
return finished;
}
#end

UIWebView leaking

I have a class that extends UIViewController and implements the UIWebViewDelegate, like this:
#interface TableViewController : UIViewController <UIWebViewDelegate, UIAlertViewDelegate>{
UIWebView *articleWebView;
NSString *link;
UIActivityIndicatorView *activityIndicator;
NSURL * safariUrl;
}
I'm trying to load a web page into the uiwebview, everything works fine, but i'm getting some leaks (like GeneralBlock-56, GeneralBlock-1024 etc) and i can't find out where they come from.
This is what i am doing:
- (void)viewDidLoad {
[super viewDidLoad];
if (articleWebView){
[articleWebView loadHTMLString: #"" baseURL: nil];
[articleWebView release];
articleWebView = nil;
}
articleWebView = [[UIWebView alloc] initWithFrame: CGRectMake(0,0,320,380)];
if (activityIndicator){
[activityIndicator release];
activityIndicator = nil;
}
activityIndicator = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(self.view.bounds.size.width/2-24, self.view.bounds.size.height/3-12, 50, 50)];
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
[self.view addSubview:activityIndicator];
link = [link stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
link = [link stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
link = [(NSString*) CFURLCreateStringByAddingPercentEscapes (NULL, (CFStringRef)link, NULL,
NULL, kCFStringEncodingUTF8) autorelease];
NSURL *url = [NSURL URLWithString:link];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[[self articleWebView] setDelegate: self];
[articleWebView loadRequest:requestObj];
[link release];
}
//////////////////////////////////////////////
-(void)webViewDidFinishLoad:(UIWebView *)webView{
if (activityIndicator){
[activityIndicator stopAnimating];
[activityIndicator release];
activityIndicator = nil;
self.view = articleWebView;
}
//////////////////////////////////////////////
- (void)viewDidUnload {
[super viewDidUnload];
[articleWebView loadHTMLString: #"" baseURL: nil];
[self.articleWebView release];
self.articleWebView = nil;
}
//////////////////////////////////////////////
- (void)dealloc {
self.articleWebView.delegate=nil;
[super dealloc];
}
What am i doing wrong? Thank you in advance.
Along with setting the webView delegate to nil, you should ask your webView to stop loading. Add the following lines of code and then try:
- (void)dealloc {
self.articleWebView.delegate = nil;
[articleWebView stopLoading];
[articleWebView release];
[super dealloc];
}
If articeWebview is an IBOutlet why is it that you want to allocate it??