开发者

App crashes due to WebView

In my app I am using UIwebview to display websites, however whenever I click back button and start playing with other parts in the app, the app mostly crashes with no reason. I suspect the webview is causing this issue because it crashes only when I try to open the webview.

I have used NSURLConnection to load the webview and have made webview, connection objects to nil in the view will Disappear method.

@implementation NewsWebSiteViewController

@synthesize connection,rcvdData,spinner1,currentSite,webView,newsWebsite;

- (void)viewDidLoad {
    [super viewDidLoad];
    @try {
        self.webView.delegate=self;
        [UIApplication sharedApplication].networkAct开发者_C百科ivityIndicatorVisible=YES;
        self.title= self.newsWebsite.title;
        self.webView.backgroundColor =[UIColor groupTableViewBackgroundColor];
        self.spinner1 = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        CGRect center = [self.view bounds];
        CGSize winCenter = center.size;
        CGPoint pont = CGPointMake(winCenter.width/2,winCenter.height/2);
        [spinner1 setCenter:pont];
        [self.view addSubview:spinner1];
        [self.spinner1 startAnimating];
        NSString *url = newsWebsite.link;
        NSURLRequest *theReq =[NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0];
        self.connection = [[NSURLConnection alloc] initWithRequest:theReq delegate:self];
        if(self.connection) {
            self.rcvdData = [[NSMutableData data] retain];
        }
        else {
        UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Error!" message:@"We are having a problem connecting to the internet, why not try again or try sometime later!.." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
        [alert show];
        }
        webView.multipleTouchEnabled=YES;
        webView.scalesPageToFit=YES;
    }
    @catch (NSException * e) {
        }  
}

-(void) goBack {
    self.webView =nil;
    [self.navigationController popViewControllerAnimated:YES];
}

-(void) viewWillDisappear:(BOOL)animated {

    [self.connection cancel];
    self.connection=nil;
    [self.webView stopLoading];
    self.webView=nil;

    [UIApplication sharedApplication].networkActivityIndicatorVisible=NO;

    if (self.spinner1 ==nil) {

    }

    else {
        [self.spinner1 stopAnimating];
    }

}

-(void) webViewDidFinishLoad:(UIWebView *)webView {

    [UIApplication sharedApplication].networkActivityIndicatorVisible=NO;

    if (self.spinner1 ==nil) {

    }
    else {
        [self.spinner1 stopAnimating];
    }
}

-(void) viewDidAppear:(BOOL)animated  {
    [UIApplication sharedApplication].networkActivityIndicatorVisible=NO;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


- (void)viewDidUnload {
    [super viewDidUnload];
}


-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [self.rcvdData setLength:0];
}


-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [self.rcvdData appendData:data];
}


-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    if (self.spinner1 ==nil) {
        }
    else {
        [self.spinner1 stopAnimating];
        }
    [connection release];
    [rcvdData release];
    UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Error!" message:@"We are having a problem connecting to the internet, why not try again or try sometime later!.." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
    [alert show];
}


-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
    [connection release];
    [rcvdData release];
    NSString *url = newsWebsite.link;
    NSURL *url1 =  [NSURL URLWithString:url];
    [self.webView loadData:self.rcvdData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:url1];
}

- (void)dealloc {
    [super dealloc];
    [self.spinner1 release];
}

@end


Firstly, here's a small primer to equality/inequality in C/Objective-C:

Let's say you have a BOOL value (ie, a value that can be either YES or NO, 'On' or 'Off', 'True' or 'False'), called isEnabled. Now, if I had assigned this BOOL value (sometimes called a 'flag') to 'YES', I could conditionally test its value like so:

BOOL isEnabled = YES;

if (isEnabled)
{
   // value set to yes
}

In addition to the above, I can use the negation operator (an exclamation mark - otherwise known as the NOT operator) to flip isEnabled's value, and test its opposite value:

BOOL isEnabled = YES;

// the following reads as "if is *not* enabled"
if (!isEnabled)
{
   // value set to no
}

now of course in the above example isEnabled is set to YES, so the condition will fail. But, if we consider the following example, the property of if whereby if an else if is 'met' (ie, true) anywhere in a series of if's and else if's, it will execute all the code inside it, and then ignore anything else:

BOOL isEnabled = NO;

if (isEnabled)
{
   // any code here will not run, as the above if condition will be false
}
else if (!isEnabled)
{
   // this code will run, since the above condition (!isEnabled) will evaluate to true
}
else if (isEnabled)
{
   // this code could never run, since the previous condition was true and all following else if's are ignored
}
else if (!isEnabled)
{
   // this code could never run, since the previous condition was true and all following else if's are ignored
}

while the above has two redundant else if's at the end, it is a good way to demonstrate how conditional code works.

So, in your webViewDidFinishLoad: method, instead of having a blank if to evaluate an if/else condition, you can replace that with a simpler condition:

-(void) webViewDidFinishLoad:(UIWebView *)webView 
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible=NO;

    // or, (self.spinner1 != nil)
    if (!self.spinner1) 
    {
        [self.spinner1 stopAnimating];
    }
}

When you've made the above change to all your if's and else if's, post a stack trace and I'll see what else it could be.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜