gpt4 book ai didi

iphone - 应用因WebView而崩溃

转载 作者:行者123 更新时间:2023-12-03 16:57:07 26 4
gpt4 key购买 nike

在我的应用程序中,我使用UIwebview来显示网站,但是,每当我单击“后退”按钮并开始与应用程序中的其他部分一起播放时,该应用程序大多会无故崩溃。我怀疑Webview导致了此问题,因为仅当我尝试打开W​​ebview时它才崩溃。

我已经使用NSURLConnection加载了webview并创建了webview, View 中的连接对象为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

最佳答案

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

假设您有一个BOOL值(即可以为YES或NO,“On”或“Off”,“True”或“False”的值),称为isEnabled。现在,如果我已将此BOOL值(有时称为“标志”)分配为"is",则可以有条件地测试其值,如下所示:

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在一系列 ifelse if中的任何位置为“met”(即true),它将执行其中的所有代码,然后忽略其他任何事情:
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进行了上述更改后,发布堆栈跟踪,我会看到它还有什么可能。

关于iphone - 应用因WebView而崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4916415/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com