【问题标题】:App crashes due to WebView由于 WebView 导致应用程序崩溃
【发布时间】:2011-02-06 21:52:36
【问题描述】:

在我的应用程序中,我使用 UIwebview 来显示网站,但是每当我单击后退按钮并开始使用应用程序中的其他部分时,应用程序大多会无缘无故地崩溃。我怀疑是 webview 导致了这个问题,因为它只有在我尝试打开 webview 时才会崩溃。

我已经使用NSURLConnection来加载webview并制作了webview,视图中为nil的连接对象将Disappear方法。

@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

【问题讨论】:

  • 只需要重新格式化您的代码(请在以后使用提供的编辑器控件)我不得不说我对您遇到问题并不感到惊讶 - 有那里有很多空的 if 和其他奇怪的结构。 (使用if(xyz != nil)... 是完全合法的,等等)
  • 嗨,我将 IF 条件留空,因为如果条件为真,我不想处理任何内容。我不明白你所说的 if(xyz!=nil) 是什么意思,你的意思是我不能使用 if(xyz==nil) 吗?请帮忙!
  • 在 C 风格的语言中,!= 检查 inequality。如,仅当条件 not 满足时才返回 true。这基本上可以让您编写更整洁的代码。我建议你把所有这些代码都扔掉,然后再试一次。或者,您可以向我们展示您的崩溃的堆栈跟踪。
  • 嗨,我基本上是应用程序开发的新手,你能告诉我如何查看堆栈跟踪吗?

标签: iphone uiwebview crash


【解决方案1】:

首先,这里是 C/Objective-C 中相等/不等式的小入门:

假设您有一个名为isEnabled 的BOOL 值(即,可以是“是”或“否”、“开”或“关”、“真”或“假”的值)。现在,如果我将这个 BOOL 值(有时称为“标志”)分配给“是”,我可以像这样有条件地测试它的值:

BOOL isEnabled = YES;

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

除了上述之外,我还可以使用否定运算符(感叹号 - 也称为 NOT 运算符)来翻转isEnabled 的值,并测试它的相反值:

BOOL isEnabled = YES;

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

现在当然在上面的例子中isEnabled 设置为YES,所以条件会失败。但是,如果我们考虑下面的例子,if 的属性,即如果else if 在一系列if 和else 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 来评估 if/else 条件:

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

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

当您对所有 if 和 else if 进行了上述更改后,发布一个堆栈跟踪,我会看看它可能是什么。

【讨论】:

  • 感谢你的努力,我现在会更新我的代码中的 if 条件,但是你能告诉我如何找到堆栈跟踪,它是我们用来查看的调试命令窗口吗异常错误?谢谢大家!
猜你喜欢
  • 1970-01-01
  • 2012-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-17
  • 2016-08-05
  • 1970-01-01
相关资源
最近更新 更多