应用程序崩溃是由于的WebView

问题描述:

在我的应用我使用的UIWebView显示网页,但是每当我单击后退按钮并开始与应用程式中其他部分播放,应用程序大多是没有理由崩溃。我怀疑webview导致这个问题,因为它只有当我尝试打开webview时崩溃。应用程序崩溃是由于的WebView

我已经使用NSURLConnection的加载网页视图,并取得了web视图,连接对象为nil在视图中会消失方法。

@implementation NewsWebSiteViewController 

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

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    @try { 
     self.webView.delegate=self; 
     [UIApplication sharedApplication].networkActivityIndicatorVisible=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 
+0

有只需要重新格式化您的代码(**请使用所提供的编辑器控件在未来**),我不得不说,我并不感到惊讶你有问题 - 有不少空IFS等奇在那里建设。 (使用`如果(XYZ!=无)...`是完全合法的,等等) – 2011-02-06 21:59:04

+0

您好我离开IF条件空东阳我不想,如果条件为真,以处理任何事情。我知道你的意思是如果(xyz!= nil),你的意思是我不能使用if(xyz == nil)?请帮忙! – likki 2011-02-06 22:13:32

首先,这里有一个小底漆平等/不平等的C/Objective-C的:

比方说,你有一个BOOL值(即,可以是YES或NO,“论值'或'关','真'或'假'),称为isEnabled。现在,如果我已经分配了此BOOL值(有时称为“标志”)为“YES”,我可以有条件地测试它的价值,像这样:

BOOL isEnabled = YES; 

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

除了上述,我可以使用否定运算符(感叹号 - 另外称为NOT运算符)翻转isEnabled的值,并测试其相反值:在上面的例子isEnabled设置为YES

BOOL isEnabled = YES; 

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

现在当然,这样的条件将失败。但是,如果我们考虑下面的例子,因此如果else if是“满足”(即真)在一系列if的和else if的任何地方,它会执行所有的代码里面,再if财产否则忽略任何东西:

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 
} 

虽然上面在年底有两个冗余else if的,这是演示代码是如何工作条件的好方法。

所以在你webViewDidFinishLoad:方法,而不是一个空白,如果来评估的if/else条件,可以替换成一个简单的条件:

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

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

当你作出了上述变化所有你的if,如果是,发布一个堆栈跟踪,我会看看还有什么可能。